Skip to content

Commit

Permalink
Fix static analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
veewee committed Apr 16, 2024
1 parent e764516 commit ea92150
Show file tree
Hide file tree
Showing 9 changed files with 187 additions and 21 deletions.
1 change: 1 addition & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
</issueHandlers>
<stubs>
<file name="stubs/DOM.phpstub" />
<file name="stubs/php_xsl.stub.phpstub" />
<file name="stubs/XMLReader.phpstub" />
<file name="stubs/XMLWriter.phpstub" />
</stubs>
Expand Down
6 changes: 3 additions & 3 deletions src/Xml/Dom/Collection/NodeList.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use \DOM\Element;
use \DOM\Node;
use \DOM\NodeList as DOMNodeList;
use DOMXpath as DOMXpath;
use \DOM\XPath as DOMXPath;
use Generator;
use InvalidArgumentException;
use IteratorAggregate;
Expand Down Expand Up @@ -193,7 +193,7 @@ public function reduce(callable $reducer, mixed $initial): mixed
}

/**
* @param list<callable(DOMXpath): DOMXpath> $configurators
* @param list<callable(DOMXPath): DOMXPath> $configurators
* @throws RuntimeException
* @return NodeList<\DOM\Node>
*/
Expand All @@ -211,7 +211,7 @@ public function query(string $xpath, callable ... $configurators): self

/**
* @template X
* @param list<callable(DOMXpath): DOMXpath> $configurators
* @param list<callable(DOMXPath): DOMXPath> $configurators
* @param TypeInterface<X> $type
* @return list<X>
*/
Expand Down
5 changes: 2 additions & 3 deletions src/Xml/Dom/Locator/Node/detect_document.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
namespace VeeWee\Xml\Dom\Locator\Node;

use \DOM\XMLDocument;
use \DOM\Node;
use InvalidArgumentException;
use Webmozart\Assert\Assert;
use function Psl\Type\instance_of;
use function VeeWee\Xml\Dom\Predicate\is_document;

/**
Expand All @@ -16,5 +15,5 @@
*/
function detect_document(\DOM\Node $node): \DOM\XMLDocument
{
return is_document($node) ? $node : $node->ownerDocument;
return is_document($node) ? $node : instance_of(XMLDocument::class)->assert($node->ownerDocument);
}
5 changes: 4 additions & 1 deletion src/Xml/Dom/Manipulator/Attribute/rename.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ function rename(\DOM\Attr $target, string $newQName, ?string $newNamespaceURI =
{
return disallow_issues(static fn (): \DOM\Attr => match(true) {
is_xmlns_attribute($target) => rename_xmlns_attribute($target, $newQName),
default => tap(fn () => $target->rename($newNamespaceURI, $newQName))($target)
default => (function() use ($target, $newNamespaceURI, $newQName): \DOM\Attr {
$target->rename($newNamespaceURI, $newQName);
return $target;
})()
});
}
2 changes: 1 addition & 1 deletion src/Xml/Dom/Manipulator/Document/optimize_namespaces.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function optimize_namespaces(\DOM\XMLDocument $document, string $prefix = 'ns'):
$documentElement = $document->documentElement;
$namespaceURIs = values(unique(map(
recursive_linked_namespaces($documentElement),
static fn (\DOM\NamespaceInfo $info): string => $info->namespaceURI
static fn (\DOM\NamespaceInfo $info): string => $info->namespaceURI // TODO -> what on null
)));

foreach (sort($namespaceURIs) as $index => $namespaceURI) {
Expand Down
3 changes: 2 additions & 1 deletion src/Xml/Dom/Traverser/Visitor/RemoveNamespaces.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ final class RemoveNamespaces extends AbstractVisitor
private $filter;

/**
* @param null | callable(\DOM\Attr): bool $filter
* @param null | callable(\DOM\Attr | \DOM\Element): bool $filter
*/
public function __construct(
?callable $filter = null
Expand Down Expand Up @@ -81,6 +81,7 @@ public function onNodeEnter(\DOM\Node $node): Action
return new Action\Noop();
}

/** @var \DOM\Element | \DOM\Attr $node */
return new Action\RenameNode($node->localName, null);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Xml/Encoding/Internal/Decoder/Builder/element.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ function element(\DOM\Element $element): array
$namespaces = namespaces($element);

if (!count($children) && !count($attributes) && !count($namespaces)) {
return [$name => $element->textContent];
return [$name => $element->textContent ?? ''];
}

return [
$name => filter(
merge(
$namespaces,
$attributes,
$children ?: ['@value' => $element->textContent]
$children ?: ['@value' => $element->textContent ?? '']
),
static fn (mixed $data): bool => $data !== []
)
Expand Down
51 changes: 41 additions & 10 deletions stubs/DOM.phpstub
Original file line number Diff line number Diff line change
Expand Up @@ -1027,6 +1027,9 @@ namespace

namespace DOM
{

use Psl\Iter\Iterator;

/**
* @var int
* @cvalue INDEX_SIZE_ERR
Expand Down Expand Up @@ -1163,7 +1166,10 @@ namespace DOM
public ?Element $parentElement;
/** @implementation-alias DOMNode::hasChildNodes */
public function hasChildNodes(): bool {}
/** @readonly */
/**
* @readonly
* @var NodeList<Node>
*/
public NodeList $childNodes;
/** @readonly */
public ?Node $firstChild;
Expand Down Expand Up @@ -1218,22 +1224,35 @@ namespace DOM
public function __wakeup(): void {}
}

class NodeList implements IteratorAggregate, Countable
/**
* @template-covariant TNode as \DOM\Node
* @template-implements \IteratorAggregate<int, TNode>
*/
class NodeList implements \IteratorAggregate, \Countable
{
/** @readonly */
public int $length;

/** @implementation-alias DOMNodeList::count */
public function count(): int {}

/** @implementation-alias DOMNodeList::getIterator */
/**
* @implementation-alias DOMNodeList::getIterator
* @psalm-return iterable<int, TNode>
*/
public function getIterator(): \Iterator {}

/** @implementation-alias DOMNodeList::item */
/**
* @implementation-alias DOMNodeList::item
* @psalm-return TNode
*/
public function item(int $index): ?Node {}
}

class NamedNodeMap implements IteratorAggregate, Countable
/**
* @implements \IteratorAggregate<array-key, \DOM\Attr>
*/
class NamedNodeMap implements \IteratorAggregate, \Countable
{
/** @readonly */
public int $length;
Expand All @@ -1248,11 +1267,14 @@ namespace DOM
/** @implementation-alias DOMNamedNodeMap::count */
public function count(): int {}

/** @implementation-alias DOMNamedNodeMap::getIterator */
/**
* @implementation-alias DOMNamedNodeMap::getIterator
* @psalm-return iterable<array-key, \DOM\Attr>
*/
public function getIterator(): \Iterator {}
}

class DTDNamedNodeMap implements IteratorAggregate, Countable
class DTDNamedNodeMap implements \IteratorAggregate, \Countable
{
/** @readonly */
public int $length;
Expand All @@ -1271,7 +1293,10 @@ namespace DOM
public function getIterator(): \Iterator {}
}

class HTMLCollection implements IteratorAggregate, Countable
/**
* @implements \IteratorAggregate<array-key, \DOM\Element>
*/
class HTMLCollection implements \IteratorAggregate, \Countable
{
/** @readonly */
public int $length;
Expand All @@ -1284,7 +1309,10 @@ namespace DOM
/** @implementation-alias DOMNodeList::count */
public function count(): int {}

/** @implementation-alias DOMNodeList::getIterator */
/**
* @implementation-alias DOMNodeList::getIterator
* @psalm-return iterable<array-key, \DOM\Element>
*/
public function getIterator(): \Iterator {}
}

Expand Down Expand Up @@ -1530,7 +1558,10 @@ namespace DOM
public Implementation $implementation;
/** @readonly */
public string $URL;
/** @readonly */
/**
* @changed by VeeWee:
* @not-readonly
*/
public string $documentURI;
public string $characterSet;
public string $charset;
Expand Down
131 changes: 131 additions & 0 deletions stubs/php_xsl.stub.phpstub
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php

/** @generate-class-entries */

/** @var int */
const XSL_CLONE_AUTO = 0;
/** @var int */
const XSL_CLONE_NEVER = -1;
/** @var int */
const XSL_CLONE_ALWAYS = 1;

/**
* @var int
* @cvalue XSL_SECPREF_NONE
*/
const XSL_SECPREF_NONE = UNKNOWN;
/**
* @var int
* @cvalue XSL_SECPREF_READ_FILE
*/
const XSL_SECPREF_READ_FILE = UNKNOWN;
/**
* @var int
* @cvalue XSL_SECPREF_WRITE_FILE
*/
const XSL_SECPREF_WRITE_FILE = UNKNOWN;
/**
* @var int
* @cvalue XSL_SECPREF_CREATE_DIRECTORY
*/
const XSL_SECPREF_CREATE_DIRECTORY = UNKNOWN;
/**
* @var int
* @cvalue XSL_SECPREF_READ_NETWORK
*/
const XSL_SECPREF_READ_NETWORK = UNKNOWN;
/**
* @var int
* @cvalue XSL_SECPREF_WRITE_NETWORK
*/
const XSL_SECPREF_WRITE_NETWORK = UNKNOWN;
/**
* @var int
* @cvalue XSL_SECPREF_DEFAULT
*/
const XSL_SECPREF_DEFAULT = UNKNOWN;

/**
* @var int
* @cvalue LIBXSLT_VERSION
*/
const LIBXSLT_VERSION = UNKNOWN;
/**
* @var string
* @cvalue LIBXSLT_DOTTED_VERSION
*/
const LIBXSLT_DOTTED_VERSION = UNKNOWN;

#ifdef HAVE_XSL_EXSLT
/**
* @var int
* @cvalue LIBEXSLT_VERSION
*/
const LIBEXSLT_VERSION = UNKNOWN;
/**
* @var string
* @cvalue LIBEXSLT_DOTTED_VERSION
*/
const LIBEXSLT_DOTTED_VERSION = UNKNOWN;
#endif

class XSLTProcessor
{
public bool $doXInclude = false;

public bool $cloneDocument = false;

public int $maxTemplateDepth;

public int $maxTemplateVars;

/**
* @param DOMDocument|DOM\Document|SimpleXMLElement $stylesheet
* @tentative-return-type
*/
public function importStylesheet(object $stylesheet): bool {}

/**
* @param DOMDocument|DOM\Document|SimpleXMLElement $document
* @tentative-return-type
*/
public function transformToDoc(object $document, ?string $returnClass = null): object|false {}

/**
* @param DOMDocument|DOM\Document|SimpleXMLElement $document
* @tentative-return-type
*/
public function transformToUri(object $document, string $uri): int {}

/**
* @param DOMDocument|DOM\Document|SimpleXMLElement $document
* @tentative-return-type
*/
public function transformToXml(object $document): string|null|false {}

/** @tentative-return-type */
public function setParameter(string $namespace, array|string $name, ?string $value = null): bool {}

/** @tentative-return-type */
public function getParameter(string $namespace, string $name): string|false {}

/** @tentative-return-type */
public function removeParameter(string $namespace, string $name): bool {}

/** @tentative-return-type */
public function hasExsltSupport(): bool {}

/** @tentative-return-type */
public function registerPHPFunctions(array|string|null $functions = null): void {}

public function registerPHPFunctionNS(string $namespaceURI, string $name, callable $callable): void {}

/** @tentative-return-type */
public function setProfiling(?string $filename): true {}

/** @tentative-return-type */
public function setSecurityPrefs(int $preferences): int {}

/** @tentative-return-type */
public function getSecurityPrefs(): int {}
}

0 comments on commit ea92150

Please sign in to comment.