Skip to content

Commit

Permalink
Merge branch 'release/2.0.5'
Browse files Browse the repository at this point in the history
  • Loading branch information
mikaelcom committed Mar 24, 2022
2 parents 988a0b0 + 48eb60a commit 48ce42f
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .php_cs → .php-cs-fixer.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
->exclude('vendor')
->in(__DIR__);

return PhpCsFixer\Config::create()
return (new PhpCsFixer\Config())
->setUsingCache(false)
->setRules(array(
'@PhpCsFixer' => true,
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# CHANGELOG

## 2.0.5 - 2022/03/24
- Rename .php_cs to .php-cs-fixer.php
- Add PHPStan
- Review PHP annotations
- Update badges

## 2.0.4 - 2021/02/03
- Review .php_cs settings, apply PHP CS Fixer
- Change tests classes to final classes
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
[![License](https://poser.pugx.org/wsdltophp/domhandler/license)](https://packagist.org/packages/wsdltophp/domhandler)
[![Latest Stable Version](https://poser.pugx.org/wsdltophp/domhandler/version.png)](https://packagist.org/packages/wsdltophp/domhandler)
[![Build Status](https://travis-ci.com/WsdlToPhp/DomHandler.svg)](https://travis-ci.com/github/WsdlToPhp/DomHandler)
[![TeamCity build status](https://teamcity.mikael-delsol.fr/app/rest/builds/buildType:id:WsdlHandler_Build/statusIcon.svg)](https://github.com/WsdlToPhp/DomHandler)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/WsdlToPhp/DomHandler/badges/quality-score.png)](https://scrutinizer-ci.com/g/WsdlToPhp/DomHandler/)
[![Code Coverage](https://scrutinizer-ci.com/g/WsdlToPhp/DomHandler/badges/coverage.png)](https://scrutinizer-ci.com/g/WsdlToPhp/DomHandler/)
[![Total Downloads](https://poser.pugx.org/wsdltophp/domhandler/downloads)](https://packagist.org/packages/wsdltophp/domhandler)
[![StyleCI](https://styleci.io/repos/87977980/shield)](https://styleci.io/repos/87977980)
[![SensioLabsInsight](https://insight.sensiolabs.com/projects/6bac01d7-5243-4682-9264-8166407c8a30/mini.png)](https://insight.sensiolabs.com/projects/6bac01d7-5243-4682-9264-8166407c8a30)
[![SymfonyInsight](https://insight.symfony.com/projects/93e73feb-60bc-4e99-a3ce-87e537c61fed/mini.svg)](https://insight.symfony.com/projects/93e73feb-60bc-4e99-a3ce-87e537c61fed)

DomHandler uses the [decorator design pattern](https://en.wikipedia.org/wiki/Decorator_pattern) in order to ease DOM handling.

Expand Down
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"ext-dom": "*"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "~2.0",
"friendsofphp/php-cs-fixer": "~3.0",
"phpstan/phpstan": "^1.4",
"phpunit/phpunit": "^9"
},
"config": {
Expand All @@ -45,7 +46,8 @@
},
"scripts": {
"test": "vendor/bin/phpunit",
"lint": "vendor/bin/php-cs-fixer fix --ansi --diff --verbose"
"lint": "vendor/bin/php-cs-fixer fix --ansi --diff --verbose",
"phpstan": "vendor/bin/phpstan analyze src --level=7"
},
"support": {
"email": "[email protected]"
Expand Down
33 changes: 33 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
parameters:
treatPhpDocTypesAsCertain: false

ignoreErrors:
-
message: "#^Method WsdlToPhp\\\\DomHandler\\\\AbstractAttributeHandler\\:\\:getAttribute\\(\\) should return DOMAttr but returns DOMNode\\.$#"
count: 1
path: src/AbstractAttributeHandler.php

-
message: "#^Method WsdlToPhp\\\\DomHandler\\\\AbstractElementHandler\\:\\:getAttribute\\(\\) should return WsdlToPhp\\\\DomHandler\\\\AttributeHandler\\|null but returns WsdlToPhp\\\\DomHandler\\\\AbstractNodeHandler\\|null\\.$#"
count: 1
path: src/AbstractElementHandler.php

-
message: "#^Method WsdlToPhp\\\\DomHandler\\\\AbstractElementHandler\\:\\:getElement\\(\\) should return DOMElement but returns DOMNode\\.$#"
count: 1
path: src/AbstractElementHandler.php

-
message: "#^Method WsdlToPhp\\\\DomHandler\\\\AbstractElementHandler\\:\\:getChildByNameAndAttributes\\(\\) should return WsdlToPhp\\\\DomHandler\\\\ElementHandler\\|null but returns WsdlToPhp\\\\DomHandler\\\\AbstractNodeHandler\\|null\\.$#"
count: 1
path: src/AbstractElementHandler.php

-
message: "#^Strict comparison using === between 1 and mixed will always evaluate to false.$#"
count: 1
path: src/AbstractAttributeHandler.php

-
message: "#^Method WsdlToPhp\\\\DomHandler\\\\AbstractDomDocumentHandler\\:\\:getElementByNameAndAttributes\\(\\) should return WsdlToPhp\\\\DomHandler\\\\ElementHandler\\|null but returns WsdlToPhp\\\\DomHandler\\\\AbstractNodeHandler\\|null\\.$#"
count: 1
path: src/AbstractDomDocumentHandler.php
51 changes: 41 additions & 10 deletions src/AbstractDomDocumentHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public function __construct(DOMDocument $domDocument)
$this->initRootElement();
}

/**
* @param DOMAttr|DOMElement|DOMNode $node
*/
public function getHandler($node, int $index = -1): AbstractNodeHandler
{
if ($node instanceof DOMElement) {
Expand Down Expand Up @@ -53,6 +56,9 @@ public function getElementByName(string $name): ?ElementHandler
return null;
}

/**
* @return AbstractAttributeHandler[]|AbstractElementHandler[]|AbstractNodeHandler[]
*/
public function getNodesByName(string $name, ?string $checkInstance = null): array
{
$nodes = [];
Expand All @@ -67,42 +73,64 @@ public function getNodesByName(string $name, ?string $checkInstance = null): arr
return $nodes;
}

/**
* @return AbstractAttributeHandler[]|AbstractElementHandler[]|AbstractNodeHandler[]
*/
public function getElementsByName(string $name): array
{
return $this->getNodesByName($name, DOMElement::class);
}

/**
* @param string[] $attributes
*
* @return AbstractAttributeHandler[]|AbstractElementHandler[]|AbstractNodeHandler[]
*/
public function getElementsByNameAndAttributes(string $name, array $attributes, ?DOMNode $node = null): array
{
$matchingElements = $this->getElementsByName($name);
if ((!empty($attributes) || $node instanceof DOMNode) && !empty($matchingElements)) {
$nodes = $this->searchTagsByXpath($name, $attributes, $node);

if (!empty($nodes)) {
if ($nodes && 0 < $nodes->count()) {
$matchingElements = $this->getElementsHandlers($nodes);
}
}

return $matchingElements;
}

/**
* @param DOMNodeList<DOMAttr|DOMElement|DOMNode> $nodeList
*
* @return AbstractElementHandler[]
*/
public function getElementsHandlers(DOMNodeList $nodeList): array
{
$nodes = [];
if (!empty($nodeList)) {
$index = 0;
foreach ($nodeList as $node) {
if ($node instanceof DOMElement) {
$nodes[] = $this->getElementHandler($node, $this, $index);
++$index;
}
if (0 === $nodeList->count()) {
return $nodes;
}

$index = 0;
foreach ($nodeList as $node) {
if (!$node instanceof DOMElement) {
continue;
}

$nodes[] = $this->getElementHandler($node, $this, $index);
++$index;
}

return $nodes;
}

public function searchTagsByXpath(string $name, array $attributes, ?DOMNode $node = null): DOMNodeList
/**
* @param string[] $attributes
*
* @return DOMNodeList<DOMAttr|DOMElement|DOMNode>|false
*/
public function searchTagsByXpath(string $name, array $attributes, ?DOMNode $node = null)
{
$xpath = new DOMXPath($node ? $node->ownerDocument : $this->domDocument);
$xQuery = sprintf("%s//*[local-name()='%s']", $node instanceof DOMNode ? '.' : '', $name);
Expand All @@ -117,6 +145,9 @@ public function searchTagsByXpath(string $name, array $attributes, ?DOMNode $nod
return $xpath->query($xQuery, $node);
}

/**
* @param string[] $attributes
*/
public function getElementByNameAndAttributes(string $name, array $attributes): ?ElementHandler
{
$elements = $this->getElementsByNameAndAttributes($name, $attributes);
Expand All @@ -129,7 +160,7 @@ public function getElementByNameAndAttributes(string $name, array $attributes):
*
* @throws InvalidArgumentException
*/
protected function initRootElement()
protected function initRootElement(): void
{
if ($this->domDocument->hasChildNodes()) {
foreach ($this->domDocument->childNodes as $node) {
Expand Down
39 changes: 27 additions & 12 deletions src/AbstractElementHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ public function hasAttribute(string $name): bool

public function getAttribute(string $name): ?AttributeHandler
{
return $this->hasAttribute($name) ? $this->getDomDocumentHandler()->getHandler($this->getNode()->getAttributeNode($name)) : null;
return $this->hasAttribute($name) ? $this->getDomDocumentHandler()->getHandler($this->getElement()->getAttributeNode($name)) : null;
}

/**
* @return null|bool|mixed|string
*/
public function getAttributeValue(string $name, bool $withNamespace = false, bool $withinItsType = true, ?string $asType = AbstractAttributeHandler::DEFAULT_VALUE_TYPE)
{
$value = null;
Expand All @@ -39,38 +42,50 @@ public function getAttributeValue(string $name, bool $withNamespace = false, boo
return $value;
}

/**
* @return array<int, AbstractElementHandler|AbstractNodeHandler>
*/
public function getChildrenByName(string $name): array
{
$children = [];
if ($this->hasChildren()) {
foreach ($this->getElement()->getElementsByTagName($name) as $index => $node) {
$children[] = $this->getDomDocumentHandler()->getHandler($node, $index);
}

if (!$this->hasChildren()) {
return $children;
}

foreach ($this->getElement()->getElementsByTagName($name) as $index => $node) {
$children[] = $this->getDomDocumentHandler()->getHandler($node, $index);
}

return $children;
}

/**
* @return AbstractElementHandler[]
*/
public function getElementChildren(): array
{
$children = [];
if ($this->hasChildren()) {
$children = $this->getDomDocumentHandler()->getElementsHandlers($this->getChildNodes());
}

return $children;
return $this->hasChildren() ? $this->getDomDocumentHandler()->getElementsHandlers($this->getChildNodes()) : [];
}

/**
* @param string[] $attributes
*
* @return AbstractAttributeHandler[]|AbstractElementHandler[]|AbstractNodeHandler[]
*/
public function getChildrenByNameAndAttributes(string $name, array $attributes): array
{
return $this->getDomDocumentHandler()->getElementsByNameAndAttributes($name, $attributes, $this->getNode());
}

/**
* @param string[] $attributes
*/
public function getChildByNameAndAttributes(string $name, array $attributes): ?ElementHandler
{
$children = $this->getChildrenByNameAndAttributes($name, $attributes);

return empty($children) ? null : array_shift($children);
return array_shift($children);
}

/**
Expand Down
15 changes: 15 additions & 0 deletions src/AbstractNodeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace WsdlToPhp\DomHandler;

use DOMElement;
use DOMNode;
use DOMNodeList;
use Traversable;
Expand All @@ -28,6 +29,9 @@ public function getNode(): DOMNode
return $this->node;
}

/**
* @return DOMNodeList<DOMElement|DOMNode>
*/
public function getChildNodes(): DOMNodeList
{
return $this->getNode()->childNodes;
Expand Down Expand Up @@ -77,6 +81,9 @@ public function hasAttributes(): bool
return $this->getNode()->hasAttributes();
}

/**
* @return AbstractAttributeHandler[]|AbstractNodeHandler[]
*/
public function getAttributes(): array
{
return $this->getHandlers($this->getNode()->attributes);
Expand All @@ -87,6 +94,9 @@ public function hasChildren(): bool
return $this->getNode()->hasChildNodes();
}

/**
* @return AbstractAttributeHandler[]|AbstractElementHandler[]|AbstractNodeHandler[]
*/
public function getChildren(): array
{
return $this->getHandlers($this->getNode()->childNodes);
Expand Down Expand Up @@ -126,6 +136,11 @@ public function getValueNamespace(): ?string
return null;
}

/**
* @param null|Traversable<DOMNode> $nodes
*
* @return AbstractAttributeHandler[]|AbstractElementHandler[]|AbstractNodeHandler[]
*/
private function getHandlers(?Traversable $nodes): array
{
if (is_null($nodes)) {
Expand Down

0 comments on commit 48ce42f

Please sign in to comment.