Skip to content

Commit a3fda04

Browse files
committed
Cleanup unnecessary try-catch statements
1 parent 290d72d commit a3fda04

File tree

8 files changed

+57
-97
lines changed

8 files changed

+57
-97
lines changed

src/Assert/CIDRTrait.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace SimpleSAML\SAML2\Assert;
66

7-
use SimpleSAML\Assert\AssertionFailedException;
87
use SimpleSAML\SAML2\Exception\ProtocolViolationException;
98

109
/**
@@ -42,19 +41,14 @@ trait CIDRTrait
4241

4342

4443
/**
45-
* @param string $value
46-
* @param string $message
4744
*/
4845
protected static function validCIDR(string $value, string $message = ''): void
4946
{
50-
try {
51-
parent::regex(
52-
$value,
53-
self::$cidr_regex,
54-
$message ?: '%s is not a valid RFC4632 CIDR-block',
55-
);
56-
} catch (AssertionFailedException $e) {
57-
throw new ProtocolViolationException($e->getMessage());
58-
}
47+
parent::regex(
48+
$value,
49+
self::$cidr_regex,
50+
$message ?: '%s is not a valid RFC4632 CIDR-block',
51+
ProtocolViolationException::class,
52+
);
5953
}
6054
}

src/Assert/DomainTrait.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace SimpleSAML\SAML2\Assert;
66

7-
use SimpleSAML\Assert\AssertionFailedException;
87
use SimpleSAML\SAML2\Exception\ProtocolViolationException;
98

109
/**
@@ -20,19 +19,14 @@ trait DomainTrait
2019

2120

2221
/**
23-
* @param string $value
24-
* @param string $message
2522
*/
2623
protected static function validDomain(string $value, string $message = ''): void
2724
{
28-
try {
29-
parent::regex(
30-
$value,
31-
self::$domain_regex,
32-
$message ?: '%s is not a valid domain name',
33-
);
34-
} catch (AssertionFailedException $e) {
35-
throw new ProtocolViolationException($e->getMessage());
36-
}
25+
parent::regex(
26+
$value,
27+
self::$domain_regex,
28+
$message ?: '%s is not a valid domain name',
29+
ProtocolViolationException::class,
30+
);
3731
}
3832
}

src/Assert/EntityIDTrait.php

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace SimpleSAML\SAML2\Assert;
66

7-
use SimpleSAML\Assert\AssertionFailedException;
87
use SimpleSAML\SAML2\Constants as C;
98
use SimpleSAML\SAML2\Exception\ProtocolViolationException;
109

@@ -16,22 +15,17 @@
1615
trait EntityIDTrait
1716
{
1817
/**
19-
* @param string $value
20-
* @param string $message
2118
*/
2219
protected static function validEntityID(string $value, string $message = ''): void
2320
{
2421
static::validSAMLAnyURI($value);
2522

26-
try {
27-
parent::notWhitespaceOnly($value);
28-
parent::maxLength(
29-
$value,
30-
C::ENTITYID_MAX_LENGTH,
31-
sprintf('An entityID cannot be longer than %d characters.', C::ENTITYID_MAX_LENGTH),
32-
);
33-
} catch (AssertionFailedException $e) {
34-
throw new ProtocolViolationException($e->getMessage());
35-
}
23+
parent::notWhitespaceOnly($value, ProtocolViolationException::class);
24+
parent::maxLength(
25+
$value,
26+
C::ENTITYID_MAX_LENGTH,
27+
sprintf('An entityID cannot be longer than %d characters.', C::ENTITYID_MAX_LENGTH),
28+
ProtocolViolationException::class,
29+
);
3630
}
3731
}

src/Assert/GeolocationTrait.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace SimpleSAML\SAML2\Assert;
66

7-
use SimpleSAML\Assert\AssertionFailedException;
87
use SimpleSAML\SAML2\Exception\ProtocolViolationException;
98

109
/**
@@ -16,8 +15,6 @@ trait GeolocationTrait
1615
//private static string $geolocation_regex = '/^geo:([-+]?\d+(?:\.\d+)?),([-+]?\d+(?:\.\d+)?)(?:\?z=(\d{1,2}))?$/';
1716

1817
/**
19-
* @param string $value
20-
* @param string $message
2118
*/
2219
protected static function validGeolocation(string $value, string $message = ''): void
2320
{
@@ -28,10 +25,6 @@ protected static function validGeolocation(string $value, string $message = ''):
2825
// );
2926
// The regex above is incomplete, so for now we only test for a valid URI
3027

31-
try {
32-
static::validAnyURI($value);
33-
} catch (AssertionFailedException $e) {
34-
throw new ProtocolViolationException($e->getMessage());
35-
}
28+
static::validAnyURI($value, ProtocolViolationException::class);
3629
}
3730
}

src/Assert/RelayStateTrait.php

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace SimpleSAML\SAML2\Assert;
66

7-
use SimpleSAML\Assert\AssertionFailedException;
87
use SimpleSAML\SAML2\Constants as C;
98
use SimpleSAML\SAML2\Exception\ProtocolViolationException;
109

@@ -14,25 +13,21 @@
1413
trait RelayStateTrait
1514
{
1615
/**
17-
* @param string $value
18-
* @param string $message
1916
*/
2017
protected static function validRelayState(string $value, string $message = ''): void
2118
{
2219
parent::notWhitespaceOnly($value, $message); // Not protocol-defined, but makes zero sense
23-
try {
24-
/**
25-
* 3.4.3 RelayState
26-
*
27-
* The value MUST NOT exceed 80 bytes in length [..]
28-
*/
29-
parent::maxLength(
30-
$value,
31-
C::MAX_RELAY_STATE_LENGTH,
32-
$message ?: '%s is not a SAML2.0-compliant RelayState',
33-
);
34-
} catch (AssertionFailedException $e) {
35-
throw new ProtocolViolationException($e->getMessage());
36-
}
20+
21+
/**
22+
* 3.4.3 RelayState
23+
*
24+
* The value MUST NOT exceed 80 bytes in length [..]
25+
*/
26+
parent::maxLength(
27+
$value,
28+
C::MAX_RELAY_STATE_LENGTH,
29+
$message ?: '%s is not a SAML2.0-compliant RelayState',
30+
ProtocolViolationException::class,
31+
);
3732
}
3833
}

src/Assert/SAMLAnyURITrait.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace SimpleSAML\SAML2\Assert;
66

7-
use SimpleSAML\Assert\AssertionFailedException;
87
use SimpleSAML\SAML2\Exception\ProtocolViolationException;
98

109
/**
@@ -16,18 +15,17 @@ trait SAMLAnyURITrait
1615

1716

1817
/**
19-
* @param string $value
20-
* @param string $message
2118
*/
2219
protected static function validSAMLAnyURI(string $value, string $message = ''): void
2320
{
2421
parent::validAnyURI($value);
2522

26-
try {
27-
// If it doesn't have a scheme, it's not an absolute URI
28-
parent::regex($value, self::$scheme_regex, $message ?: '%s is not a SAML2-compliant URI');
29-
} catch (AssertionFailedException $e) {
30-
throw new ProtocolViolationException($e->getMessage());
31-
}
23+
// If it doesn't have a scheme, it's not an absolute URI
24+
parent::regex(
25+
$value,
26+
self::$scheme_regex,
27+
$message ?: '%s is not a SAML2-compliant URI',
28+
ProtocolViolationException::class,
29+
);
3230
}
3331
}

src/Assert/SAMLDateTimeTrait.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace SimpleSAML\SAML2\Assert;
66

7-
use SimpleSAML\Assert\AssertionFailedException;
87
use SimpleSAML\SAML2\Exception\ProtocolViolationException;
98

109
/**
@@ -13,21 +12,16 @@
1312
trait SAMLDateTimeTrait
1413
{
1514
/**
16-
* @param string $value
17-
* @param string $message
1815
*/
1916
protected static function validSAMLDateTime(string $value, string $message = ''): void
2017
{
2118
parent::validDateTime($value);
2219

23-
try {
24-
parent::endsWith(
25-
$value,
26-
'Z',
27-
'%s is not a DateTime expressed in the UTC timezone using the \'Z\' timezone identifier.',
28-
);
29-
} catch (AssertionFailedException $e) {
30-
throw new ProtocolViolationException($e->getMessage());
31-
}
20+
parent::endsWith(
21+
$value,
22+
'Z',
23+
'%s is not a DateTime expressed in the UTC timezone using the \'Z\' timezone identifier.',
24+
ProtocolViolationException::class,
25+
);
3226
}
3327
}

src/Assert/SAMLStringTrait.php

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace SimpleSAML\SAML2\Assert;
66

7-
use SimpleSAML\Assert\AssertionFailedException;
87
use SimpleSAML\SAML2\Exception\ProtocolViolationException;
98
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
109

@@ -14,22 +13,21 @@
1413
trait SAMLStringTrait
1514
{
1615
/**
17-
* @param string $value
18-
* @param string $message
1916
*/
2017
protected static function validSAMLString(string $value, string $message = ''): void
2118
{
2219
parent::validString($value, $message, SchemaViolationException::class);
23-
try {
24-
/**
25-
* 1.3.1 String Values
26-
*
27-
* Unless otherwise noted in this specification or particular profiles, all strings in
28-
* SAML messages MUST consist of at least one non-whitespace character
29-
*/
30-
parent::notWhitespaceOnly($value, $message ?: '%s is not a SAML2.0-compliant string');
31-
} catch (AssertionFailedException $e) {
32-
throw new ProtocolViolationException($e->getMessage());
33-
}
20+
21+
/**
22+
* 1.3.1 String Values
23+
*
24+
* Unless otherwise noted in this specification or particular profiles, all strings in
25+
* SAML messages MUST consist of at least one non-whitespace character
26+
*/
27+
parent::notWhitespaceOnly(
28+
$value,
29+
$message ?: '%s is not a SAML2.0-compliant string',
30+
ProtocolViolationException::class,
31+
);
3432
}
3533
}

0 commit comments

Comments
 (0)