Skip to content
This repository was archived by the owner on Oct 12, 2024. It is now read-only.

Commit 786a96d

Browse files
authored
Merge pull request #6 from phpbench/2023-update
2023 update
2 parents b013b71 + a70c4ca commit 786a96d

File tree

9 files changed

+63
-50
lines changed

9 files changed

+63
-50
lines changed

.github/workflows/ci.yaml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
strategy:
2121
matrix:
2222
php-version:
23-
- '7.2'
23+
- '7.3'
2424

2525
steps:
2626
-
@@ -50,7 +50,7 @@ jobs:
5050
strategy:
5151
matrix:
5252
php-version:
53-
- '7.2'
53+
- '7.4'
5454

5555
steps:
5656
-
@@ -87,7 +87,7 @@ jobs:
8787
strategy:
8888
matrix:
8989
php-version:
90-
- '7.2'
90+
- '7.4'
9191

9292
steps:
9393
-
@@ -126,7 +126,6 @@ jobs:
126126
strategy:
127127
matrix:
128128
php-version:
129-
- '7.2'
130129
- '7.3'
131130
- '7.4'
132131
- '8.0'
@@ -136,7 +135,7 @@ jobs:
136135
with-examples: ['yes']
137136
allow-failures: [false]
138137
include:
139-
- php-version: '7.2'
138+
- php-version: '7.3'
140139
dependency: 'lowest'
141140
with-examples: 'no'
142141
allow-failures: false

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
composer.lock
22
vendor
33
.phpunit.result.cache
4-
.php_cs.cache
4+
.php-cs-fixer.cache

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
11
<?php
22

3-
$finder = PhpCsFixer\Finder::create()
3+
use PhpCsFixer\Finder;
4+
use PhpCsFixer\Config;
5+
6+
$finder = Finder::create()
47
->in([
58
__DIR__ . '/lib',
69
__DIR__ . '/tests',
710
])
8-
->exclude([
9-
'Attributes'
10-
])
1111
;
1212

13-
return PhpCsFixer\Config::create()
13+
return (new Config())
1414
->setRiskyAllowed(true)
1515
->setRules([
16-
'@PSR2' => true,
16+
'@PSR12' => true,
1717
'void_return' => true,
18-
'binary_operator_spaces' => [ 'align_double_arrow' => false ],
18+
'binary_operator_spaces' => [
19+
'operators' => [
20+
'=>' => null
21+
],
22+
],
1923
'blank_line_before_statement' => [
2024
'statements' => [
2125
'break',
2226
'continue',
2327
'declare',
2428
'default',
25-
'die',
2629
'do',
2730
'exit',
2831
'for',
@@ -41,11 +44,8 @@
4144
'yield',
4245
],
4346
],
44-
'ordered_imports' => true,
4547
'concat_space' => false,
46-
'method_argument_space' => false,
4748
'no_unused_imports' => true,
48-
'ordered_imports' => true,
4949
'php_unit_set_up_tear_down_visibility' => true,
5050
'phpdoc_align' => [],
5151
'phpdoc_indent' => false,

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
}
1010
],
1111
"require": {
12-
"php": "^7.2||^8.0",
12+
"php": "^7.3||^8.0",
1313
"ext-dom": "*"
1414
},
1515
"require-dev": {
16+
"friendsofphp/php-cs-fixer": "^3.14",
1617
"phpunit/phpunit": "^8.0||^9.0",
17-
"phpstan/phpstan": "^0.12.83",
18-
"friendsofphp/php-cs-fixer": "^2.18"
18+
"phpstan/phpstan": "^1.10"
1919
},
2020
"autoload": {
2121
"psr-4": {

lib/Document.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class Document extends \DOMDocument implements XPathAware
2727

2828
/**
2929
* @param string $version
30-
* @param mixed $encoding
30+
* @param string $encoding
3131
*/
3232
public function __construct($version = '1.0', $encoding = null)
3333
{
@@ -79,13 +79,13 @@ public function query($query, DOMNode $context = null): DOMNodeList
7979
/**
8080
* {@inheritdoc}
8181
*/
82-
public function queryOne($query, DOMNode $context = null)
82+
public function queryOne($query, DOMNode $context = null): ?Element
8383
{
8484
return $this->xpath()->queryOne($query, $context);
8585
}
8686

8787
/**
88-
* {@inheritdoc}
88+
* @return mixed
8989
*/
9090
public function evaluate($expression, DOMNode $context = null)
9191
{
@@ -99,7 +99,7 @@ public function evaluate($expression, DOMNode $context = null)
9999
public function dump(): string
100100
{
101101
$this->formatOutput = true;
102-
$result = $this->saveXml();
102+
$result = $this->saveXML();
103103
$this->formatOutput = false;
104104

105105
if (false === $result) {
@@ -112,9 +112,12 @@ public function dump(): string
112112
public function duplicate(): Document
113113
{
114114
$dom = new self();
115-
$firstChild = $dom->importNode($this->firstChild, true);
116115

117-
$dom->appendChild($firstChild);
116+
if ($this->firstChild) {
117+
$firstChild = $dom->importNode($this->firstChild, true);
118+
$dom->appendChild($firstChild);
119+
}
120+
118121

119122
return $dom;
120123
}

lib/Element.php

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,36 +21,26 @@ class Element extends \DOMElement implements XPathAware
2121
{
2222
/**
2323
* Create and append a text-node with the given name and value.
24-
*
25-
* @param string $name
26-
* @param string $value
27-
*
28-
* @return Element
2924
*/
30-
public function appendTextNode($name, $value)
25+
public function appendTextNode(string $name, ?string $value): Element
3126
{
3227
$el = new self($name);
3328
$element = $this->appendChild($el);
3429
assert($element instanceof Element);
35-
30+
3631
$element->appendChild(
37-
$this->owner()->createTextNode($value)
32+
$this->owner()->createTextNode($value ?? '')
3833
);
3934

4035
return $element;
4136
}
42-
37+
4338
/**
4439
* Create and append an element with the given name and optionally given value.
4540
*
4641
* Note: The value will not be escaped. Use DOMDocument::createTextNode() to create a text node with escaping support.
47-
*
48-
* @param string $name
49-
* @param mixed $value
50-
*
51-
* @return Element
5242
*/
53-
public function appendElement($name, $value = null)
43+
public function appendElement(string $name, ?string $value = null): Element
5444
{
5545
$element = $this->appendChild(new self($name, $value));
5646
assert($element instanceof Element);
@@ -66,13 +56,13 @@ public function query($xpath, DOMNode $context = null): DOMNodeList
6656
return $this->owner()->xpath()->query($xpath, $context ?: $this);
6757
}
6858

69-
public function queryOne($xpath, DOMNode $context = null)
59+
public function queryOne($xpath, DOMNode $context = null): ?Element
7060
{
7161
return $this->owner()->xpath()->queryOne($xpath, $context ?: $this);
7262
}
7363

7464
/**
75-
* {@inheritdoc}
65+
* @return mixed
7666
*/
7767
public function evaluate($expression, DOMNode $context = null)
7868
{

lib/XPath.php

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ class XPath extends \DOMXPath
2222
{
2323
/**
2424
* {@inheritdoc}
25+
*
26+
* @param mixed $contextnode
27+
* @param bool $registerNodeNS
2528
*/
2629
#[\ReturnTypeWillChange]
2730
public function evaluate($expression, $contextnode = null, $registerNodeNS = true)
@@ -32,12 +35,21 @@ public function evaluate($expression, $contextnode = null, $registerNodeNS = tru
3235
}
3336

3437
/**
38+
* @param bool $registerNodeNS
39+
* @param mixed $contextnode
40+
*
3541
* @return DOMNodeList<DOMNode>
3642
*/
3743
#[\ReturnTypeWillChange]
38-
public function query($expression, $contextnode = null, $registerNodeNS = true)
44+
public function query($expression, $contextnode = null, $registerNodeNS = true): DOMNodeList
3945
{
40-
return $this->execute('query', 'query', $expression, $contextnode, $registerNodeNS);
46+
$list = $this->execute('query', 'query', $expression, $contextnode, $registerNodeNS);
47+
48+
if (!$list instanceof DOMNodeList) {
49+
throw new RuntimeException(sprintf('Expected XPAth expression to return DOMNodeList, got "%s"', is_object($list) ? get_class($list) : gettype($list)));
50+
}
51+
52+
return $list;
4153
}
4254

4355
public function queryOne(string $expr, DOMNode $contextEl = null, bool $registerNodeNs = false): ?Element
@@ -54,7 +66,7 @@ public function queryOne(string $expr, DOMNode $contextEl = null, bool $register
5466
throw new RuntimeException(sprintf(
5567
'Expected "%s" but got "%s"',
5668
Element::class,
57-
get_class($node)
69+
$node ? get_class($node) : gettype($node)
5870
));
5971
}
6072

@@ -64,10 +76,12 @@ public function queryOne(string $expr, DOMNode $contextEl = null, bool $register
6476
/**
6577
* Execute the given xpath method and cactch any errors.
6678
*
79+
* @param mixed $contextEl
80+
*
6781
* @return mixed
6882
*/
6983
#[\ReturnTypeWillChange]
70-
private function execute(string $method, string $context, string $query, DOMNode $contextEl = null, bool $registerNodeNs = false)
84+
private function execute(string $method, string $context, string $query, $contextEl = null, bool $registerNodeNs = false)
7185
{
7286
libxml_use_internal_errors(true);
7387

@@ -85,7 +99,10 @@ private function execute(string $method, string $context, string $query, DOMNode
8599

86100
throw new Exception\InvalidQueryException(sprintf(
87101
'Errors encountered when evaluating XPath %s "%s": %s%s',
88-
$context, $query, PHP_EOL, implode(PHP_EOL, $errors)
102+
$context,
103+
$query,
104+
PHP_EOL,
105+
implode(PHP_EOL, $errors)
89106
));
90107
}
91108

phpstan.neon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
parameters:
2+
level: max
3+
paths:
4+
- lib

tests/Unit/ElementTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function testAppendElement(): void
3535
$this->assertInstanceOf('PhpBench\Dom\Element', $element);
3636
$this->assertEquals(1, $result);
3737
}
38-
38+
3939
/**
4040
* It should create and append text.
4141
*/
@@ -106,7 +106,7 @@ public function testDumpNode(): void
106106
</test>
107107
108108
EOT
109-
, $dump);
109+
, $dump);
110110
}
111111

112112
private function getXml()

0 commit comments

Comments
 (0)