Skip to content

Commit 22bd76b

Browse files
committed
Initialize SonarCloud
Apply PHP CS Fixer
1 parent 26cb25c commit 22bd76b

15 files changed

+190
-178
lines changed

.github/workflows/sonarcloud.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: SonarCloud
2+
on:
3+
push:
4+
branches:
5+
- develop
6+
- feature/*
7+
- feat/*
8+
pull_request:
9+
types: [ opened, synchronize, reopened ]
10+
jobs:
11+
sonarcloud:
12+
name: SonarCloud
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/checkout@v2
16+
with:
17+
fetch-depth: 0
18+
19+
- name: Setup PHP with Xdebug
20+
uses: shivammathur/setup-php@v2
21+
with:
22+
php-version: 7.4
23+
coverage: xdebug
24+
25+
- name: Install dependencies with composer
26+
run: composer update --no-ansi --no-interaction --no-progress
27+
28+
- name: Generate coverage report with phpunit/phpunit
29+
run: vendor/bin/phpunit --coverage-clover coverage.xml --log-junit report.xml
30+
31+
- name: Fix phpunit files paths
32+
run: sed -i 's@'$GITHUB_WORKSPACE/'@''@g' coverage.xml report.xml
33+
34+
- name: SonarCloud Scan
35+
uses: SonarSource/sonarcloud-github-action@master
36+
env:
37+
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}

phpstan.neon.dist

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,2 @@
11
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
2+
treatPhpDocTypesAsCertain: false

phpunit.xml.dist

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" backupGlobals="false" colors="true" bootstrap="vendor/autoload.php">
2+
<phpunit
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
5+
backupGlobals="false"
6+
colors="true"
7+
bootstrap="vendor/autoload.php"
8+
>
39
<coverage>
410
<include>
5-
<directory>./</directory>
11+
<directory suffix=".php">src</directory>
612
</include>
7-
<exclude>
8-
<directory>./tests</directory>
9-
<directory>./vendor</directory>
10-
</exclude>
1113
</coverage>
1214
<php>
1315
<ini name="error_reporting" value="-1"/>

sonar-project.properties

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
sonar.projectKey=WsdlToPhp_DomHandler
2+
sonar.organization=wsdltophp
3+
sonar.php.coverage.reportPaths=coverage.xml
4+
sonar.php.tests.reportPath=report.xml
5+
6+
# This is the name and version displayed in the SonarCloud UI.
7+
#sonar.projectName=DomHandler
8+
#sonar.projectVersion=1.0
9+
10+
# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
11+
#sonar.sources=.
12+
13+
# Encoding of the source code. Default is default system encoding
14+
#sonar.sourceEncoding=UTF-8

src/AbstractAttributeHandler.php

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

55
namespace WsdlToPhp\DomHandler;
66

7-
use DOMAttr;
8-
97
class AbstractAttributeHandler extends AbstractNodeHandler
108
{
119
public const DEFAULT_VALUE_TYPE = 'string';
@@ -37,9 +35,9 @@ class AbstractAttributeHandler extends AbstractNodeHandler
3735

3836
public const DEFAULT_OCCURRENCE_VALUE = 1;
3937

40-
public function getAttribute(): DOMAttr
38+
public function getAttribute(): ?\DOMAttr
4139
{
42-
return $this->getNode();
40+
return $this->getNode() instanceof \DOMAttr ? $this->getNode() : null;
4341
}
4442

4543
/**
@@ -82,8 +80,8 @@ public function getValueNamespace(): ?string
8280
/**
8381
* Returns the value with good type.
8482
*
85-
* @param mixed $value the value
86-
* @param null|string $knownType the value
83+
* @param null|bool|float|int|string $value the value
84+
* @param null|string $knownType the value expected type
8785
*
8886
* @return mixed
8987
*/
@@ -99,20 +97,20 @@ public static function getValueWithinItsType($value, ?string $knownType = null)
9997
'int',
10098
'integer',
10199
], true))) {
102-
return intval($value);
100+
return (int) $value;
103101
}
104102
if (is_float($value) || (!is_null($value) && in_array($knownType, [
105103
'float',
106104
'double',
107105
'decimal',
108106
], true))) {
109-
return floatval($value);
107+
return (float) $value;
110108
}
111109
if (is_bool($value) || (!is_null($value) && in_array($knownType, [
112110
'bool',
113111
'boolean',
114112
], true))) {
115-
return 'true' === $value || true === $value || 1 === $value || '1' === $value;
113+
return 'true' === $value || true === $value || '1' === $value;
116114
}
117115

118116
return $value;

src/AbstractDomDocumentHandler.php

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,32 @@
22

33
namespace WsdlToPhp\DomHandler;
44

5-
use DOMAttr;
6-
use DOMDocument;
75
use DOMElement;
8-
use DOMNameSpaceNode;
9-
use DOMNode;
10-
use DOMNodeList;
11-
use DOMXPath;
12-
use InvalidArgumentException;
136

147
abstract class AbstractDomDocumentHandler
158
{
16-
protected DOMDocument $domDocument;
9+
protected \DOMDocument $domDocument;
1710

1811
protected ?ElementHandler $rootElement;
1912

20-
public function __construct(DOMDocument $domDocument)
13+
public function __construct(\DOMDocument $domDocument)
2114
{
2215
$this->domDocument = $domDocument;
2316
$this->initRootElement();
2417
}
2518

2619
/**
27-
* @param DOMAttr|DOMElement|DOMNode|DOMNameSpaceNode $node
20+
* @param \DOMAttr|\DOMElement|\DOMNameSpaceNode|\DOMNode $node
2821
*/
2922
public function getHandler($node, int $index = -1): AbstractNodeHandler
3023
{
31-
if ($node instanceof DOMElement) {
24+
if ($node instanceof \DOMElement) {
3225
return $this->getElementHandler($node, $this, $index);
3326
}
34-
if ($node instanceof DOMAttr) {
27+
if ($node instanceof \DOMAttr) {
3528
return $this->getAttributeHandler($node, $this, $index);
3629
}
37-
if ($node instanceof DOMNameSpaceNode) {
30+
if ($node instanceof \DOMNameSpaceNode) {
3831
return new NameSpaceHandler($node, $this, $index);
3932
}
4033

@@ -49,7 +42,7 @@ public function getNodeByName(string $name): ?NodeHandler
4942
public function getElementByName(string $name): ?ElementHandler
5043
{
5144
$node = $this->getNodeByName($name);
52-
if ($node instanceof AbstractNodeHandler && $node->getNode() instanceof DOMElement) {
45+
if ($node instanceof AbstractNodeHandler && $node->getNode() instanceof \DOMElement) {
5346
return $this->getElementHandler($node->getNode(), $this);
5447
}
5548

@@ -78,18 +71,18 @@ public function getNodesByName(string $name, ?string $checkInstance = null): arr
7871
*/
7972
public function getElementsByName(string $name): array
8073
{
81-
return $this->getNodesByName($name, DOMElement::class);
74+
return $this->getNodesByName($name, \DOMElement::class);
8275
}
8376

8477
/**
8578
* @param string[] $attributes
8679
*
8780
* @return AbstractAttributeHandler[]|AbstractElementHandler[]|AbstractNodeHandler[]
8881
*/
89-
public function getElementsByNameAndAttributes(string $name, array $attributes, ?DOMNode $node = null): array
82+
public function getElementsByNameAndAttributes(string $name, array $attributes, ?\DOMNode $node = null): array
9083
{
9184
$matchingElements = $this->getElementsByName($name);
92-
if ((!empty($attributes) || $node instanceof DOMNode) && !empty($matchingElements)) {
85+
if ((!empty($attributes) || $node instanceof \DOMNode) && !empty($matchingElements)) {
9386
$nodes = $this->searchTagsByXpath($name, $attributes, $node);
9487

9588
if (false !== $nodes) {
@@ -101,11 +94,11 @@ public function getElementsByNameAndAttributes(string $name, array $attributes,
10194
}
10295

10396
/**
104-
* @param DOMNodeList<DOMAttr|DOMElement|DOMNode> $nodeList
97+
* @param \DOMNodeList<\DOMAttr|\DOMElement|\DOMNode> $nodeList
10598
*
10699
* @return AbstractElementHandler[]
107100
*/
108-
public function getElementsHandlers(DOMNodeList $nodeList): array
101+
public function getElementsHandlers(\DOMNodeList $nodeList): array
109102
{
110103
$nodes = [];
111104
if (0 === $nodeList->count()) {
@@ -114,7 +107,7 @@ public function getElementsHandlers(DOMNodeList $nodeList): array
114107

115108
$index = 0;
116109
foreach ($nodeList as $node) {
117-
if (!$node instanceof DOMElement) {
110+
if (!$node instanceof \DOMElement) {
118111
continue;
119112
}
120113

@@ -128,12 +121,12 @@ public function getElementsHandlers(DOMNodeList $nodeList): array
128121
/**
129122
* @param string[] $attributes
130123
*
131-
* @return DOMNodeList<DOMAttr|DOMElement|DOMNode>|false
124+
* @return \DOMNodeList<\DOMAttr|\DOMElement|\DOMNode>|false
132125
*/
133-
public function searchTagsByXpath(string $name, array $attributes, ?DOMNode $node = null)
126+
public function searchTagsByXpath(string $name, array $attributes, ?\DOMNode $node = null)
134127
{
135-
$xpath = new DOMXPath($node ? $node->ownerDocument : $this->domDocument);
136-
$xQuery = sprintf("%s//*[local-name()='%s']", $node instanceof DOMNode ? '.' : '', $name);
128+
$xpath = new \DOMXPath($node ? $node->ownerDocument : $this->domDocument);
129+
$xQuery = sprintf("%s//*[local-name()='%s']", $node instanceof \DOMNode ? '.' : '', $name);
137130
foreach ($attributes as $attributeName => $attributeValue) {
138131
if (false !== strpos($attributeValue, '*')) {
139132
$xQuery .= sprintf("[contains(@%s, '%s')]", $attributeName, str_replace('*', '', $attributeValue));
@@ -152,32 +145,34 @@ public function getElementByNameAndAttributes(string $name, array $attributes):
152145
{
153146
$elements = $this->getElementsByNameAndAttributes($name, $attributes);
154147

155-
return array_shift($elements);
148+
$element = array_shift($elements);
149+
150+
return $element instanceof ElementHandler ? $element : null;
156151
}
157152

158153
/**
159154
* Find valid root node (not a comment, at least a DOMElement node).
160155
*
161-
* @throws InvalidArgumentException
156+
* @throws \InvalidArgumentException
162157
*/
163158
protected function initRootElement(): void
164159
{
165160
if ($this->domDocument->hasChildNodes()) {
166161
foreach ($this->domDocument->childNodes as $node) {
167-
if ($node instanceof DOMElement) {
162+
if ($node instanceof \DOMElement) {
168163
$this->rootElement = $this->getElementHandler($node, $this);
169164

170165
break;
171166
}
172167
}
173168
} else {
174-
throw new InvalidArgumentException('Document seems to be invalid', __LINE__);
169+
throw new \InvalidArgumentException('Document seems to be invalid', __LINE__);
175170
}
176171
}
177172

178-
abstract protected function getNodeHandler(DOMNode $node, AbstractDomDocumentHandler $domDocument, int $index = -1): NodeHandler;
173+
abstract protected function getNodeHandler(\DOMNode $node, AbstractDomDocumentHandler $domDocument, int $index = -1): NodeHandler;
179174

180-
abstract protected function getElementHandler(DOMElement $element, AbstractDomDocumentHandler $domDocument, int $index = -1): ElementHandler;
175+
abstract protected function getElementHandler(\DOMElement $element, AbstractDomDocumentHandler $domDocument, int $index = -1): ElementHandler;
181176

182-
abstract protected function getAttributeHandler(DOMAttr $attribute, AbstractDomDocumentHandler $domDocument, int $index = -1): AttributeHandler;
177+
abstract protected function getAttributeHandler(\DOMAttr $attribute, AbstractDomDocumentHandler $domDocument, int $index = -1): AttributeHandler;
183178
}

src/AbstractElementHandler.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,32 @@
44

55
namespace WsdlToPhp\DomHandler;
66

7-
use DOMElement;
8-
97
abstract class AbstractElementHandler extends AbstractNodeHandler
108
{
11-
public function __construct(DOMElement $element, AbstractDomDocumentHandler $domDocument, int $index = -1)
9+
public function __construct(\DOMElement $element, AbstractDomDocumentHandler $domDocument, int $index = -1)
1210
{
1311
parent::__construct($element, $domDocument, $index);
1412
}
1513

16-
public function getElement(): DOMElement
14+
public function getElement(): ?\DOMElement
1715
{
18-
return $this->getNode();
16+
return $this->getNode() instanceof \DOMElement ? $this->getNode() : null;
1917
}
2018

2119
public function hasAttribute(string $name): bool
2220
{
23-
return $this->getElement()->hasAttribute($name);
21+
return $this->getElement() && $this->getElement()->hasAttribute($name);
2422
}
2523

2624
public function getAttribute(string $name): ?AttributeHandler
2725
{
28-
return $this->hasAttribute($name) ? $this->getDomDocumentHandler()->getHandler($this->getElement()->getAttributeNode($name)) : null;
26+
if (!$this->hasAttribute($name) || !$this->getElement()) {
27+
return null;
28+
}
29+
30+
$attribute = $this->getDomDocumentHandler()->getHandler($this->getElement()->getAttributeNode($name));
31+
32+
return $attribute instanceof AttributeHandler ? $attribute : null;
2933
}
3034

3135
/**
@@ -49,7 +53,7 @@ public function getChildrenByName(string $name): array
4953
{
5054
$children = [];
5155

52-
if (!$this->hasChildren()) {
56+
if (!$this->hasChildren() || !$this->getElement()) {
5357
return $children;
5458
}
5559

@@ -85,7 +89,9 @@ public function getChildByNameAndAttributes(string $name, array $attributes): ?E
8589
{
8690
$children = $this->getChildrenByNameAndAttributes($name, $attributes);
8791

88-
return array_shift($children);
92+
$child = array_shift($children);
93+
94+
return $child instanceof ElementHandler ? $child : null;
8995
}
9096

9197
/**

0 commit comments

Comments
 (0)