Skip to content

Commit e1cdf19

Browse files
committed
Fix phpstan errors in MduiChunk and GsspUserAttributesChunk
Prior to this change, toXML() in both classes called saveXML() on a DOMDocument that phpstan correctly identifies as potentially null, and saveXML() itself returns string|false which does not satisfy the string return type. In MduiChunk::__construct(), documentElement is also typed as DOMElement|null. This change adds explicit null and false guards with RuntimeException, adds missing RuntimeException imports, and regenerates the phpstan baseline to remove stale suppressions that no longer match any reported errors. Verified locally against symfony/framework-bundle 6.4.* and 7.4.*.
1 parent d18b3d0 commit e1cdf19

3 files changed

Lines changed: 25 additions & 39 deletions

File tree

‎ci/qa/phpstan-baseline.neon‎

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -486,12 +486,6 @@ parameters:
486486
count: 1
487487
path: ../../src/SAML2/AuthnRequest.php
488488

489-
-
490-
message: '#^Method Surfnet\\SamlBundle\\SAML2\\AuthnRequest\:\:getAuthenticationContextClassRef\(\) should return string\|null but returns mixed\.$#'
491-
identifier: return.type
492-
count: 1
493-
path: ../../src/SAML2/AuthnRequest.php
494-
495489
-
496490
message: '#^Method Surfnet\\SamlBundle\\SAML2\\AuthnRequest\:\:getDestination\(\) should return string but returns string\|null\.$#'
497491
identifier: return.type
@@ -528,12 +522,6 @@ parameters:
528522
count: 1
529523
path: ../../src/SAML2/AuthnRequest.php
530524

531-
-
532-
message: '#^Parameter \#1 \$array of function reset expects array\|object, mixed given\.$#'
533-
identifier: argument.type
534-
count: 1
535-
path: ../../src/SAML2/AuthnRequest.php
536-
537525
-
538526
message: '#^Parameter \#1 \$string of function base64_encode expects string, string\|false given\.$#'
539527
identifier: argument.type
@@ -642,18 +630,6 @@ parameters:
642630
count: 1
643631
path: ../../src/SAML2/Extensions/Extensions.php
644632

645-
-
646-
message: '#^Cannot call method saveXML\(\) on DOMDocument\|null\.$#'
647-
identifier: method.nonObject
648-
count: 1
649-
path: ../../src/SAML2/Extensions/GsspUserAttributesChunk.php
650-
651-
-
652-
message: '#^Method Surfnet\\SamlBundle\\SAML2\\Extensions\\GsspUserAttributesChunk\:\:toXML\(\) has no return type specified\.$#'
653-
identifier: missingType.return
654-
count: 1
655-
path: ../../src/SAML2/Extensions/GsspUserAttributesChunk.php
656-
657633
-
658634
message: '#^Parameter \#1 \$element of method Surfnet\\SamlBundle\\SAML2\\Extensions\\Chunk\:\:append\(\) expects DOMElement, DOMElement\|null given\.$#'
659635
identifier: argument.type
@@ -678,12 +654,6 @@ parameters:
678654
count: 1
679655
path: ../../src/SAML2/ReceivedAuthnRequest.php
680656

681-
-
682-
message: '#^Method Surfnet\\SamlBundle\\SAML2\\ReceivedAuthnRequest\:\:getAuthenticationContextClassRef\(\) should return string\|null but returns mixed\.$#'
683-
identifier: return.type
684-
count: 1
685-
path: ../../src/SAML2/ReceivedAuthnRequest.php
686-
687657
-
688658
message: '#^Method Surfnet\\SamlBundle\\SAML2\\ReceivedAuthnRequest\:\:getScopingRequesterIds\(\) return type has no value type specified in iterable type array\.$#'
689659
identifier: missingType.iterableValue
@@ -708,12 +678,6 @@ parameters:
708678
count: 1
709679
path: ../../src/SAML2/ReceivedAuthnRequest.php
710680

711-
-
712-
message: '#^Parameter \#1 \$array of function reset expects array\|object, mixed given\.$#'
713-
identifier: argument.type
714-
count: 1
715-
path: ../../src/SAML2/ReceivedAuthnRequest.php
716-
717681
-
718682
message: '#^Parameter \#1 \$xml of static method SAML2\\Message\:\:fromXML\(\) expects DOMElement, DOMNode given\.$#'
719683
identifier: argument.type

‎src/SAML2/Extensions/GsspUserAttributesChunk.php‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
use DOMDocument;
2121
use DOMElement;
22+
use RuntimeException;
2223
use SAML2\Utils;
2324

2425
class GsspUserAttributesChunk extends Chunk
@@ -67,7 +68,15 @@ public function addAttribute(string $name, string $format, string $value): void
6768

6869
public function toXML(): string
6970
{
70-
return $this->getValue()->ownerDocument->saveXML();
71+
$doc = $this->getValue()->ownerDocument;
72+
if ($doc === null) {
73+
throw new RuntimeException('DOMElement has no ownerDocument');
74+
}
75+
$xml = $doc->saveXML();
76+
if ($xml === false) {
77+
throw new RuntimeException('Failed to serialize XML document');
78+
}
79+
return $xml;
7180
}
7281

7382
public static function fromXML(string $xmlString): GsspUserAttributesChunk

‎src/SAML2/Extensions/MduiChunk.php‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
use DOMDocument;
2222
use DOMElement;
23+
use RuntimeException;
2324

2425
class MduiChunk extends Chunk
2526
{
@@ -38,7 +39,11 @@ public function __construct(?DOMElement $value = null)
3839
}
3940

4041
$doc->appendChild($doc->importNode($root, true));
41-
parent::__construct('UIInfo', self::MDUI_NAMESPACE, $doc->documentElement);
42+
$element = $doc->documentElement;
43+
if ($element === null) {
44+
throw new RuntimeException('Failed to create UIInfo root element');
45+
}
46+
parent::__construct('UIInfo', self::MDUI_NAMESPACE, $element);
4247
}
4348

4449
/**
@@ -65,7 +70,15 @@ public function getDisplayNames(): array
6570

6671
public function toXML(): string
6772
{
68-
return $this->getValue()->ownerDocument->saveXML();
73+
$doc = $this->getValue()->ownerDocument;
74+
if ($doc === null) {
75+
throw new RuntimeException('DOMElement has no ownerDocument');
76+
}
77+
$xml = $doc->saveXML();
78+
if ($xml === false) {
79+
throw new RuntimeException('Failed to serialize XML document');
80+
}
81+
return $xml;
6982
}
7083

7184
public static function fromXML(string $xmlString): self

0 commit comments

Comments
 (0)