Skip to content

Commit

Permalink
Start using PHP 8.1 features
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Dec 13, 2023
1 parent b7d1341 commit 5e995f8
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 166 deletions.
27 changes: 1 addition & 26 deletions src/Constants.php
Expand Up @@ -27,32 +27,7 @@ class Constants
public const NS_XSI = 'http://www.w3.org/2001/XMLSchema-instance';

/**
* The namespace-attribute values for xs:any elements
* The maximum amount of child nodes this library is willing to handle.
*/
public const XS_ANY_NS_ANY = '##any';
public const XS_ANY_NS_LOCAL = '##local';
public const XS_ANY_NS_OTHER = '##other';
public const XS_ANY_NS_TARGET = '##targetNamespace';

public const XS_ANY_NS = [
self::XS_ANY_NS_ANY,
self::XS_ANY_NS_LOCAL,
self::XS_ANY_NS_OTHER,
self::XS_ANY_NS_TARGET,
];

/**
* The processContents-attribute values for xs:any elements
*/
public const XS_ANY_PROCESS_LAX = 'lax';
public const XS_ANY_PROCESS_SKIP = 'skip';
public const XS_ANY_PROCESS_STRICT = 'strict';

public const XS_ANY_PROCESS = [
self::XS_ANY_PROCESS_LAX,
self::XS_ANY_PROCESS_SKIP,
self::XS_ANY_PROCESS_STRICT,
];

public const UNBOUNDED_LIMIT = 10000;
}
40 changes: 20 additions & 20 deletions src/ExtendableAttributesTrait.php
Expand Up @@ -9,6 +9,7 @@
use SimpleSAML\Assert\Assert;
use SimpleSAML\XML\Attribute;
use SimpleSAML\XML\Constants as C;
use SimpleSAML\XML\XsNamespace as NS;

use function array_diff;
use function array_map;
Expand Down Expand Up @@ -100,25 +101,25 @@ protected static function getAttributesNSFromXML(DOMElement $xml, string|array $
// Validate namespace value
if (!is_array($namespace)) {
// Must be one of the predefined values
Assert::oneOf($namespace, C::XS_ANY_NS);
Assert::oneOf($namespace, NS::cases());

if ($namespace === C::XS_ANY_NS_ANY) {
if ($namespace === NS::ANY) {
foreach ($xml->attributes as $a) {
$attributes[] = new Attribute($a->namespaceURI, $a->prefix, $a->localName, $a->nodeValue);
}
} elseif ($namespace === C::XS_ANY_NS_LOCAL) {
} elseif ($namespace === NS::LOCAL) {
foreach ($xml->attributes as $a) {
if ($a->namespaceURI === null) {
$attributes[] = new Attribute($a->namespaceURI, $a->prefix, $a->localName, $a->nodeValue);
}
}
} elseif ($namespace === C::XS_ANY_NS_OTHER) {
} elseif ($namespace === NS::OTHER) {
foreach ($xml->attributes as $a) {
if (!in_array($a->namespaceURI, [static::NS, null], true)) {
$attributes[] = new Attribute($a->namespaceURI, $a->prefix, $a->localName, $a->nodeValue);
}
}
} elseif ($namespace === C::XS_ANY_NS_TARGET) {
} elseif ($namespace === NS::TARGET) {
foreach ($xml->attributes as $a) {
if ($a->namespaceURI === static::NS) {
$attributes[] = new Attribute($a->namespaceURI, $a->prefix, $a->localName, $a->nodeValue);
Expand All @@ -129,16 +130,16 @@ protected static function getAttributesNSFromXML(DOMElement $xml, string|array $
// Array must be non-empty and cannot contain ##any or ##other
Assert::notEmpty($namespace);
Assert::allStringNotEmpty($namespace);
Assert::allNotSame($namespace, C::XS_ANY_NS_ANY);
Assert::allNotSame($namespace, C::XS_ANY_NS_OTHER);
Assert::allNotSame($namespace, NS::ANY);
Assert::allNotSame($namespace, NS::OTHER);

// Replace the ##targetedNamespace with the actual namespace
if (($key = array_search(C::XS_ANY_NS_TARGET, $namespace)) !== false) {
if (($key = array_search(NS::TARGET, $namespace)) !== false) {
$namespace[$key] = static::NS;
}

// Replace the ##local with null
if (($key = array_search(C::XS_ANY_NS_LOCAL, $namespace)) !== false) {
if (($key = array_search(NS::LOCAL, $namespace)) !== false) {
$namespace[$key] = null;
}

Expand Down Expand Up @@ -170,13 +171,12 @@ protected function setAttributesNS(array $attributes): void
// Validate namespace value
if (!is_array($namespace)) {
// Must be one of the predefined values
Assert::oneOf($namespace, C::XS_ANY_NS);
Assert::oneOf($namespace, NS::cases());
} else {
// Array must be non-empty and cannot contain ##any or ##other
Assert::notEmpty($namespace);
Assert::allStringNotEmpty($namespace);
Assert::allNotSame($namespace, C::XS_ANY_NS_ANY);
Assert::allNotSame($namespace, C::XS_ANY_NS_OTHER);
Assert::allNotSame($namespace, NS::ANY);
Assert::allNotSame($namespace, NS::OTHER);
}

// Get namespaces for all attributes
Expand All @@ -191,20 +191,20 @@ function (Attribute $attr) {
$attributes,
);

if ($namespace === C::XS_ANY_NS_LOCAL) {
if ($namespace === NS::LOCAL) {
// If ##local then all namespaces must be null
Assert::allNull($actual_namespaces);
} elseif (is_array($namespace)) {
// Make a local copy of the property that we can edit
$allowed_namespaces = $namespace;

// Replace the ##targetedNamespace with the actual namespace
if (($key = array_search(C::XS_ANY_NS_TARGET, $allowed_namespaces)) !== false) {
if (($key = array_search(NS::TARGET, $allowed_namespaces)) !== false) {
$allowed_namespaces[$key] = static::NS;
}

// Replace the ##local with null
if (($key = array_search(C::XS_ANY_NS_LOCAL, $allowed_namespaces)) !== false) {
if (($key = array_search(NS::LOCAL, $allowed_namespaces)) !== false) {
$allowed_namespaces[$key] = null;
}

Expand All @@ -221,10 +221,10 @@ function (Attribute $attr) {
// All attributes must be namespaced, ergo non-null
Assert::allNotNull($actual_namespaces);

if ($namespace === C::XS_ANY_NS_OTHER) {
if ($namespace === NS::OTHER) {
// Must be any namespace other than the parent element
Assert::allNotSame($actual_namespaces, static::NS);
} elseif ($namespace === C::XS_ANY_NS_TARGET) {
} elseif ($namespace === NS::TARGET) {
// Must be the same namespace as the one of the parent element
Assert::allSame($actual_namespaces, static::NS);
}
Expand All @@ -236,9 +236,9 @@ function (Attribute $attr) {


/**
* @return array|string
* @return array|\SimpleSAML\XML\XsNamespace
*/
public function getAttributeNamespace(): array|string
public function getAttributeNamespace(): array|NS
{
Assert::true(
defined('static::XS_ANY_ATTR_NAMESPACE'),
Expand Down
21 changes: 11 additions & 10 deletions src/ExtendableElementTrait.php
Expand Up @@ -9,6 +9,7 @@
use SimpleSAML\Assert\Assert;
use SimpleSAML\XML\Chunk;
use SimpleSAML\XML\Constants as C;
use SimpleSAML\XML\XsNamespace as NS;

use function array_diff;
use function array_map;
Expand Down Expand Up @@ -45,12 +46,12 @@ protected function setElements(array $elements): void
// Validate namespace value
if (!is_array($namespace)) {
// Must be one of the predefined values
Assert::oneOf($namespace, C::XS_ANY_NS);
Assert::oneOf($namespace, NS::cases());
} else {
// Array must be non-empty and cannot contain ##any or ##other
Assert::notEmpty($namespace);
Assert::allNotSame($namespace, C::XS_ANY_NS_ANY);
Assert::allNotSame($namespace, C::XS_ANY_NS_OTHER);
Assert::allNotSame($namespace, NS::ANY);
Assert::allNotSame($namespace, NS::OTHER);
}

// Get namespaces for all elements
Expand All @@ -66,20 +67,20 @@ function (ElementInterface $elt) {
$elements
);

if ($namespace === C::XS_ANY_NS_LOCAL) {
if ($namespace === NS::LOCAL) {
// If ##local then all namespaces must be null
Assert::allNull($actual_namespaces);
} elseif (is_array($namespace)) {
// Make a local copy of the property that we can edit
$allowed_namespaces = $namespace;

// Replace the ##targetedNamespace with the actual namespace
if (($key = array_search(C::XS_ANY_NS_TARGET, $allowed_namespaces)) !== false) {
if (($key = array_search(NS::TARGET, $allowed_namespaces)) !== false) {
$allowed_namespaces[$key] = static::NS;
}

// Replace the ##local with null
if (($key = array_search(C::XS_ANY_NS_LOCAL, $allowed_namespaces)) !== false) {
if (($key = array_search(NS::LOCAL, $allowed_namespaces)) !== false) {
$allowed_namespaces[$key] = null;
}

Expand All @@ -92,11 +93,11 @@ function (ElementInterface $elt) {
static::NS,
),
);
} elseif ($namespace === C::XS_ANY_NS_OTHER) {
} elseif ($namespace === NS::OTHER) {
// Must be any namespace other than the parent element, excluding elements with no namespace
Assert::notInArray(null, $actual_namespaces);
Assert::allNotSame($actual_namespaces, static::NS);
} elseif ($namespace === C::XS_ANY_NS_TARGET) {
} elseif ($namespace === NS::TARGET) {
// Must be the same namespace as the one of the parent element
Assert::allSame($actual_namespaces, static::NS);
} else {
Expand All @@ -119,9 +120,9 @@ public function getElements(): array


/**
* @return array|string
* @return array|\SimpleSAML\XML\XsNamespace
*/
public function getElementNamespace(): array|string
public function getElementNamespace(): array|NS
{
Assert::true(
defined('static::XS_ANY_ELT_NAMESPACE'),
Expand Down
18 changes: 18 additions & 0 deletions src/XsNamespace.php
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\XML;

/**
* The namespace-attribute values for xs:any elements
*
* @package simplesamlphp/xml-common
*/
enum XsNamespace: string
{
case ANY = '##any';
case LOCAL = '##local';
case OTHER = '##other';
case TARGET = '##targetNamespace';
}
17 changes: 17 additions & 0 deletions src/XsProcess.php
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\XML;

/**
* The processContents-attribute values for xs:any elements
*
* @package simplesamlphp/xml-common
*/
enum XsProcess: string
{
case LAX = 'lax';
case SKIP = 'skip';
case STRICT = 'strict';
}
4 changes: 2 additions & 2 deletions tests/Utils/ExtendableAttributesElement.php
Expand Up @@ -8,7 +8,7 @@
use SimpleSAML\Assert\Assert;
use SimpleSAML\Assert\AssertionFailedException;
use SimpleSAML\XML\AbstractElement;
use SimpleSAML\XML\Constants as C;
use SimpleSAML\XML\XsNamespace as NS;
use SimpleSAML\XML\ExtendableAttributesTrait;
use SimpleSAML\XML\Exception\InvalidDOMElementException;

Expand All @@ -32,7 +32,7 @@ class ExtendableAttributesElement extends AbstractElement
public const LOCALNAME = 'ExtendableAttributesElement';

/** @var string|array */
public const XS_ANY_ATTR_NAMESPACE = C::XS_ANY_NS_ANY;
public const XS_ANY_ATTR_NAMESPACE = NS::ANY;


/**
Expand Down
6 changes: 3 additions & 3 deletions tests/Utils/ExtendableElement.php
Expand Up @@ -8,7 +8,7 @@
use SimpleSAML\Assert\Assert;
use SimpleSAML\XML\AbstractElement;
use SimpleSAML\XML\Chunk;
use SimpleSAML\XML\Constants as C;
use SimpleSAML\XML\XsNamespace as NS;
use SimpleSAML\XML\ExtendableElementTrait;
use SimpleSAML\XML\Exception\InvalidDOMElementException;
use SimpleSAML\XML\SerializableElementTrait;
Expand All @@ -33,8 +33,8 @@ class ExtendableElement extends AbstractElement
/** @var string */
public const LOCALNAME = 'ExtendableElement';

/** @var string|array */
public const XS_ANY_ELT_NAMESPACE = C::XS_ANY_NS_ANY;
/** @var \SimpleSAML\XML\XsNamespace|array */
public const XS_ANY_ELT_NAMESPACE = NS::ANY;


/**
Expand Down
2 changes: 1 addition & 1 deletion tests/XML/ExtendableAttributesTest.php
Expand Up @@ -6,10 +6,10 @@

use PHPUnit\Framework\TestCase;
use SimpleSAML\Test\XML\ExtendableAttributesElement;
use SimpleSAML\XML\Attribute;
use SimpleSAML\XML\DOMDocumentFactory;
use SimpleSAML\XML\TestUtils\SchemaValidationTestTrait;
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;
use SimpleSAML\XML\Attribute;

use function dirname;

Expand Down

0 comments on commit 5e995f8

Please sign in to comment.