Skip to content

Commit

Permalink
Replace Psalm with PHPStan
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Jan 20, 2024
1 parent 6c1ca39 commit 91cc001
Show file tree
Hide file tree
Showing 26 changed files with 175 additions and 165 deletions.
4 changes: 2 additions & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ codecov.yml export-ignore
.editorconfig export-ignore
.gitattributes export-ignore
.gitignore export-ignore
psalm.xml export-ignore
psalm-dev.xml export-ignore
phpstan.neon export-ignore
phpstan-dev.neon export-ignore
phpcs.xml export-ignore
phpunit.xml export-ignore
.php_cs.dist export-ignore
Expand Down
26 changes: 6 additions & 20 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,8 @@ jobs:
with:
# Should be the higest supported version, so we can use the newest tools
php-version: '8.3'
tools: composer, composer-require-checker, composer-unused, phpcs, psalm
# optional performance gain for psalm: opcache
extensions: ctype, date, dom, filter, libxml, opcache, pcre, spl, xml
tools: composer, composer-require-checker, composer-unused, phpcs, phpstan
extensions: ctype, date, dom, filter, libxml, pcre, spl, xml
coverage: none

- name: Setup problem matchers for PHP
Expand Down Expand Up @@ -83,26 +82,13 @@ jobs:
- name: PHP Code Sniffer
run: phpcs

- name: Psalm
run: |
psalm -c psalm.xml \
--show-info=true \
--shepherd \
--php-version=${{ steps.setup-php.outputs.php-version }}
- name: Psalm (testsuite)
- name: PHPStan
run: |
psalm -c psalm-dev.xml \
--show-info=true \
--shepherd \
--php-version=${{ steps.setup-php.outputs.php-version }}
phpstan analyze -c phpstan.neon
- name: Psalter
- name: PHPStan (testsuite)
run: |
psalm --alter \
--issues=UnnecessaryVarAnnotation \
--dry-run \
--php-version=${{ steps.setup-php.outputs.php-version }}
phpstan analyze -c phpstan-dev.neon
security:
name: Security checks
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"ext-spl": "*",
"ext-xmlreader": "*",

"simplesamlphp/assert": "^1.0"
"simplesamlphp/assert": "^1.1"
},
"require-dev": {
"simplesamlphp/simplesamlphp-test-framework": "^1.5"
Expand Down
4 changes: 4 additions & 0 deletions phpstan-dev.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
parameters:
level: 9
paths:
- tests
4 changes: 4 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
parameters:
level: 6
paths:
- src
27 changes: 0 additions & 27 deletions psalm-dev.xml

This file was deleted.

38 changes: 0 additions & 38 deletions psalm.xml

This file was deleted.

9 changes: 6 additions & 3 deletions src/AbstractElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,11 @@ public static function getNamespaceURI(): ?string
. '::NS constant must be defined and set to the namespace for the XML-class it represents.',
RuntimeException::class,
);
Assert::nullOrValidURI(static::NS, SchemaViolationException::class);
// @phpstan-ignore classConstant.notFound
Assert::nullOrValidURI(static::NS, SchemaViolationException::class); // @phpstan-ignore-line

return static::NS;
// @phpstan-ignore classConstant.notFound
return static::NS; // @phpstan-ignore-line
}


Expand All @@ -272,7 +274,8 @@ public static function getNamespacePrefix(): string
RuntimeException::class,
);

return strval(static::NS_PREFIX);
// @phpstan-ignore classConstant.notFound
return strval(static::NS_PREFIX); // @phpstan-ignore-line
}


Expand Down
4 changes: 2 additions & 2 deletions src/ArrayizableElementInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ interface ArrayizableElementInterface
/**
* Create a class from an array
*
* @param array $data
* @param array<string, mixed> $data
* @return static
*/
public static function fromArray(array $data): static;
Expand All @@ -23,7 +23,7 @@ public static function fromArray(array $data): static;
/**
* Create an array from this class
*
* @return array
* @return array<string, mixed>
*/
public function toArray(): array;
}
9 changes: 5 additions & 4 deletions src/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function getAttrValue(): string
/**
* Create a class from XML
*
* @param \DOMAttr $xml
* @param \DOMAttr $attr
* @return static
*/
public static function fromXML(DOMAttr $attr): static
Expand Down Expand Up @@ -120,7 +120,7 @@ public function toXML(DOMElement $parent): DOMElement
/**
* Create a class from an array
*
* @param array $data
* @param array{namespaceURI: string, namespacePrefix: string|null, attrName: string, attrValue: mixed} $data
* @return static
*/
public static function fromArray(array $data): static
Expand All @@ -139,13 +139,14 @@ public static function fromArray(array $data): static
/**
* Validates an array representation of this object and returns the same array with rationalized keys
*
* @param array $data
* @return array
* @param array{namespaceURI: string, namespacePrefix: string|null, attrName: string, attrValue: mixed} $data
* @return array{namespaceURI: string, namespacePrefix: string|null, attrName: string, attrValue: mixed}
*/
private static function processArrayContents(array $data): array
{
$data = array_change_key_case($data, CASE_LOWER);

/** @var array{namespaceuri: string, namespaceprefix: string|null, attrname: string, attrvalue: mixed} $data */
Assert::allOneOf(
array_keys($data),
['namespaceuri', 'namespaceprefix', 'attrname', 'attrvalue'],
Expand Down
1 change: 0 additions & 1 deletion src/Chunk.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,6 @@ public static function getOptionalBooleanAttribute(DOMElement $xml, string $name
*
* @param \DOMElement $xml The element where we should search for the attribute.
* @param string $name The name of the attribute.
* @param int $default The default to return in case the attribute does not exist and it is optional.
* @return int
*
* @throws \SimpleSAML\XML\Exception\MissingAttributeException if the attribute is missing from the element
Expand Down
4 changes: 1 addition & 3 deletions src/DOMDocumentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ final class DOMDocumentFactory
{
/**
* @param string $xml
* @psalm-param non-empty-string $xml
* @param non-empty-string $xml
*
* @return \DOMDocument
*/
Expand Down Expand Up @@ -76,15 +76,13 @@ public static function fromFile(string $file): DOMDocument
error_clear_last();
$xml = @file_get_contents($file);
if ($xml === false) {
/** @psalm-var array $e */
$e = error_get_last();
$error = $e['message'] ?: "Check that the file exists and can be read.";

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

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

Expand Down
7 changes: 2 additions & 5 deletions src/SerializableElementTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,8 @@ public function __toString(): string
{
$xml = $this->toXML();

/** @psalm-var \DOMDocument $xml->ownerDocument */
$xmlString = $xml->ownerDocument->saveXML();

/** @psalm-var non-empty-string $xmlString */
$doc = DOMDocumentFactory::fromString($xmlString);
$doc->formatOutput = $this->formatOutput;

Expand All @@ -52,12 +50,11 @@ public function __toString(): string
*
* This method will be invoked by any calls to serialize().
*
* @return array The serialized representation of this XML object.
* @return array{0: string} The serialized representation of this XML object.
*/
public function __serialize(): array
{
$xml = $this->toXML();
/** @psalm-var \DOMDocument $xml->ownerDocument */
return [$xml->ownerDocument->saveXML($xml)];
}

Expand All @@ -68,7 +65,7 @@ public function __serialize(): array
* This method will be invoked by any calls to unserialize(), allowing us to restore any data that might not
* be serializable in its original form (e.g.: DOM objects).
*
* @param array $serialized The XML object that we want to restore.
* @param array{0: string} $serialized The XML object that we want to restore.
*/
public function __unserialize(array $serialized): void
{
Expand Down
7 changes: 3 additions & 4 deletions src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Utils
* @param \DOMElement $parent The element that contains the localized strings.
* @param string $namespaceURI The namespace URI the localized strings should have.
* @param string $localName The localName of the localized strings.
* @return array Localized strings.
* @return array<string, string> Localized strings.
*/
public static function extractLocalizedStrings(DOMElement $parent, string $namespaceURI, string $localName): array
{
Expand Down Expand Up @@ -57,7 +57,7 @@ public static function extractLocalizedStrings(DOMElement $parent, string $names
* @param \DOMElement $parent The element that contains the localized strings.
* @param string $namespaceURI The namespace URI the string elements should have.
* @param string $localName The localName of the string elements.
* @return array The string values of the various nodes.
* @return string[] The string values of the various nodes.
*/
public static function extractStrings(DOMElement $parent, string $namespaceURI, string $localName): array
{
Expand Down Expand Up @@ -111,7 +111,7 @@ public static function addString(
* @param string $namespace The namespace of the created elements
* @param string $name The name of the created elements
* @param bool $localized Whether the strings are localized, and should include the xml:lang attribute.
* @param array $values The values we should create the elements from.
* @param string[] $values The values we should create the elements from.
*/
public static function addStrings(
DOMElement $parent,
Expand All @@ -122,7 +122,6 @@ public static function addStrings(
): void {
$doc = $parent->ownerDocument;
Assert::notNull($doc);
/** @psalm-var \DOMDocument $doc */

foreach ($values as $index => $value) {
$n = $doc->createElementNS($namespace, $name);
Expand Down
1 change: 0 additions & 1 deletion tests/Utils/ExtendableAttributesElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ public static function fromXML(DOMElement $xml): static
*/
public function toXML(DOMElement $parent = null): DOMElement
{
/** @psalm-var \DOMDocument $e->ownerDocument */
$e = $this->instantiateParentElement($parent);

foreach ($this->getAttributesNS() as $attr) {
Expand Down
2 changes: 1 addition & 1 deletion tests/Utils/ExtendableElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class ExtendableElement extends AbstractElement
/** @var string */
public const LOCALNAME = 'ExtendableElement';

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


Expand Down
5 changes: 4 additions & 1 deletion tests/Utils/XMLDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ final class XMLDumper
{
public static function dumpDOMDocumentXMLWithBase64Content(DOMDocument $document): string
{
/** @var string $dump */
$dump = $document->saveXML($document->documentElement);
return preg_replace('/ *[\\r\\n] */', '', $dump);
/** @var string $result */
$result = preg_replace('/ *[\\r\\n] */', '', $dump);
return $result;
}
}
17 changes: 13 additions & 4 deletions tests/XML/AbstractElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ public function testMarshalling(): void
*/
public function testUnmarshalling(): void
{
$element = Element::fromXML(self::$xmlRepresentation->documentElement);
/** @var \DOMElement $elt */
$elt = self::$xmlRepresentation->documentElement;
$element = Element::fromXML($elt);

$this->assertEquals(2, $element->getInteger());
$this->assertEquals(false, $element->getBoolean());
Expand All @@ -67,6 +69,7 @@ public function testUnmarshalling(): void
*/
public function testGetAttribute(): void
{
/** @var \DOMElement $xml */
$xml = self::$xmlRepresentation->documentElement;

// Get mandatory attributes
Expand Down Expand Up @@ -101,7 +104,9 @@ public function testGetAttribute(): void
*/
public function testGetAttributeThrowsExceptionOnMissingAttribute(): void
{
$xml = clone self::$xmlRepresentation->documentElement;
/** @var \DOMElement $xml */
$xml = self::$xmlRepresentation->documentElement;
$xml = clone $xml;
$xml->removeAttribute('text');

$this->expectException(MissingAttributeException::class);
Expand All @@ -113,7 +118,9 @@ public function testGetAttributeThrowsExceptionOnMissingAttribute(): void
*/
public function testGetBooleanAttributeThrowsExceptionOnMissingAttribute(): void
{
$xml = clone self::$xmlRepresentation->documentElement;
/** @var \DOMElement $xml */
$xml = self::$xmlRepresentation->documentElement;
$xml = clone $xml;
$xml->removeAttribute('boolean');

$this->expectException(MissingAttributeException::class);
Expand All @@ -125,7 +132,9 @@ public function testGetBooleanAttributeThrowsExceptionOnMissingAttribute(): void
*/
public function testGetIntegerAttributeThrowsExceptionOnMissingAttribute(): void
{
$xml = clone self::$xmlRepresentation->documentElement;
/** @var \DOMElement $xml */
$xml = self::$xmlRepresentation->documentElement;
$xml = clone $xml;
$xml->removeAttribute('integer');

$this->expectException(MissingAttributeException::class);
Expand Down
Loading

0 comments on commit 91cc001

Please sign in to comment.