Skip to content

Commit 48ce42f

Browse files
committed
Merge branch 'release/2.0.5'
2 parents 988a0b0 + 48eb60a commit 48ce42f

8 files changed

+129
-27
lines changed

.php_cs renamed to .php-cs-fixer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
->exclude('vendor')
55
->in(__DIR__);
66

7-
return PhpCsFixer\Config::create()
7+
return (new PhpCsFixer\Config())
88
->setUsingCache(false)
99
->setRules(array(
1010
'@PhpCsFixer' => true,

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# CHANGELOG
22

3+
## 2.0.5 - 2022/03/24
4+
- Rename .php_cs to .php-cs-fixer.php
5+
- Add PHPStan
6+
- Review PHP annotations
7+
- Update badges
8+
39
## 2.0.4 - 2021/02/03
410
- Review .php_cs settings, apply PHP CS Fixer
511
- Change tests classes to final classes

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
55
[![License](https://poser.pugx.org/wsdltophp/domhandler/license)](https://packagist.org/packages/wsdltophp/domhandler)
66
[![Latest Stable Version](https://poser.pugx.org/wsdltophp/domhandler/version.png)](https://packagist.org/packages/wsdltophp/domhandler)
7-
[![Build Status](https://travis-ci.com/WsdlToPhp/DomHandler.svg)](https://travis-ci.com/github/WsdlToPhp/DomHandler)
7+
[![TeamCity build status](https://teamcity.mikael-delsol.fr/app/rest/builds/buildType:id:WsdlHandler_Build/statusIcon.svg)](https://github.com/WsdlToPhp/DomHandler)
88
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/WsdlToPhp/DomHandler/badges/quality-score.png)](https://scrutinizer-ci.com/g/WsdlToPhp/DomHandler/)
99
[![Code Coverage](https://scrutinizer-ci.com/g/WsdlToPhp/DomHandler/badges/coverage.png)](https://scrutinizer-ci.com/g/WsdlToPhp/DomHandler/)
1010
[![Total Downloads](https://poser.pugx.org/wsdltophp/domhandler/downloads)](https://packagist.org/packages/wsdltophp/domhandler)
1111
[![StyleCI](https://styleci.io/repos/87977980/shield)](https://styleci.io/repos/87977980)
12-
[![SensioLabsInsight](https://insight.sensiolabs.com/projects/6bac01d7-5243-4682-9264-8166407c8a30/mini.png)](https://insight.sensiolabs.com/projects/6bac01d7-5243-4682-9264-8166407c8a30)
12+
[![SymfonyInsight](https://insight.symfony.com/projects/93e73feb-60bc-4e99-a3ce-87e537c61fed/mini.svg)](https://insight.symfony.com/projects/93e73feb-60bc-4e99-a3ce-87e537c61fed)
1313

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

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
"ext-dom": "*"
2828
},
2929
"require-dev": {
30-
"friendsofphp/php-cs-fixer": "~2.0",
30+
"friendsofphp/php-cs-fixer": "~3.0",
31+
"phpstan/phpstan": "^1.4",
3132
"phpunit/phpunit": "^9"
3233
},
3334
"config": {
@@ -45,7 +46,8 @@
4546
},
4647
"scripts": {
4748
"test": "vendor/bin/phpunit",
48-
"lint": "vendor/bin/php-cs-fixer fix --ansi --diff --verbose"
49+
"lint": "vendor/bin/php-cs-fixer fix --ansi --diff --verbose",
50+
"phpstan": "vendor/bin/phpstan analyze src --level=7"
4951
},
5052
"support": {
5153
"email": "[email protected]"

phpstan.neon.dist

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
parameters:
2+
treatPhpDocTypesAsCertain: false
3+
4+
ignoreErrors:
5+
-
6+
message: "#^Method WsdlToPhp\\\\DomHandler\\\\AbstractAttributeHandler\\:\\:getAttribute\\(\\) should return DOMAttr but returns DOMNode\\.$#"
7+
count: 1
8+
path: src/AbstractAttributeHandler.php
9+
10+
-
11+
message: "#^Method WsdlToPhp\\\\DomHandler\\\\AbstractElementHandler\\:\\:getAttribute\\(\\) should return WsdlToPhp\\\\DomHandler\\\\AttributeHandler\\|null but returns WsdlToPhp\\\\DomHandler\\\\AbstractNodeHandler\\|null\\.$#"
12+
count: 1
13+
path: src/AbstractElementHandler.php
14+
15+
-
16+
message: "#^Method WsdlToPhp\\\\DomHandler\\\\AbstractElementHandler\\:\\:getElement\\(\\) should return DOMElement but returns DOMNode\\.$#"
17+
count: 1
18+
path: src/AbstractElementHandler.php
19+
20+
-
21+
message: "#^Method WsdlToPhp\\\\DomHandler\\\\AbstractElementHandler\\:\\:getChildByNameAndAttributes\\(\\) should return WsdlToPhp\\\\DomHandler\\\\ElementHandler\\|null but returns WsdlToPhp\\\\DomHandler\\\\AbstractNodeHandler\\|null\\.$#"
22+
count: 1
23+
path: src/AbstractElementHandler.php
24+
25+
-
26+
message: "#^Strict comparison using === between 1 and mixed will always evaluate to false.$#"
27+
count: 1
28+
path: src/AbstractAttributeHandler.php
29+
30+
-
31+
message: "#^Method WsdlToPhp\\\\DomHandler\\\\AbstractDomDocumentHandler\\:\\:getElementByNameAndAttributes\\(\\) should return WsdlToPhp\\\\DomHandler\\\\ElementHandler\\|null but returns WsdlToPhp\\\\DomHandler\\\\AbstractNodeHandler\\|null\\.$#"
32+
count: 1
33+
path: src/AbstractDomDocumentHandler.php

src/AbstractDomDocumentHandler.php

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ public function __construct(DOMDocument $domDocument)
2323
$this->initRootElement();
2424
}
2525

26+
/**
27+
* @param DOMAttr|DOMElement|DOMNode $node
28+
*/
2629
public function getHandler($node, int $index = -1): AbstractNodeHandler
2730
{
2831
if ($node instanceof DOMElement) {
@@ -53,6 +56,9 @@ public function getElementByName(string $name): ?ElementHandler
5356
return null;
5457
}
5558

59+
/**
60+
* @return AbstractAttributeHandler[]|AbstractElementHandler[]|AbstractNodeHandler[]
61+
*/
5662
public function getNodesByName(string $name, ?string $checkInstance = null): array
5763
{
5864
$nodes = [];
@@ -67,42 +73,64 @@ public function getNodesByName(string $name, ?string $checkInstance = null): arr
6773
return $nodes;
6874
}
6975

76+
/**
77+
* @return AbstractAttributeHandler[]|AbstractElementHandler[]|AbstractNodeHandler[]
78+
*/
7079
public function getElementsByName(string $name): array
7180
{
7281
return $this->getNodesByName($name, DOMElement::class);
7382
}
7483

84+
/**
85+
* @param string[] $attributes
86+
*
87+
* @return AbstractAttributeHandler[]|AbstractElementHandler[]|AbstractNodeHandler[]
88+
*/
7589
public function getElementsByNameAndAttributes(string $name, array $attributes, ?DOMNode $node = null): array
7690
{
7791
$matchingElements = $this->getElementsByName($name);
7892
if ((!empty($attributes) || $node instanceof DOMNode) && !empty($matchingElements)) {
7993
$nodes = $this->searchTagsByXpath($name, $attributes, $node);
8094

81-
if (!empty($nodes)) {
95+
if ($nodes && 0 < $nodes->count()) {
8296
$matchingElements = $this->getElementsHandlers($nodes);
8397
}
8498
}
8599

86100
return $matchingElements;
87101
}
88102

103+
/**
104+
* @param DOMNodeList<DOMAttr|DOMElement|DOMNode> $nodeList
105+
*
106+
* @return AbstractElementHandler[]
107+
*/
89108
public function getElementsHandlers(DOMNodeList $nodeList): array
90109
{
91110
$nodes = [];
92-
if (!empty($nodeList)) {
93-
$index = 0;
94-
foreach ($nodeList as $node) {
95-
if ($node instanceof DOMElement) {
96-
$nodes[] = $this->getElementHandler($node, $this, $index);
97-
++$index;
98-
}
111+
if (0 === $nodeList->count()) {
112+
return $nodes;
113+
}
114+
115+
$index = 0;
116+
foreach ($nodeList as $node) {
117+
if (!$node instanceof DOMElement) {
118+
continue;
99119
}
120+
121+
$nodes[] = $this->getElementHandler($node, $this, $index);
122+
++$index;
100123
}
101124

102125
return $nodes;
103126
}
104127

105-
public function searchTagsByXpath(string $name, array $attributes, ?DOMNode $node = null): DOMNodeList
128+
/**
129+
* @param string[] $attributes
130+
*
131+
* @return DOMNodeList<DOMAttr|DOMElement|DOMNode>|false
132+
*/
133+
public function searchTagsByXpath(string $name, array $attributes, ?DOMNode $node = null)
106134
{
107135
$xpath = new DOMXPath($node ? $node->ownerDocument : $this->domDocument);
108136
$xQuery = sprintf("%s//*[local-name()='%s']", $node instanceof DOMNode ? '.' : '', $name);
@@ -117,6 +145,9 @@ public function searchTagsByXpath(string $name, array $attributes, ?DOMNode $nod
117145
return $xpath->query($xQuery, $node);
118146
}
119147

148+
/**
149+
* @param string[] $attributes
150+
*/
120151
public function getElementByNameAndAttributes(string $name, array $attributes): ?ElementHandler
121152
{
122153
$elements = $this->getElementsByNameAndAttributes($name, $attributes);
@@ -129,7 +160,7 @@ public function getElementByNameAndAttributes(string $name, array $attributes):
129160
*
130161
* @throws InvalidArgumentException
131162
*/
132-
protected function initRootElement()
163+
protected function initRootElement(): void
133164
{
134165
if ($this->domDocument->hasChildNodes()) {
135166
foreach ($this->domDocument->childNodes as $node) {

src/AbstractElementHandler.php

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,12 @@ public function hasAttribute(string $name): bool
2525

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

31+
/**
32+
* @return null|bool|mixed|string
33+
*/
3134
public function getAttributeValue(string $name, bool $withNamespace = false, bool $withinItsType = true, ?string $asType = AbstractAttributeHandler::DEFAULT_VALUE_TYPE)
3235
{
3336
$value = null;
@@ -39,38 +42,50 @@ public function getAttributeValue(string $name, bool $withNamespace = false, boo
3942
return $value;
4043
}
4144

45+
/**
46+
* @return array<int, AbstractElementHandler|AbstractNodeHandler>
47+
*/
4248
public function getChildrenByName(string $name): array
4349
{
4450
$children = [];
45-
if ($this->hasChildren()) {
46-
foreach ($this->getElement()->getElementsByTagName($name) as $index => $node) {
47-
$children[] = $this->getDomDocumentHandler()->getHandler($node, $index);
48-
}
51+
52+
if (!$this->hasChildren()) {
53+
return $children;
54+
}
55+
56+
foreach ($this->getElement()->getElementsByTagName($name) as $index => $node) {
57+
$children[] = $this->getDomDocumentHandler()->getHandler($node, $index);
4958
}
5059

5160
return $children;
5261
}
5362

63+
/**
64+
* @return AbstractElementHandler[]
65+
*/
5466
public function getElementChildren(): array
5567
{
56-
$children = [];
57-
if ($this->hasChildren()) {
58-
$children = $this->getDomDocumentHandler()->getElementsHandlers($this->getChildNodes());
59-
}
60-
61-
return $children;
68+
return $this->hasChildren() ? $this->getDomDocumentHandler()->getElementsHandlers($this->getChildNodes()) : [];
6269
}
6370

71+
/**
72+
* @param string[] $attributes
73+
*
74+
* @return AbstractAttributeHandler[]|AbstractElementHandler[]|AbstractNodeHandler[]
75+
*/
6476
public function getChildrenByNameAndAttributes(string $name, array $attributes): array
6577
{
6678
return $this->getDomDocumentHandler()->getElementsByNameAndAttributes($name, $attributes, $this->getNode());
6779
}
6880

81+
/**
82+
* @param string[] $attributes
83+
*/
6984
public function getChildByNameAndAttributes(string $name, array $attributes): ?ElementHandler
7085
{
7186
$children = $this->getChildrenByNameAndAttributes($name, $attributes);
7287

73-
return empty($children) ? null : array_shift($children);
88+
return array_shift($children);
7489
}
7590

7691
/**

src/AbstractNodeHandler.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace WsdlToPhp\DomHandler;
66

7+
use DOMElement;
78
use DOMNode;
89
use DOMNodeList;
910
use Traversable;
@@ -28,6 +29,9 @@ public function getNode(): DOMNode
2829
return $this->node;
2930
}
3031

32+
/**
33+
* @return DOMNodeList<DOMElement|DOMNode>
34+
*/
3135
public function getChildNodes(): DOMNodeList
3236
{
3337
return $this->getNode()->childNodes;
@@ -77,6 +81,9 @@ public function hasAttributes(): bool
7781
return $this->getNode()->hasAttributes();
7882
}
7983

84+
/**
85+
* @return AbstractAttributeHandler[]|AbstractNodeHandler[]
86+
*/
8087
public function getAttributes(): array
8188
{
8289
return $this->getHandlers($this->getNode()->attributes);
@@ -87,6 +94,9 @@ public function hasChildren(): bool
8794
return $this->getNode()->hasChildNodes();
8895
}
8996

97+
/**
98+
* @return AbstractAttributeHandler[]|AbstractElementHandler[]|AbstractNodeHandler[]
99+
*/
90100
public function getChildren(): array
91101
{
92102
return $this->getHandlers($this->getNode()->childNodes);
@@ -126,6 +136,11 @@ public function getValueNamespace(): ?string
126136
return null;
127137
}
128138

139+
/**
140+
* @param null|Traversable<DOMNode> $nodes
141+
*
142+
* @return AbstractAttributeHandler[]|AbstractElementHandler[]|AbstractNodeHandler[]
143+
*/
129144
private function getHandlers(?Traversable $nodes): array
130145
{
131146
if (is_null($nodes)) {

0 commit comments

Comments
 (0)