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 21, 2024
1 parent 6c1ca39 commit b764f1e
Show file tree
Hide file tree
Showing 32 changed files with 203 additions and 208 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
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@
![Build Status](https://github.com/simplesamlphp/xml-common/workflows/CI/badge.svg?branch=master)
[![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)
[![Coverage Status](https://codecov.io/gh/simplesamlphp/xml-common/branch/master/graph/badge.svg)](https://codecov.io/gh/simplesamlphp/xml-common)
[![Type coverage](https://shepherd.dev/github/simplesamlphp/xml-common/coverage.svg)](https://shepherd.dev/github/simplesamlphp/xml-common)
[![Psalm Level](https://shepherd.dev/github/simplesamlphp/xml-common/level.svg)](https://shepherd.dev/github/simplesamlphp/xml-common)
[![PHPStan Enabled](https://img.shields.io/badge/PHPStan-enabled-brightgreen.svg?style=flat)](https://github.com/simplesamlphp/xml-common)
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.

20 changes: 11 additions & 9 deletions src/AbstractElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public function instantiateParentElement(DOMElement $parent = null): DOMElement
$parent = DOMDocumentFactory::create();
$e = $parent->createElementNS($namespace, $qualifiedName);
} else {
/** @psalm-var \DOMDocument $doc */
$doc = $parent->ownerDocument;
Assert::notNull($doc);
$e = $doc->createElementNS($namespace, $qualifiedName);
Expand Down Expand Up @@ -87,7 +86,7 @@ public static function getAttribute(DOMElement $xml, string $name): string
* @param \DOMElement $xml The element where we should search for the attribute.
* @param string $name The name of the attribute.
* @param string|null $default The default to return in case the attribute does not exist and it is optional.
* @psalm-return ($default is string ? string : string|null)
* @return ($default is string ? string : string|null)
*/
public static function getOptionalAttribute(DOMElement $xml, string $name, ?string $default = null): ?string
{
Expand Down Expand Up @@ -128,7 +127,7 @@ public static function getBooleanAttribute(DOMElement $xml, string $name): bool
* @param \DOMElement $xml The element where we should search for the attribute.
* @param string $name The name of the attribute.
* @param bool|null $default The default to return in case the attribute does not exist and it is optional.
* @psalm-return ($default is bool ? bool : bool|null)
* @return ($default is bool ? bool : bool|null)
*
* @throws \SimpleSAML\Assert\AssertionFailedException if the attribute is not a boolean
*/
Expand Down Expand Up @@ -174,7 +173,7 @@ public static function getIntegerAttribute(DOMElement $xml, string $name): int
* @param \DOMElement $xml The element where we should search for the attribute.
* @param string $name The name of the attribute.
* @param int|null $default The default to return in case the attribute does not exist and it is optional.
* @psalm-return ($default is int ? int : int|null)
* @return ($default is int ? int : int|null)
*
* @throws \SimpleSAML\Assert\AssertionFailedException if the attribute is not an integer
*/
Expand Down Expand Up @@ -227,10 +226,10 @@ public static function getChildrenOfClass(DOMElement $parent): array
$ret = [];
foreach ($parent->childNodes as $node) {
if (
$node->namespaceURI === static::getNamespaceURI()
$node instanceof DOMElement
&& $node->namespaceURI === static::getNamespaceURI()
&& $node->localName === static::getLocalName()
) {
/** @psalm-var \DOMElement $node */
$ret[] = static::fromXML($node);
}
}
Expand All @@ -252,9 +251,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 +273,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;
}
14 changes: 7 additions & 7 deletions src/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,17 @@ final class Attribute implements ArrayizableElementInterface
* Create an Attribute class
*
* @param string|null $namespaceURI
* @param string $namespacePrefix
* @param string|null $namespacePrefix
* @param string $attrName
* @param string $attrValue
*/
public function __construct(
protected ?string $namespaceURI,
protected string $namespacePrefix,
protected ?string $namespacePrefix,
protected string $attrName,
protected string $attrValue,
) {
Assert::nullOrStringNotEmpty($namespaceURI);
Assert::string($namespacePrefix);
Assert::notSame('xmlns', $namespacePrefix);
if ($namespaceURI !== null) {
Assert::stringNotEmpty($namespacePrefix);
Expand Down Expand Up @@ -89,7 +88,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 +119,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 +138,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
14 changes: 5 additions & 9 deletions src/Chunk.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public static function getAttribute(DOMElement $xml, string $name): string
* @param \DOMElement $xml The element where we should search for the attribute.
* @param string $name The name of the attribute.
* @param string|null $default The default to return in case the attribute does not exist and it is optional.
* @psalm-return ($default is string ? string : null)
* @return ($default is string ? string : null)
*/
public static function getOptionalAttribute(DOMElement $xml, string $name, ?string $default = null): ?string
{
Expand Down Expand Up @@ -218,7 +218,7 @@ public static function getBooleanAttribute(DOMElement $xml, string $name): bool
* @param \DOMElement $xml The element where we should search for the attribute.
* @param string $name The name of the attribute.
* @param bool|null $default The default to return in case the attribute does not exist and it is optional.
* @psalm-return ($default is bool ? bool : null)
* @return ($default is bool ? bool : null)
*
* @throws \SimpleSAML\Assert\AssertionFailedException if the attribute is not a boolean
*/
Expand All @@ -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 All @@ -262,7 +261,7 @@ public static function getIntegerAttribute(DOMElement $xml, string $name): int
* @param \DOMElement $xml The element where we should search for the attribute.
* @param string $name The name of the attribute.
* @param int|null $default The default to return in case the attribute does not exist and it is optional.
* @psalm-return ($default is int ? int : null)
* @return ($default is int ? int : null)
*
* @throws \SimpleSAML\Assert\AssertionFailedException if the attribute is not an integer
*/
Expand All @@ -283,8 +282,9 @@ public static function getOptionalIntegerAttribute(DOMElement $xml, string $name
*/
public function isEmptyElement(): bool
{
/** @var \DOMElement $xml */
$xml = $this->getXML();
return ($xml->childNodes->length === 0) && ($xml->attributes->length === 0);
return ($xml->childNodes->length === 0) && ($xml->attributes->count() === 0);
}


Expand Down Expand Up @@ -317,10 +317,6 @@ public function toXML(DOMElement $parent = null): DOMElement
$parent = $doc;
}

/**
* @psalm-var \DOMDocument $parent
* @psalm-var \DOMDocument $doc
*/
$parent->appendChild($doc->importNode($this->getXML(), true));

return $doc->documentElement;
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
Loading

0 comments on commit b764f1e

Please sign in to comment.