Skip to content

Commit 380601a

Browse files
committed
Refactor assertions
1 parent 886e2a5 commit 380601a

19 files changed

+34
-196
lines changed

composer.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717

1818
"psr/clock": "^1.0",
1919
"psr/log": "^2.0 | ^3.0",
20-
"simplesamlphp/assert": "^1.6",
21-
"simplesamlphp/xml-common": "~1.23.0",
22-
"simplesamlphp/xml-security": "~1.12.0"
20+
"simplesamlphp/assert": "^1.8",
21+
"simplesamlphp/xml-common": "~1.24.0",
22+
"simplesamlphp/xml-security": "~1.13.0"
2323
},
2424
"require-dev": {
2525
"beste/clock": "^3.0",

src/SAML11/Assert/Assert.php

+2-153
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,7 @@
44

55
namespace SimpleSAML\SAML11\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\XML\Assert\Assert as BaseAssert;
308

319
/**
3210
* SimpleSAML\SAML11\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/SAML11/Assert/CustomAssertionTrait.php

+8-17
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace SimpleSAML\SAML11\Assert;
66

7-
use SimpleSAML\Assert\Assert as BaseAssert;
87
use SimpleSAML\Assert\AssertionFailedException;
98
use SimpleSAML\SAML11\Exception\ProtocolViolationException;
109
use SimpleSAML\XML\Exception\SchemaViolationException;
@@ -18,7 +17,7 @@ trait CustomAssertionTrait
1817

1918
/***********************************************************************************
2019
* NOTE: Custom assertions may be added below this line. *
21-
* They SHOULD be marked as `private` to ensure the call is forced *
20+
* They SHOULD be marked as `protected` to ensure the call is forced *
2221
* through __callStatic(). *
2322
* Assertions marked `public` are called directly and will *
2423
* not handle any custom exception passed to it. *
@@ -29,16 +28,12 @@ trait CustomAssertionTrait
2928
* @param string $value
3029
* @param string $message
3130
*/
32-
private static function validDateTime(string $value, string $message = ''): void
31+
protected static function validDateTime(string $value, string $message = ''): void
3332
{
34-
try {
35-
BaseAssert::validDateTime($value, $message);
36-
} catch (AssertionFailedException $e) {
37-
throw new SchemaViolationException($e->getMessage());
38-
}
33+
parent::validDateTime($value, $message, SchemaViolationException::class);
3934

4035
try {
41-
BaseAssert::endsWith(
36+
static::endsWith(
4237
$value,
4338
'Z',
4439
$message ?: '%s is not a DateTime expressed in the UTC timezone using the \'Z\' timezone identifier.',
@@ -53,18 +48,14 @@ private static function validDateTime(string $value, string $message = ''): void
5348
* @param string $value
5449
* @param string $message
5550
*/
56-
private static function validURI(string $value, string $message = ''): void
51+
protected static function validURI(string $value, string $message = ''): void
5752
{
58-
try {
59-
BaseAssert::validURI($value, $message);
60-
} catch (AssertionFailedException $e) {
61-
throw new SchemaViolationException($e->getMessage());
62-
}
53+
parent::validURI($value, $message, SchemaViolationException::class);
6354

6455
try {
65-
BaseAssert::notWhitespaceOnly($value, $message ?: '%s is not a SAML1.1-compliant URI');
56+
static::notWhitespaceOnly($value, $message ?: '%s is not a SAML1.1-compliant URI');
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 SAML1.1-compliant URI');
58+
static::regex($value, self::$scheme_regex, $message ?: '%s is not a SAML1.1-compliant URI');
6859
} catch (AssertionFailedException $e) {
6960
throw new ProtocolViolationException($e->getMessage());
7061
}

src/SAML11/Compat/AbstractContainer.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use Psr\Clock\ClockInterface;
88
use Psr\Log\LoggerInterface;
9-
use SimpleSAML\Assert\Assert;
9+
use SimpleSAML\SAML11\Assert\Assert;
1010
use SimpleSAML\SAML11\XML\ExtensionPointInterface;
1111
use SimpleSAML\XML\AbstractElement;
1212
use SimpleSAML\XML\Exception\SchemaViolationException;

src/SAML11/XML/ExtensionPointTrait.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
namespace SimpleSAML\SAML11\XML;
66

77
use RuntimeException;
8-
use SimpleSAML\Assert\Assert;
9-
use SimpleSAML\SAML11\Assert\Assert as SAMLAssert;
8+
use SimpleSAML\SAML11\Assert\Assert;
109
use SimpleSAML\XML\Exception\SchemaViolationException;
1110

1211
/**
@@ -58,7 +57,7 @@ public static function getXsiTypeNamespaceURI(): string
5857
RuntimeException::class,
5958
);
6059

61-
SAMLAssert::validURI(static::XSI_TYPE_NAMESPACE, SchemaViolationException::class);
60+
Assert::validURI(static::XSI_TYPE_NAMESPACE, SchemaViolationException::class);
6261
return static::XSI_TYPE_NAMESPACE;
6362
}
6463

src/SAML11/XML/saml/AbstractAssertionType.php

+2-3
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\SAML11\Assert\Assert as SAMLAssert;
9+
use SimpleSAML\SAML11\Assert\Assert;
1110
use SimpleSAML\SAML11\Compat\ContainerSingleton;
1211
use SimpleSAML\SAML11\Constants as C;
1312
use SimpleSAML\SAML11\Exception\ProtocolViolationException;
@@ -249,7 +248,7 @@ public static function fromXML(DOMElement $xml): static
249248
// Strip sub-seconds - See paragraph 1.2.2 of SAML core specifications
250249
$issueInstant = preg_replace('/([.][0-9]+Z)$/', 'Z', $issueInstant, 1);
251250

252-
SAMLAssert::validDateTime($issueInstant, ProtocolViolationException::class);
251+
Assert::validDateTime($issueInstant, ProtocolViolationException::class);
253252
$issueInstant = new DateTimeImmutable($issueInstant);
254253

255254
$conditions = Conditions::getChildrenOfClass($xml);

src/SAML11/XML/saml/AbstractAuthorityBindingType.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
namespace SimpleSAML\SAML11\XML\saml;
66

77
use DOMElement;
8-
use SimpleSAML\Assert\Assert;
9-
use SimpleSAML\SAML11\Assert\Assert as SAMLAssert;
8+
use SimpleSAML\SAML11\Assert\Assert;
109
use SimpleSAML\SAML11\Constants as C;
1110
use SimpleSAML\XML\Exception\InvalidDOMElementException;
1211

@@ -30,8 +29,8 @@ final public function __construct(
3029
protected string $Binding,
3130
) {
3231
Assert::validQName($AuthorityKind);
33-
SAMLAssert::validURI($Location);
34-
SAMLAssert::validURI($Binding);
32+
Assert::validURI($Location);
33+
Assert::validURI($Binding);
3534
}
3635

3736

src/SAML11/XML/saml/AbstractCondition.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace SimpleSAML\SAML11\XML\saml;
66

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

src/SAML11/XML/saml/AbstractStatement.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace SimpleSAML\SAML11\XML\saml;
66

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

src/SAML11/XML/saml/AbstractSubjectStatement.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace SimpleSAML\SAML11\XML\saml;
66

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

src/SAML11/XML/saml/AssertionIDReference.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace SimpleSAML\SAML11\XML\saml;
66

77
use DOMElement;
8-
use SimpleSAML\Assert\Assert;
8+
use SimpleSAML\SAML11\Assert\Assert;
99
use SimpleSAML\SAML11\XML\StringElementTrait;
1010
use SimpleSAML\XML\Exception\InvalidDOMElementException;
1111
use SimpleSAML\XML\Exception\SchemaViolationException;

src/SAML11/XML/saml/AttributeValue.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use DateTimeImmutable;
88
use DateTimeInterface;
99
use DOMElement;
10-
use SimpleSAML\Assert\Assert;
10+
use SimpleSAML\SAML11\Assert\Assert;
1111
use SimpleSAML\SAML11\Constants as C;
1212
use SimpleSAML\XML\AbstractElement;
1313
use SimpleSAML\XML\Chunk;

src/SAML11/XML/samlp/AbstractQuery.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace SimpleSAML\SAML11\XML\samlp;
66

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

src/SAML11/XML/samlp/AbstractRequestAbstractType.php

+1-1
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\SAML11\Assert\Assert;
1010
use SimpleSAML\SAML11\Utils\XPath;
1111
use SimpleSAML\XML\Exception\SchemaViolationException;
1212

src/SAML11/XML/samlp/AbstractResponseType.php

+1-1
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\SAML11\Assert\Assert;
1010
use SimpleSAML\SAML11\XML\saml\Assertion;
1111
use SimpleSAML\SAML11\XML\samlp\Status;
1212
use SimpleSAML\XML\Exception\SchemaViolationException;

src/SAML11/XML/samlp/AbstractStatusCodeType.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace SimpleSAML\SAML11\XML\samlp;
66

77
use DOMElement;
8-
use SimpleSAML\Assert\Assert;
8+
use SimpleSAML\SAML11\Assert\Assert;
99
use SimpleSAML\SAML11\Constants as C;
1010
use SimpleSAML\XML\Exception\InvalidDOMElementException;
1111

src/SAML11/XML/samlp/AbstractSubjectQuery.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace SimpleSAML\SAML11\XML\samlp;
66

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

0 commit comments

Comments
 (0)