Skip to content

Commit 4aead0a

Browse files
committed
Refactor assertions
1 parent 8735c79 commit 4aead0a

28 files changed

+60
-233
lines changed

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@
3131
"psr/clock": "^1.0",
3232
"psr/http-message": "^2.0",
3333
"psr/log": "^2.0 || ^3.0",
34-
"simplesamlphp/assert": "~1.7.0",
35-
"simplesamlphp/xml-common": "~1.23.0",
36-
"simplesamlphp/xml-security": "~1.12.0",
37-
"simplesamlphp/xml-soap": "~1.6.0"
34+
"simplesamlphp/assert": "~1.8.0",
35+
"simplesamlphp/xml-common": "~1.24.0",
36+
"simplesamlphp/xml-security": "~1.13.0",
37+
"simplesamlphp/xml-soap": "~1.7.0"
3838
},
3939
"require-dev": {
4040
"ext-intl": "*",

src/Assert/Assert.php

Lines changed: 2 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,7 @@
44

55
namespace SimpleSAML\SAML2\Assert;
66

7-
use BadMethodCallException; // Requires ext-spl
8-
use DateTime; // requires ext-date
9-
use DateTimeImmutable; // requires ext-date
10-
use InvalidArgumentException; // Requires ext-spl
11-
use SimpleSAML\Assert\Assert as BaseAssert;
12-
use SimpleSAML\Assert\AssertionFailedException;
13-
use Throwable;
14-
15-
use function array_pop;
16-
use function array_unshift;
17-
use function call_user_func_array;
18-
use function end;
19-
use function enum_exists;
20-
use function function_exists;
21-
use function get_class;
22-
use function is_object;
23-
use function is_resource;
24-
use function is_string;
25-
use function is_subclass_of;
26-
use function lcfirst;
27-
use function method_exists;
28-
use function preg_match; // Requires ext-pcre
29-
use function strval;
7+
use SimpleSAML\XMLSecurity\Assert\Assert as BaseAssert;
308

319
/**
3210
* SimpleSAML\SAML2\Assert\Assert wrapper class
@@ -43,136 +21,7 @@
4321
* @method static void allValidURI(mixed $value, string $message = '', string $exception = '')
4422
* @method static void allValidEntityID(mixed $value, string $message = '', string $exception = '')
4523
*/
46-
final class Assert
24+
class Assert extends BaseAssert
4725
{
4826
use CustomAssertionTrait;
49-
50-
51-
/**
52-
* @param string $name
53-
* @param array<mixed> $arguments
54-
*/
55-
public static function __callStatic(string $name, array $arguments): void
56-
{
57-
// Handle Exception-parameter
58-
$exception = AssertionFailedException::class;
59-
60-
$last = end($arguments);
61-
if (is_string($last) && class_exists($last) && is_subclass_of($last, Throwable::class)) {
62-
$exception = $last;
63-
array_pop($arguments);
64-
}
65-
66-
try {
67-
if (method_exists(static::class, $name)) {
68-
call_user_func_array([static::class, $name], $arguments);
69-
return;
70-
} elseif (preg_match('/^nullOr(.*)$/i', $name, $matches)) {
71-
$method = lcfirst($matches[1]);
72-
if (method_exists(static::class, $method)) {
73-
call_user_func_array([static::class, 'nullOr'], [[static::class, $method], $arguments]);
74-
} elseif (method_exists(BaseAssert::class, $method)) {
75-
call_user_func_array([static::class, 'nullOr'], [[BaseAssert::class, $method], $arguments]);
76-
} else {
77-
throw new BadMethodCallException(sprintf("Assertion named `%s` does not exists.", $method));
78-
}
79-
} elseif (preg_match('/^all(.*)$/i', $name, $matches)) {
80-
$method = lcfirst($matches[1]);
81-
if (method_exists(static::class, $method)) {
82-
call_user_func_array([static::class, 'all'], [[static::class, $method], $arguments]);
83-
} elseif (method_exists(BaseAssert::class, $method)) {
84-
call_user_func_array([static::class, 'all'], [[BaseAssert::class, $method], $arguments]);
85-
} else {
86-
throw new BadMethodCallException(sprintf("Assertion named `%s` does not exists.", $method));
87-
}
88-
} else {
89-
throw new BadMethodCallException(sprintf("Assertion named `%s` does not exists.", $name));
90-
}
91-
} catch (InvalidArgumentException $e) {
92-
throw new $exception($e->getMessage());
93-
}
94-
}
95-
96-
97-
/**
98-
* Handle nullOr* for either Webmozart or for our custom assertions
99-
*
100-
* @param callable $method
101-
* @param array<mixed> $arguments
102-
* @return void
103-
*/
104-
private static function nullOr(callable $method, array $arguments): void
105-
{
106-
$value = reset($arguments);
107-
($value === null) || call_user_func_array($method, $arguments);
108-
}
109-
110-
111-
/**
112-
* all* for our custom assertions
113-
*
114-
* @param callable $method
115-
* @param array<mixed> $arguments
116-
* @return void
117-
*/
118-
private static function all(callable $method, array $arguments): void
119-
{
120-
$values = array_pop($arguments);
121-
foreach ($values as $value) {
122-
$tmp = $arguments;
123-
array_unshift($tmp, $value);
124-
call_user_func_array($method, $tmp);
125-
}
126-
}
127-
128-
129-
/**
130-
* @param mixed $value
131-
*
132-
* @return string
133-
*/
134-
protected static function valueToString(mixed $value): string
135-
{
136-
if (is_resource($value)) {
137-
return 'resource';
138-
}
139-
140-
if (null === $value) {
141-
return 'null';
142-
}
143-
144-
if (true === $value) {
145-
return 'true';
146-
}
147-
148-
if (false === $value) {
149-
return 'false';
150-
}
151-
152-
if (is_array($value)) {
153-
return 'array';
154-
}
155-
156-
if (is_object($value)) {
157-
if (method_exists($value, '__toString')) {
158-
return $value::class . ': ' . self::valueToString($value->__toString());
159-
}
160-
161-
if ($value instanceof DateTime || $value instanceof DateTimeImmutable) {
162-
return $value::class . ': ' . self::valueToString($value->format('c'));
163-
}
164-
165-
if (function_exists('enum_exists') && enum_exists(get_class($value))) {
166-
return get_class($value) . '::' . $value->name;
167-
}
168-
169-
return $value::class;
170-
}
171-
172-
if (is_string($value)) {
173-
return '"' . $value . '"';
174-
}
175-
176-
return strval($value);
177-
}
17827
}

src/Assert/CustomAssertionTrait.php

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

55
namespace SimpleSAML\SAML2\Assert;
66

7-
use SimpleSAML\Assert\Assert as BaseAssert;
87
use SimpleSAML\Assert\AssertionFailedException;
98
use SimpleSAML\SAML2\Constants as C;
109
use SimpleSAML\SAML2\Exception\ProtocolViolationException;
@@ -30,16 +29,12 @@ trait CustomAssertionTrait
3029
* @param string $value
3130
* @param string $message
3231
*/
33-
private static function validDateTime(string $value, string $message = ''): void
32+
protected static function validDateTime(string $value, string $message = ''): void
3433
{
35-
try {
36-
BaseAssert::validDateTime($value);
37-
} catch (AssertionFailedException $e) {
38-
throw new SchemaViolationException($e->getMessage());
39-
}
34+
parent::validDateTime($value);
4035

4136
try {
42-
BaseAssert::endsWith(
37+
parent::endsWith(
4338
$value,
4439
'Z',
4540
'%s is not a DateTime expressed in the UTC timezone using the \'Z\' timezone identifier.',
@@ -54,17 +49,13 @@ private static function validDateTime(string $value, string $message = ''): void
5449
* @param string $value
5550
* @param string $message
5651
*/
57-
private static function validURI(string $value, string $message = ''): void
52+
protected static function validURI(string $value, string $message = ''): void
5853
{
59-
try {
60-
BaseAssert::validURI($value);
61-
} catch (AssertionFailedException $e) {
62-
throw new SchemaViolationException($e->getMessage());
63-
}
54+
parent::validURI($value);
6455

6556
try {
6657
// If it doesn't have a scheme, it's not an absolute URI
67-
BaseAssert::regex($value, self::$scheme_regex, $message ?: '%s is not a SAML2-compliant URI');
58+
parent::regex($value, self::$scheme_regex, $message ?: '%s is not a SAML2-compliant URI');
6859
} catch (AssertionFailedException $e) {
6960
throw new ProtocolViolationException($e->getMessage());
7061
}
@@ -75,13 +66,13 @@ private static function validURI(string $value, string $message = ''): void
7566
* @param string $value
7667
* @param string $message
7768
*/
78-
private static function validEntityID(string $value, string $message = ''): void
69+
protected static function validEntityID(string $value, string $message = ''): void
7970
{
8071
static::validURI($value);
8172

8273
try {
83-
BaseAssert::notWhitespaceOnly($value);
84-
BaseAssert::maxLength(
74+
parent::notWhitespaceOnly($value);
75+
parent::maxLength(
8576
$value,
8677
C::ENTITYID_MAX_LENGTH,
8778
sprintf('An entityID cannot be longer than %d characters.', C::ENTITYID_MAX_LENGTH),

src/XML/ExtensionPointTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace SimpleSAML\SAML2\XML;
66

77
use RuntimeException;
8-
use SimpleSAML\Assert\Assert;
8+
use SimpleSAML\SAML2\Assert\Assert;
99
use SimpleSAML\XML\Exception\SchemaViolationException;
1010

1111
/**

src/XML/md/AbstractMetadataDocument.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use DateTimeImmutable;
88
use DOMElement;
9-
use SimpleSAML\Assert\Assert;
9+
use SimpleSAML\SAML2\Assert\Assert;
1010
use SimpleSAML\SAML2\Constants as C;
1111
use SimpleSAML\SAML2\XML\ExtendableElementTrait;
1212
use SimpleSAML\XML\Exception\SchemaViolationException;

src/XML/md/AbstractRoleDescriptor.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66

77
use DateTimeImmutable;
88
use DOMElement;
9-
use SimpleSAML\Assert\Assert;
10-
use SimpleSAML\SAML2\Assert\Assert as SAMLAssert;
9+
use SimpleSAML\SAML2\Assert\Assert;
1110
use SimpleSAML\SAML2\Constants as C;
1211
use SimpleSAML\SAML2\Utils;
1312
use SimpleSAML\SAML2\XML\ExtensionPointInterface;
@@ -136,7 +135,7 @@ public static function fromXML(DOMElement $xml): static
136135
$protocols = self::getAttribute($xml, 'protocolSupportEnumeration');
137136

138137
$validUntil = self::getOptionalAttribute($xml, 'validUntil', null);
139-
SAMLAssert::nullOrValidDateTime($validUntil);
138+
Assert::nullOrValidDateTime($validUntil);
140139

141140
$orgs = Organization::getChildrenOfClass($xml);
142141
Assert::maxCount(

src/XML/saml/AbstractBaseID.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace SimpleSAML\SAML2\XML\saml;
66

77
use DOMElement;
8-
use SimpleSAML\Assert\Assert;
8+
use SimpleSAML\SAML2\Assert\Assert;
99
use SimpleSAML\SAML2\Constants as C;
1010
use SimpleSAML\SAML2\Utils;
1111
use SimpleSAML\SAML2\XML\EncryptableElementTrait;

src/XML/saml/AbstractCondition.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace SimpleSAML\SAML2\XML\saml;
66

77
use DOMElement;
8-
use SimpleSAML\Assert\Assert;
8+
use SimpleSAML\SAML2\Assert\Assert;
99
use SimpleSAML\SAML2\Constants as C;
1010
use SimpleSAML\SAML2\Utils;
1111
use SimpleSAML\SAML2\XML\ExtensionPointInterface;

src/XML/saml/AbstractStatement.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace SimpleSAML\SAML2\XML\saml;
66

77
use DOMElement;
8-
use SimpleSAML\Assert\Assert;
8+
use SimpleSAML\SAML2\Assert\Assert;
99
use SimpleSAML\SAML2\Constants as C;
1010
use SimpleSAML\SAML2\Utils;
1111
use SimpleSAML\SAML2\XML\ExtensionPointInterface;

src/XML/saml/Assertion.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66

77
use DateTimeImmutable;
88
use DOMElement;
9-
use SimpleSAML\Assert\Assert;
10-
use SimpleSAML\SAML2\Assert\Assert as SAMLAssert;
9+
use SimpleSAML\SAML2\Assert\Assert;
1110
use SimpleSAML\SAML2\Constants as C;
1211
use SimpleSAML\SAML2\Exception\ProtocolViolationException;
1312
use SimpleSAML\SAML2\Utils\XPath;
@@ -262,7 +261,7 @@ public static function fromXML(DOMElement $xml): static
262261
// Strip sub-seconds - See paragraph 1.3.3 of SAML core specifications
263262
$issueInstant = preg_replace('/([.][0-9]+Z)$/', 'Z', $issueInstant, 1);
264263

265-
SAMLAssert::validDateTime($issueInstant, ProtocolViolationException::class);
264+
Assert::validDateTime($issueInstant, ProtocolViolationException::class);
266265
$issueInstant = new DateTimeImmutable($issueInstant);
267266

268267
$issuer = Issuer::getChildrenOfClass($xml);

0 commit comments

Comments
 (0)