Skip to content

Commit b764f1e

Browse files
committed
Replace Psalm with PHPStan
1 parent 6c1ca39 commit b764f1e

32 files changed

+203
-208
lines changed

.gitattributes

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ codecov.yml export-ignore
99
.editorconfig export-ignore
1010
.gitattributes export-ignore
1111
.gitignore export-ignore
12-
psalm.xml export-ignore
13-
psalm-dev.xml export-ignore
12+
phpstan.neon export-ignore
13+
phpstan-dev.neon export-ignore
1414
phpcs.xml export-ignore
1515
phpunit.xml export-ignore
1616
.php_cs.dist export-ignore

.github/workflows/php.yml

+6-20
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,8 @@ jobs:
4848
with:
4949
# Should be the higest supported version, so we can use the newest tools
5050
php-version: '8.3'
51-
tools: composer, composer-require-checker, composer-unused, phpcs, psalm
52-
# optional performance gain for psalm: opcache
53-
extensions: ctype, date, dom, filter, libxml, opcache, pcre, spl, xml
51+
tools: composer, composer-require-checker, composer-unused, phpcs, phpstan
52+
extensions: ctype, date, dom, filter, libxml, pcre, spl, xml
5453
coverage: none
5554

5655
- name: Setup problem matchers for PHP
@@ -83,26 +82,13 @@ jobs:
8382
- name: PHP Code Sniffer
8483
run: phpcs
8584

86-
- name: Psalm
87-
run: |
88-
psalm -c psalm.xml \
89-
--show-info=true \
90-
--shepherd \
91-
--php-version=${{ steps.setup-php.outputs.php-version }}
92-
93-
- name: Psalm (testsuite)
85+
- name: PHPStan
9486
run: |
95-
psalm -c psalm-dev.xml \
96-
--show-info=true \
97-
--shepherd \
98-
--php-version=${{ steps.setup-php.outputs.php-version }}
87+
phpstan analyze -c phpstan.neon
9988
100-
- name: Psalter
89+
- name: PHPStan (testsuite)
10190
run: |
102-
psalm --alter \
103-
--issues=UnnecessaryVarAnnotation \
104-
--dry-run \
105-
--php-version=${{ steps.setup-php.outputs.php-version }}
91+
phpstan analyze -c phpstan-dev.neon
10692
10793
security:
10894
name: Security checks

README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@
33
![Build Status](https://github.com/simplesamlphp/xml-common/workflows/CI/badge.svg?branch=master)
44
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/simplesamlphp/xml-common/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/simplesamlphp/xml-common/?branch=master)
55
[![Coverage Status](https://codecov.io/gh/simplesamlphp/xml-common/branch/master/graph/badge.svg)](https://codecov.io/gh/simplesamlphp/xml-common)
6-
[![Type coverage](https://shepherd.dev/github/simplesamlphp/xml-common/coverage.svg)](https://shepherd.dev/github/simplesamlphp/xml-common)
7-
[![Psalm Level](https://shepherd.dev/github/simplesamlphp/xml-common/level.svg)](https://shepherd.dev/github/simplesamlphp/xml-common)
6+
[![PHPStan Enabled](https://img.shields.io/badge/PHPStan-enabled-brightgreen.svg?style=flat)](https://github.com/simplesamlphp/xml-common)

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"ext-spl": "*",
3535
"ext-xmlreader": "*",
3636

37-
"simplesamlphp/assert": "^1.0"
37+
"simplesamlphp/assert": "^1.1"
3838
},
3939
"require-dev": {
4040
"simplesamlphp/simplesamlphp-test-framework": "^1.5"

phpstan-dev.neon

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
parameters:
2+
level: 9
3+
paths:
4+
- tests

phpstan.neon

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
parameters:
2+
level: 6
3+
paths:
4+
- src

psalm-dev.xml

-27
This file was deleted.

psalm.xml

-38
This file was deleted.

src/AbstractElement.php

+11-9
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ public function instantiateParentElement(DOMElement $parent = null): DOMElement
4545
$parent = DOMDocumentFactory::create();
4646
$e = $parent->createElementNS($namespace, $qualifiedName);
4747
} else {
48-
/** @psalm-var \DOMDocument $doc */
4948
$doc = $parent->ownerDocument;
5049
Assert::notNull($doc);
5150
$e = $doc->createElementNS($namespace, $qualifiedName);
@@ -87,7 +86,7 @@ public static function getAttribute(DOMElement $xml, string $name): string
8786
* @param \DOMElement $xml The element where we should search for the attribute.
8887
* @param string $name The name of the attribute.
8988
* @param string|null $default The default to return in case the attribute does not exist and it is optional.
90-
* @psalm-return ($default is string ? string : string|null)
89+
* @return ($default is string ? string : string|null)
9190
*/
9291
public static function getOptionalAttribute(DOMElement $xml, string $name, ?string $default = null): ?string
9392
{
@@ -128,7 +127,7 @@ public static function getBooleanAttribute(DOMElement $xml, string $name): bool
128127
* @param \DOMElement $xml The element where we should search for the attribute.
129128
* @param string $name The name of the attribute.
130129
* @param bool|null $default The default to return in case the attribute does not exist and it is optional.
131-
* @psalm-return ($default is bool ? bool : bool|null)
130+
* @return ($default is bool ? bool : bool|null)
132131
*
133132
* @throws \SimpleSAML\Assert\AssertionFailedException if the attribute is not a boolean
134133
*/
@@ -174,7 +173,7 @@ public static function getIntegerAttribute(DOMElement $xml, string $name): int
174173
* @param \DOMElement $xml The element where we should search for the attribute.
175174
* @param string $name The name of the attribute.
176175
* @param int|null $default The default to return in case the attribute does not exist and it is optional.
177-
* @psalm-return ($default is int ? int : int|null)
176+
* @return ($default is int ? int : int|null)
178177
*
179178
* @throws \SimpleSAML\Assert\AssertionFailedException if the attribute is not an integer
180179
*/
@@ -227,10 +226,10 @@ public static function getChildrenOfClass(DOMElement $parent): array
227226
$ret = [];
228227
foreach ($parent->childNodes as $node) {
229228
if (
230-
$node->namespaceURI === static::getNamespaceURI()
229+
$node instanceof DOMElement
230+
&& $node->namespaceURI === static::getNamespaceURI()
231231
&& $node->localName === static::getLocalName()
232232
) {
233-
/** @psalm-var \DOMElement $node */
234233
$ret[] = static::fromXML($node);
235234
}
236235
}
@@ -252,9 +251,11 @@ public static function getNamespaceURI(): ?string
252251
. '::NS constant must be defined and set to the namespace for the XML-class it represents.',
253252
RuntimeException::class,
254253
);
255-
Assert::nullOrValidURI(static::NS, SchemaViolationException::class);
254+
// @phpstan-ignore classConstant.notFound
255+
Assert::nullOrValidURI(static::NS, SchemaViolationException::class); // @phpstan-ignore-line
256256

257-
return static::NS;
257+
// @phpstan-ignore classConstant.notFound
258+
return static::NS; // @phpstan-ignore-line
258259
}
259260

260261

@@ -272,7 +273,8 @@ public static function getNamespacePrefix(): string
272273
RuntimeException::class,
273274
);
274275

275-
return strval(static::NS_PREFIX);
276+
// @phpstan-ignore classConstant.notFound
277+
return strval(static::NS_PREFIX); // @phpstan-ignore-line
276278
}
277279

278280

src/ArrayizableElementInterface.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ interface ArrayizableElementInterface
1414
/**
1515
* Create a class from an array
1616
*
17-
* @param array $data
17+
* @param array<string, mixed> $data
1818
* @return static
1919
*/
2020
public static function fromArray(array $data): static;
@@ -23,7 +23,7 @@ public static function fromArray(array $data): static;
2323
/**
2424
* Create an array from this class
2525
*
26-
* @return array
26+
* @return array<string, mixed>
2727
*/
2828
public function toArray(): array;
2929
}

src/Attribute.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,17 @@ final class Attribute implements ArrayizableElementInterface
2121
* Create an Attribute class
2222
*
2323
* @param string|null $namespaceURI
24-
* @param string $namespacePrefix
24+
* @param string|null $namespacePrefix
2525
* @param string $attrName
2626
* @param string $attrValue
2727
*/
2828
public function __construct(
2929
protected ?string $namespaceURI,
30-
protected string $namespacePrefix,
30+
protected ?string $namespacePrefix,
3131
protected string $attrName,
3232
protected string $attrValue,
3333
) {
3434
Assert::nullOrStringNotEmpty($namespaceURI);
35-
Assert::string($namespacePrefix);
3635
Assert::notSame('xmlns', $namespacePrefix);
3736
if ($namespaceURI !== null) {
3837
Assert::stringNotEmpty($namespacePrefix);
@@ -89,7 +88,7 @@ public function getAttrValue(): string
8988
/**
9089
* Create a class from XML
9190
*
92-
* @param \DOMAttr $xml
91+
* @param \DOMAttr $attr
9392
* @return static
9493
*/
9594
public static function fromXML(DOMAttr $attr): static
@@ -120,7 +119,7 @@ public function toXML(DOMElement $parent): DOMElement
120119
/**
121120
* Create a class from an array
122121
*
123-
* @param array $data
122+
* @param array{namespaceURI: string, namespacePrefix: string|null, attrName: string, attrValue: mixed} $data
124123
* @return static
125124
*/
126125
public static function fromArray(array $data): static
@@ -139,13 +138,14 @@ public static function fromArray(array $data): static
139138
/**
140139
* Validates an array representation of this object and returns the same array with rationalized keys
141140
*
142-
* @param array $data
143-
* @return array
141+
* @param array{namespaceURI: string, namespacePrefix: string|null, attrName: string, attrValue: mixed} $data
142+
* @return array{namespaceURI: string, namespacePrefix: string|null, attrName: string, attrValue: mixed}
144143
*/
145144
private static function processArrayContents(array $data): array
146145
{
147146
$data = array_change_key_case($data, CASE_LOWER);
148147

148+
/** @var array{namespaceuri: string, namespaceprefix: string|null, attrname: string, attrvalue: mixed} $data */
149149
Assert::allOneOf(
150150
array_keys($data),
151151
['namespaceuri', 'namespaceprefix', 'attrname', 'attrvalue'],

src/Chunk.php

+5-9
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ public static function getAttribute(DOMElement $xml, string $name): string
180180
* @param \DOMElement $xml The element where we should search for the attribute.
181181
* @param string $name The name of the attribute.
182182
* @param string|null $default The default to return in case the attribute does not exist and it is optional.
183-
* @psalm-return ($default is string ? string : null)
183+
* @return ($default is string ? string : null)
184184
*/
185185
public static function getOptionalAttribute(DOMElement $xml, string $name, ?string $default = null): ?string
186186
{
@@ -218,7 +218,7 @@ public static function getBooleanAttribute(DOMElement $xml, string $name): bool
218218
* @param \DOMElement $xml The element where we should search for the attribute.
219219
* @param string $name The name of the attribute.
220220
* @param bool|null $default The default to return in case the attribute does not exist and it is optional.
221-
* @psalm-return ($default is bool ? bool : null)
221+
* @return ($default is bool ? bool : null)
222222
*
223223
* @throws \SimpleSAML\Assert\AssertionFailedException if the attribute is not a boolean
224224
*/
@@ -237,7 +237,6 @@ public static function getOptionalBooleanAttribute(DOMElement $xml, string $name
237237
*
238238
* @param \DOMElement $xml The element where we should search for the attribute.
239239
* @param string $name The name of the attribute.
240-
* @param int $default The default to return in case the attribute does not exist and it is optional.
241240
* @return int
242241
*
243242
* @throws \SimpleSAML\XML\Exception\MissingAttributeException if the attribute is missing from the element
@@ -262,7 +261,7 @@ public static function getIntegerAttribute(DOMElement $xml, string $name): int
262261
* @param \DOMElement $xml The element where we should search for the attribute.
263262
* @param string $name The name of the attribute.
264263
* @param int|null $default The default to return in case the attribute does not exist and it is optional.
265-
* @psalm-return ($default is int ? int : null)
264+
* @return ($default is int ? int : null)
266265
*
267266
* @throws \SimpleSAML\Assert\AssertionFailedException if the attribute is not an integer
268267
*/
@@ -283,8 +282,9 @@ public static function getOptionalIntegerAttribute(DOMElement $xml, string $name
283282
*/
284283
public function isEmptyElement(): bool
285284
{
285+
/** @var \DOMElement $xml */
286286
$xml = $this->getXML();
287-
return ($xml->childNodes->length === 0) && ($xml->attributes->length === 0);
287+
return ($xml->childNodes->length === 0) && ($xml->attributes->count() === 0);
288288
}
289289

290290

@@ -317,10 +317,6 @@ public function toXML(DOMElement $parent = null): DOMElement
317317
$parent = $doc;
318318
}
319319

320-
/**
321-
* @psalm-var \DOMDocument $parent
322-
* @psalm-var \DOMDocument $doc
323-
*/
324320
$parent->appendChild($doc->importNode($this->getXML(), true));
325321

326322
return $doc->documentElement;

src/DOMDocumentFactory.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ final class DOMDocumentFactory
2424
{
2525
/**
2626
* @param string $xml
27-
* @psalm-param non-empty-string $xml
27+
* @param non-empty-string $xml
2828
*
2929
* @return \DOMDocument
3030
*/
@@ -76,15 +76,13 @@ public static function fromFile(string $file): DOMDocument
7676
error_clear_last();
7777
$xml = @file_get_contents($file);
7878
if ($xml === false) {
79-
/** @psalm-var array $e */
8079
$e = error_get_last();
8180
$error = $e['message'] ?: "Check that the file exists and can be read.";
8281

8382
throw new IOException("File '$file' was not loaded; $error");
8483
}
8584

8685
Assert::notWhitespaceOnly($xml, sprintf('File "%s" does not have content', $file), RuntimeException::class);
87-
/** @psalm-var non-empty-string $xml */
8886
return static::fromString($xml);
8987
}
9088

0 commit comments

Comments
 (0)