Skip to content

Commit

Permalink
Bugfix: use self instead of static
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Sep 16, 2024
1 parent 66e3dd2 commit 737af6e
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 104 deletions.
30 changes: 15 additions & 15 deletions src/ExtendableAttributesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ public function getAttributesNS(): array
*/
protected static function getAttributesNSFromXML(DOMElement $xml, NS|array $namespace = null): array
{
$namespace = $namespace ?? static::XS_ANY_ATTR_NAMESPACE;
$exclusionList = static::getAttributeExclusions();
$namespace = $namespace ?? self::XS_ANY_ATTR_NAMESPACE;
$exclusionList = self::getAttributeExclusions();
$attributes = [];

// Validate namespace value
Expand All @@ -107,9 +107,9 @@ protected static function getAttributesNSFromXML(DOMElement $xml, NS|array $name
foreach ($xml->attributes as $a) {
if (in_array([$a->namespaceURI, $a->localName], $exclusionList, true)) {
continue;
} elseif ($namespace === NS::OTHER && in_array($a->namespaceURI, [static::NS, null], true)) {
} elseif ($namespace === NS::OTHER && in_array($a->namespaceURI, [self::NS, null], true)) {
continue;
} elseif ($namespace === NS::TARGET && $a->namespaceURI !== static::NS) {
} elseif ($namespace === NS::TARGET && $a->namespaceURI !== self::NS) {
continue;
} elseif ($namespace === NS::LOCAL && $a->namespaceURI !== null) {
continue;
Expand All @@ -126,7 +126,7 @@ protected static function getAttributesNSFromXML(DOMElement $xml, NS|array $name

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

// Replace the ##local with null
Expand Down Expand Up @@ -195,7 +195,7 @@ function (Attribute $attr) {

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

// Replace the ##local with null
Expand All @@ -209,7 +209,7 @@ function (Attribute $attr) {
sprintf(
'Attributes from namespaces [ %s ] are not allowed inside a %s element.',
rtrim(implode(', ', $diff)),
static::NS,
self::NS,
),
);
} else {
Expand All @@ -218,14 +218,14 @@ function (Attribute $attr) {
Assert::allNotNull($actual_namespaces);

// Must be any namespace other than the parent element
Assert::allNotSame($actual_namespaces, static::NS);
Assert::allNotSame($actual_namespaces, self::NS);
} elseif ($namespace === NS::TARGET) {
// Must be the same namespace as the one of the parent element
Assert::allSame($actual_namespaces, static::NS);
Assert::allSame($actual_namespaces, self::NS);
}
}

$exclusionList = static::getAttributeExclusions();
$exclusionList = self::getAttributeExclusions();
foreach ($attributes as $i => $attr) {
if (in_array([$attr->getNamespaceURI(), $attr->getAttrName()], $exclusionList, true)) {
unset($attributes[$i]);
Expand All @@ -243,13 +243,13 @@ function (Attribute $attr) {
public function getAttributeNamespace(): array|NS
{
Assert::true(
defined('static::XS_ANY_ATTR_NAMESPACE'),
self::getClassName(static::class)
defined('self::XS_ANY_ATTR_NAMESPACE'),
self::getClassName(self::class)
. '::XS_ANY_ATTR_NAMESPACE constant must be defined and set to the namespace for the xs:anyAttribute.',
RuntimeException::class,
);

return static::XS_ANY_ATTR_NAMESPACE;
return self::XS_ANY_ATTR_NAMESPACE;
}


Expand All @@ -260,8 +260,8 @@ public function getAttributeNamespace(): array|NS
*/
public static function getAttributeExclusions(): array
{
if (defined('static::XS_ANY_ATTR_EXCLUSIONS')) {
return static::XS_ANY_ATTR_EXCLUSIONS;
if (defined('self::XS_ANY_ATTR_EXCLUSIONS')) {
return self::XS_ANY_ATTR_EXCLUSIONS;
}

return [];
Expand Down
30 changes: 15 additions & 15 deletions src/ExtendableElementTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ trait ExtendableElementTrait
*/
protected static function getChildElementsFromXML(DOMElement $xml, NS|array $namespace = null): array
{
$namespace = $namespace ?? static::XS_ANY_ELT_NAMESPACE;
$exclusionList = static::getElementExclusions();
$namespace = $namespace ?? self::XS_ANY_ELT_NAMESPACE;
$exclusionList = self::getElementExclusions();
$registry = ElementRegistry::getInstance();
$elements = [];

Expand All @@ -59,9 +59,9 @@ protected static function getChildElementsFromXML(DOMElement $xml, NS|array $nam
continue;
} elseif (in_array([$elt->namespaceURI, $elt->localName], $exclusionList, true)) {
continue;
} elseif ($namespace === NS::OTHER && in_array($elt->namespaceURI, [static::NS, null], true)) {
} elseif ($namespace === NS::OTHER && in_array($elt->namespaceURI, [self::NS, null], true)) {
continue;
} elseif ($namespace === NS::TARGET && $elt->namespaceURI !== static::NS) {
} elseif ($namespace === NS::TARGET && $elt->namespaceURI !== self::NS) {
continue;
} elseif ($namespace === NS::LOCAL && $elt->namespaceURI !== null) {
continue;
Expand All @@ -79,7 +79,7 @@ protected static function getChildElementsFromXML(DOMElement $xml, NS|array $nam

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

// Replace the ##local with null
Expand Down Expand Up @@ -149,7 +149,7 @@ function (SerializableElementInterface $elt) {

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

// Replace the ##local with null
Expand All @@ -163,21 +163,21 @@ function (SerializableElementInterface $elt) {
sprintf(
'Elements from namespaces [ %s ] are not allowed inside a %s element.',
rtrim(implode(', ', $diff)),
static::NS,
self::NS,
),
);
} 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);
Assert::allNotSame($actual_namespaces, self::NS);
} elseif ($namespace === NS::TARGET) {
// Must be the same namespace as the one of the parent element
Assert::allSame($actual_namespaces, static::NS);
Assert::allSame($actual_namespaces, self::NS);
} else {
// XS_ANY_NS_ANY
}

$exclusionList = static::getElementExclusions();
$exclusionList = self::getElementExclusions();
foreach ($elements as $i => $elt) {
if (in_array([$elt->getNamespaceURI(), $elt->getLocalName()], $exclusionList, true)) {
unset($elements[$i]);
Expand Down Expand Up @@ -205,13 +205,13 @@ public function getElements(): array
public function getElementNamespace(): array|NS
{
Assert::true(
defined('static::XS_ANY_ELT_NAMESPACE'),
self::getClassName(static::class)
defined('self::XS_ANY_ELT_NAMESPACE'),
self::getClassName(self::class)
. '::XS_ANY_ELT_NAMESPACE constant must be defined and set to the namespace for the xs:any element.',
RuntimeException::class,
);

return static::XS_ANY_ELT_NAMESPACE;
return self::XS_ANY_ELT_NAMESPACE;
}


Expand All @@ -222,8 +222,8 @@ public function getElementNamespace(): array|NS
*/
public static function getElementExclusions(): array
{
if (defined('static::XS_ANY_ELT_EXCLUSIONS')) {
return static::XS_ANY_ELT_EXCLUSIONS;
if (defined('self::XS_ANY_ELT_EXCLUSIONS')) {
return self::XS_ANY_ELT_EXCLUSIONS;
}

return [];
Expand Down
27 changes: 2 additions & 25 deletions tests/Utils/ExtendableAttributesElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ class ExtendableAttributesElement extends AbstractElement
{
use ExtendableAttributesTrait;


/** @var string */
public const NS = 'urn:x-simplesamlphp:namespace';

Expand All @@ -31,36 +30,14 @@ class ExtendableAttributesElement extends AbstractElement
public const LOCALNAME = 'ExtendableAttributesElement';

/** @var string|\SimpleSAML\XML\XsNamespace */
public const XS_ANY_ATTR_NAMESPACE = NS::ANY;
final public const XS_ANY_ATTR_NAMESPACE = NS::ANY;

/** @var array{array{string, string}} */
public const XS_ANY_ATTR_EXCLUSIONS = [
final public const XS_ANY_ATTR_EXCLUSIONS = [
['urn:x-simplesamlphp:namespace', 'attr3'],
];


/**
* Get the namespace for the element.
*
* @return string
*/
public static function getNamespaceURI(): string
{
return static::NS;
}


/**
* Get the namespace-prefix for the element.
*
* @return string
*/
public static function getNamespacePrefix(): string
{
return static::NS_PREFIX;
}


/**
* Initialize element.
*
Expand Down
36 changes: 3 additions & 33 deletions tests/Utils/ExtendableElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class ExtendableElement extends AbstractElement
use ExtendableElementTrait;
use SerializableElementTrait;


/** @var string */
public const NS = 'urn:x-simplesamlphp:namespace';

Expand All @@ -32,36 +31,14 @@ class ExtendableElement extends AbstractElement
public const LOCALNAME = 'ExtendableElement';

/** @var \SimpleSAML\XML\XsNamespace|array<int, \SimpleSAML\XML\XsNamespace> */
public const XS_ANY_ELT_NAMESPACE = NS::ANY;
final public const XS_ANY_ELT_NAMESPACE = NS::ANY;

/** @var array{array{string, string}} */
public const XS_ANY_ELT_EXCLUSIONS = [
final public const XS_ANY_ELT_EXCLUSIONS = [
['urn:custom:other', 'Chunk'],
];


/**
* Get the namespace for the element.
*
* @return string
*/
public static function getNamespaceURI(): string
{
return static::NS;
}


/**
* Get the namespace-prefix for the element.
*
* @return string
*/
public static function getNamespacePrefix(): string
{
return static::NS_PREFIX;
}


/**
* Initialize element.
*
Expand All @@ -81,14 +58,7 @@ final public function __construct(array $elements)
*/
public static function fromXML(DOMElement $xml): static
{
$children = [];
foreach ($xml->childNodes as $node) {
if ($node instanceof DOMElement) {
$children[] = Chunk::fromXML($node);
}
}

return new static($children);
return new static(self::getChildElementsFromXML($xml));
}


Expand Down
16 changes: 0 additions & 16 deletions tests/XML/ExtendableAttributesTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,22 +222,6 @@ public function getAttributeNamespace(): array|NS
}


/**
*/
public function testLocalNamespacePassingTargetThrowsAnException(): void
{
$this->expectException(AssertionFailedException::class);
new class ([self::$target]) extends ExtendableAttributesElement {
/** @return array<int, NS>|NS */
public function getAtributeNamespace(): array|NS
{
return NS::LOCAL;
}
};
}



/**
*/
public function testLocalNamespacePassingOtherThrowsAnException(): void
Expand Down

0 comments on commit 737af6e

Please sign in to comment.