From 0d0baed3493f1436604ac3269f7583f7891231bf Mon Sep 17 00:00:00 2001 From: Filippo Tessarotto Date: Mon, 11 Jan 2021 13:42:52 +0100 Subject: [PATCH 1/3] Update laminas/laminas-coding-standard Signed-off-by: Filippo Tessarotto --- composer.json | 2 +- phpcs.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index f2437f06..71bdacc6 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,7 @@ "laminas/laminas-zendframework-bridge": "^1.1" }, "require-dev": { - "laminas/laminas-coding-standard": "~1.0.0", + "laminas/laminas-coding-standard": "^2.1.4", "phpunit/phpunit": "^9.4.3" }, "autoload": { diff --git a/phpcs.xml b/phpcs.xml index c6e76749..c0358142 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -1,6 +1,6 @@  - + src From ecd2d548338a047c4d1d2535cee6d71e476f20d5 Mon Sep 17 00:00:00 2001 From: Filippo Tessarotto Date: Mon, 11 Jan 2021 13:46:06 +0100 Subject: [PATCH 2/3] php7.3 vendor/bin/phpcbf Signed-off-by: Filippo Tessarotto --- src/Css2Xpath.php | 8 ++++++ src/DOMXPath.php | 19 +++++++++---- src/Document.php | 42 ++++++++++++++++++++------- src/Document/NodeList.php | 9 ++---- src/Document/Query.php | 29 +++++++++++++++---- src/NodeList.php | 25 ++++++++-------- src/Query.php | 52 +++++++++++++++++++++++----------- test/Document/NodeListTest.php | 8 ++++-- test/Document/QueryTest.php | 9 +++--- test/DocumentTest.php | 47 ++++++++++++++++++------------ 10 files changed, 166 insertions(+), 82 deletions(-) diff --git a/src/Css2Xpath.php b/src/Css2Xpath.php index 5c046f98..5d86baeb 100644 --- a/src/Css2Xpath.php +++ b/src/Css2Xpath.php @@ -8,10 +8,16 @@ namespace Laminas\Dom; +use function sprintf; +use function trigger_error; + +use const E_USER_DEPRECATED; + /** * Transform CSS selectors to XPath * * @deprecated + * * @see Document\Query */ class Css2Xpath @@ -20,7 +26,9 @@ class Css2Xpath * Transform CSS expression to XPath * * @deprecated + * * @see Document\Query + * * @param string $path * @return string */ diff --git a/src/DOMXPath.php b/src/DOMXPath.php index e853be30..64133733 100644 --- a/src/DOMXPath.php +++ b/src/DOMXPath.php @@ -5,10 +5,20 @@ * @copyright https://github.com/laminas/laminas-dom/blob/master/COPYRIGHT.md * @license https://github.com/laminas/laminas-dom/blob/master/LICENSE.md New BSD License */ + namespace Laminas\Dom; +use DOMNode; +use DOMNodeList; use ErrorException; +use function array_pop; +use function end; +use function restore_error_handler; +use function set_error_handler; + +use const E_WARNING; + /** * Extends DOMXpath to throw ErrorExceptions instead of raising errors. */ @@ -26,11 +36,10 @@ class DOMXPath extends \DOMXPath * raising an error * * @param string $expression The XPath expression to evaluate. - * @param \DOMNode $contextNode - * @return \DOMNodeList + * @return DOMNodeList * @throws ErrorException */ - public function queryWithErrorException($expression, \DOMNode $contextNode = null) + public function queryWithErrorException($expression, ?DOMNode $contextNode = null) { $this->errors = [null]; @@ -38,7 +47,7 @@ public function queryWithErrorException($expression, \DOMNode $contextNode = nul $contextNode = $this->document->documentElement; } - set_error_handler([$this, 'addError'], \E_WARNING); + set_error_handler([$this, 'addError'], E_WARNING); $nodeList = $this->query($expression, $contextNode); restore_error_handler(); @@ -61,7 +70,7 @@ public function queryWithErrorException($expression, \DOMNode $contextNode = nul */ public function addError($errno, $errstr = '', $errfile = '', $errline = 0) { - $last_error = end($this->errors); + $last_error = end($this->errors); $this->errors[] = new ErrorException( $errstr, 0, diff --git a/src/Document.php b/src/Document.php index 41d6b24b..4ba429d7 100644 --- a/src/Document.php +++ b/src/Document.php @@ -10,6 +10,19 @@ use DOMDocument; +use function libxml_clear_errors; +use function libxml_disable_entity_loader; +use function libxml_get_errors; +use function libxml_use_internal_errors; +use function preg_match; +use function sprintf; +use function strstr; +use function substr; +use function trim; + +use const LIBXML_VERSION; +use const XML_DOCUMENT_TYPE_NODE; + /** * Class used to initialize DomDocument from string, with proper verifications */ @@ -18,43 +31,49 @@ class Document /**#@+ * Document types */ - const DOC_HTML = 'DOC_HTML'; - const DOC_XHTML = 'DOC_XHTML'; - const DOC_XML = 'DOC_XML'; + const DOC_HTML = 'DOC_HTML'; + const DOC_XHTML = 'DOC_XHTML'; + const DOC_XML = 'DOC_XML'; /**#@-*/ /** * Raw document + * * @var string */ protected $stringDocument; /** * DOMDocument generated from raw string document + * * @var DOMDocument */ protected $domDocument; /** * Type of the document provided + * * @var string */ protected $type; /** * Error list generated from transformation of document to DOMDocument + * * @var array */ protected $errors = []; /** * XPath namespaces + * * @var array */ protected $xpathNamespaces = []; /** * XPath PHP Functions + * * @var mixed */ protected $xpathPhpFunctions; @@ -105,13 +124,13 @@ protected function setStringDocument($document, $forcedType = null, $forcedEncod $type = static::DOC_XML; if (preg_match('/]*xmlns="([^"]+)"[^>]*>/i', $document, $matches)) { $this->xpathNamespaces[] = $matches[1]; - $type = static::DOC_XHTML; + $type = static::DOC_XHTML; } } // Unsetting previously registered DOMDocument - $this->domDocument = null; - $this->stringDocument = ! empty($document) ? $document : null; + $this->domDocument = null; + $this->stringDocument = ! empty($document) ? $document : null; $this->setType($forcedType ?: (! empty($document) ? $type : null)); $this->setEncoding($forcedEncoding); @@ -165,9 +184,9 @@ public function getDomDocument() /** * Set DOMDocument * - * @param DOMDocument $domDocument - * @return self * @deprecated + * + * @return self */ protected function setDomDocument(DOMDocument $domDocument) { @@ -233,9 +252,9 @@ protected function getDomDocumentFromString($stringDocument) libxml_use_internal_errors(true); $disableEntityLoaderFlag = self::disableEntityLoader(); - $encoding = $this->getEncoding(); - $domDoc = null === $encoding ? new DOMDocument('1.0') : new DOMDocument('1.0', $encoding); - $type = $this->getType(); + $encoding = $this->getEncoding(); + $domDoc = null === $encoding ? new DOMDocument('1.0') : new DOMDocument('1.0', $encoding); + $type = $this->getType(); switch ($type) { case static::DOC_XML: @@ -301,6 +320,7 @@ public function getXpathPhpFunctions() { return $this->xpathPhpFunctions; } + /** * Register PHP Functions to use in internal DOMXPath * diff --git a/src/Document/NodeList.php b/src/Document/NodeList.php index a11745d9..7aabb267 100644 --- a/src/Document/NodeList.php +++ b/src/Document/NodeList.php @@ -20,21 +20,18 @@ */ class NodeList implements Iterator, Countable, ArrayAccess { - /** - * @var DOMNodeList - */ + /** @var DOMNodeList */ protected $list; /** * Current iterator position + * * @var int */ protected $position = 0; /** * Constructor - * - * @param DOMNodeList $list */ public function __construct(DOMNodeList $list) { @@ -114,7 +111,7 @@ public function count() public function offsetExists($key) { // DOMNodeList return `null` if item not exists. - return (null !== $this->list->item($key)); + return null !== $this->list->item($key); } /** diff --git a/src/Document/Query.php b/src/Document/Query.php index e2be0866..5caa1bd2 100644 --- a/src/Document/Query.php +++ b/src/Document/Query.php @@ -8,9 +8,29 @@ namespace Laminas\Dom\Document; +use DOMNode; use Laminas\Dom\Document; use Laminas\Dom\DOMXPath; +use function array_merge; +use function count; +use function explode; +use function implode; +use function is_array; +use function is_string; +use function ltrim; +use function mt_rand; +use function preg_replace; +use function preg_replace_callback; +use function preg_split; +use function sprintf; +use function str_replace; +use function strpos; +use function strstr; +use function strtolower; +use function trim; +use function uniqid; + /** * Query object executable in a Laminas\Dom\Document */ @@ -19,8 +39,8 @@ class Query /**#@+ * Query types */ - const TYPE_XPATH = 'TYPE_XPATH'; - const TYPE_CSS = 'TYPE_CSS'; + const TYPE_XPATH = 'TYPE_XPATH'; + const TYPE_CSS = 'TYPE_CSS'; /**#@-*/ /** @@ -29,14 +49,13 @@ class Query * @param string $expression CSS selector or XPath query * @param Document $document Document to query * @param string $type The type of $expression - * @param \DOMNode $contextNode * @return NodeList */ public static function execute( $expression, Document $document, $type = self::TYPE_XPATH, - \DOMNode $contextNode = null + ?DOMNode $contextNode = null ) { // Expression check if ($type === static::TYPE_CSS) { @@ -116,7 +135,7 @@ function ($matches) use ($placeholder) { if (0 === strpos($pathSegment, '[contains(')) { foreach ($paths as $pathKey => $xpath) { $paths[$pathKey] .= '//*' . ltrim($pathSegment, '*'); - $paths[] = $xpath . $pathSegment; + $paths[] = $xpath . $pathSegment; } } else { foreach ($paths as $pathKey => $xpath) { diff --git a/src/NodeList.php b/src/NodeList.php index a67ade74..51737c9b 100644 --- a/src/NodeList.php +++ b/src/NodeList.php @@ -15,44 +15,46 @@ use DOMNodeList; use Iterator; +use function in_array; +use function range; + /** * Nodelist for DOM XPath query + * * @deprecated + * * @see \Laminas\Dom\Document\NodeList */ class NodeList implements Iterator, Countable, ArrayAccess { /** * CSS Selector query + * * @var string */ protected $cssQuery; - /** - * @var DOMDocument - */ + /** @var DOMDocument */ protected $document; - /** - * @var DOMNodeList - */ + /** @var DOMNodeList */ protected $nodeList; /** * Current iterator position + * * @var int */ protected $position = 0; /** * XPath query + * * @var string */ protected $xpathQuery; - /** - * @var DOMNode|null - */ + /** @var DOMNode|null */ protected $contextNode; /** @@ -60,16 +62,13 @@ class NodeList implements Iterator, Countable, ArrayAccess * * @param string $cssQuery * @param string|array $xpathQuery - * @param DOMDocument $document - * @param DOMNodeList $nodeList - * @param DOMNode|null $contextNode */ public function __construct( $cssQuery, $xpathQuery, DOMDocument $document, DOMNodeList $nodeList, - DOMNode $contextNode = null + ?DOMNode $contextNode = null ) { $this->cssQuery = $cssQuery; $this->xpathQuery = $xpathQuery; diff --git a/src/Query.php b/src/Query.php index ecbff610..d2be92ef 100644 --- a/src/Query.php +++ b/src/Query.php @@ -10,10 +10,28 @@ use DOMDocument; use DOMNode; +use DOMNodeList; +use ErrorException; + +use function libxml_clear_errors; +use function libxml_disable_entity_loader; +use function libxml_get_errors; +use function libxml_use_internal_errors; +use function preg_match; +use function sprintf; +use function strlen; +use function strstr; +use function substr; +use function trim; + +use const LIBXML_VERSION; +use const XML_DOCUMENT_TYPE_NODE; /** * Query DOM structures based on CSS selectors and/or XPath + * * @deprecated + * * @see \Laminas\Dom\Document\Query */ class Query @@ -26,37 +44,40 @@ class Query const DOC_XHTML = 'docXhtml'; /**#@-*/ - /** - * @var string - */ + /** @var string */ protected $document; /** * DOMDocument errors, if any + * * @var false|array */ protected $documentErrors = false; /** * Document type + * * @var string */ protected $docType; /** * Document encoding + * * @var null|string */ protected $encoding; /** * XPath namespaces + * * @var array */ protected $xpathNamespaces = []; /** * XPath PHP Functions + * * @var mixed */ protected $xpathPhpFunctions; @@ -81,7 +102,7 @@ public function __construct($document = null, $encoding = null) */ public function setEncoding($encoding) { - $this->encoding = (null === $encoding) ? null : (string) $encoding; + $this->encoding = null === $encoding ? null : (string) $encoding; return $this; } @@ -206,10 +227,9 @@ public function getDocumentErrors() * Perform a CSS selector query * * @param string $query - * @param DOMNode $contextNode * @return NodeList */ - public function execute($query, DOMNode $contextNode = null) + public function execute($query, ?DOMNode $contextNode = null) { $xpathQuery = Document\Query::cssToXpath($query); return $this->queryXpath($xpathQuery, $query, $contextNode); @@ -224,7 +244,7 @@ public function execute($query, DOMNode $contextNode = null) * @throws Exception\RuntimeException * @return NodeList */ - public function queryXpath($xpathQuery, $query = null, DOMNode $contextNode = null) + public function queryXpath($xpathQuery, $query = null, ?DOMNode $contextNode = null) { if (null === ($document = $this->getDocument())) { throw new Exception\RuntimeException('Cannot query; no document registered'); @@ -238,7 +258,7 @@ public function queryXpath($xpathQuery, $query = null, DOMNode $contextNode = nu } else { $domDoc = new DOMDocument('1.0', $encoding); } - $type = $this->getDocumentType(); + $type = $this->getDocumentType(); switch ($type) { case self::DOC_XML: $success = $domDoc->loadXML($document); @@ -268,7 +288,7 @@ public function queryXpath($xpathQuery, $query = null, DOMNode $contextNode = nu throw new Exception\RuntimeException(sprintf('Error parsing document (type == %s)', $type)); } - $nodeList = $this->getNodeList($domDoc, $xpathQuery, $contextNode); + $nodeList = $this->getNodeList($domDoc, $xpathQuery, $contextNode); return new NodeList($query, $xpathQuery, $domDoc, $nodeList, $contextNode); } @@ -299,26 +319,24 @@ public function registerXpathPhpFunctions($xpathPhpFunctions = true) * * @param DOMDocument $document * @param string|array $xpathQuery - * @param DOMNode $contextNode - * @return \DOMNodeList - * @throws \ErrorException If query cannot be executed + * @return DOMNodeList + * @throws ErrorException If query cannot be executed */ - protected function getNodeList($document, $xpathQuery, DOMNode $contextNode = null) + protected function getNodeList($document, $xpathQuery, ?DOMNode $contextNode = null) { - $xpath = new DOMXPath($document); + $xpath = new DOMXPath($document); foreach ($this->xpathNamespaces as $prefix => $namespaceUri) { $xpath->registerNamespace($prefix, $namespaceUri); } if ($this->xpathPhpFunctions) { $xpath->registerNamespace("php", "http://php.net/xpath"); - ($this->xpathPhpFunctions === true) ? + $this->xpathPhpFunctions === true ? $xpath->registerPhpFunctions() : $xpath->registerPhpFunctions($this->xpathPhpFunctions); } $xpathQuery = (string) $xpathQuery; - $nodeList = $xpath->queryWithErrorException($xpathQuery, $contextNode); - return $nodeList; + return $xpath->queryWithErrorException($xpathQuery, $contextNode); } /** diff --git a/test/Document/NodeListTest.php b/test/Document/NodeListTest.php index 56386dba..6900d279 100644 --- a/test/Document/NodeListTest.php +++ b/test/Document/NodeListTest.php @@ -15,6 +15,8 @@ use Laminas\Dom\Exception\BadMethodCallException; use PHPUnit\Framework\TestCase; +use function iterator_to_array; + /** * @covers Laminas\Dom\Document\NodeList */ @@ -31,7 +33,7 @@ protected function setUp(): void $document = new DOMDocument(); $document->loadHTML(''); $this->domNodeList = $domNodeList = $document->getElementsByTagName('*'); - $this->nodeList = new NodeList($domNodeList); + $this->nodeList = new NodeList($domNodeList); } /** @@ -39,9 +41,9 @@ protected function setUp(): void */ public function testEmptyResultDoesNotReturnIteratorValidTrue() { - $dom = new DOMDocument(); + $dom = new DOMDocument(); $emptyNodeList = $dom->getElementsByTagName('a'); - $result = new NodeList($emptyNodeList); + $result = new NodeList($emptyNodeList); $this->assertFalse($result->valid()); } diff --git a/test/Document/QueryTest.php b/test/Document/QueryTest.php index 1ec576e5..f53fec4d 100644 --- a/test/Document/QueryTest.php +++ b/test/Document/QueryTest.php @@ -11,6 +11,9 @@ use Laminas\Dom\Document\Query; use PHPUnit\Framework\TestCase; +use function count; +use function explode; + /** * @covers Laminas\Dom\Document\Query */ @@ -144,9 +147,7 @@ public function descendantSelector() /** * @group Laminas-8006 - * * @dataProvider descendantSelector - * * @param string $path */ public function testShouldAllowWhitespaceInDescendantSelectorExpressions($path) @@ -188,11 +189,11 @@ public function testCanTransformWithAttributeAndDot() public function nestedAttributeSelectors() { return [ - 'with-double-quotes' => [ + 'with-double-quotes' => [ 'select[name="foo"] option[selected="selected"]', "//select[@name='foo']//option[@selected='selected']", ], - 'with-single-quotes' => [ + 'with-single-quotes' => [ "select[name='foo'] option[selected='selected']", "//select[@name='foo']//option[@selected='selected']", ], diff --git a/test/DocumentTest.php b/test/DocumentTest.php index d9f6ef8b..510f3175 100644 --- a/test/DocumentTest.php +++ b/test/DocumentTest.php @@ -9,11 +9,22 @@ namespace LaminasTest\Dom; use DOMDocument; +use Error; +use ErrorException; +use Exception; use Laminas\Dom\Document; use Laminas\Dom\Exception\ExceptionInterface as DOMException; use Laminas\Dom\Exception\RuntimeException; use PHPUnit\Framework\TestCase; +use function count; +use function file_get_contents; +use function restore_error_handler; +use function set_error_handler; +use function strtolower; + +use const PHP_VERSION_ID; + /** * @covers Laminas\Dom\Document * @covers Laminas\Dom\Document\Query::execute @@ -38,7 +49,7 @@ public function setUp(): void public function getHtml() { if (null === $this->html) { - $this->html = file_get_contents(__DIR__ . '/_files/sample.xhtml'); + $this->html = file_get_contents(__DIR__ . '/_files/sample.xhtml'); } return $this->html; } @@ -60,7 +71,7 @@ public function testConstructorShouldNotRequireArguments() public function testConstructorShouldAcceptDocumentString() { - $html = $this->getHtml(); + $html = $this->getHtml(); $document = new Document($html); $this->assertSame($html, $document->getStringDocument()); } @@ -79,7 +90,7 @@ public function testDomDocShouldRaiseExceptionByDefault() public function testDocShouldBeNullByEmptyStringConstructor() { - $emptyStr = ''; + $emptyStr = ''; $this->document = new Document($emptyStr); $this->assertNull($this->document->getStringDocument()); } @@ -133,7 +144,7 @@ public function testQueryingInvalidDocumentShouldThrowException() public function testGetDomMethodShouldReturnDomDocumentWithStringDocumentInConstructor() { - $html = $this->getHtml(); + $html = $this->getHtml(); $document = new Document($html); $this->assertInstanceOf(DOMDocument::class, $document->getDomDocument()); } @@ -208,7 +219,7 @@ public function testXpathPhpFunctionsShouldBeDisabledByDefault() '//meta[php:functionString("strtolower", @http-equiv) = "content-type"]', $this->document ); - } catch (\Exception $e) { + } catch (Exception $e) { return; } $this->fail('XPath PHPFunctions should be disabled by default'); @@ -233,7 +244,7 @@ public function testXpathPhpFunctionsShouldBeNotCalledWhenSpecifiedFunction() $this->loadHtml(); $this->document->registerXpathPhpFunctions('stripos'); - $this->expectException(PHP_VERSION_ID >= 80000 ? \Error::class : \ErrorException::class); + $this->expectException(PHP_VERSION_ID >= 80000 ? Error::class : ErrorException::class); $this->expectExceptionMessageMatches('/Not allowed to call handler .strtolower()/'); Document\Query::execute( @@ -247,10 +258,10 @@ public function testXpathPhpFunctionsShouldBeNotCalledWhenSpecifiedFunction() */ public function testLoadingDocumentWithErrorsShouldNotRaisePhpErrors() { - $file = file_get_contents(__DIR__ . '/_files/bad-sample.html'); + $file = file_get_contents(__DIR__ . '/_files/bad-sample.html'); $this->document = new Document($file); - $result = Document\Query::execute('p', $this->document, Document\Query::TYPE_CSS); - $errors = $this->document->getErrors(); + $result = Document\Query::execute('p', $this->document, Document\Query::TYPE_CSS); + $errors = $this->document->getErrors(); $this->assertIsArray($errors); $this->assertNotEmpty($errors); } @@ -275,7 +286,7 @@ public function testCssSelectorShouldFindNodesWhenMatchingMultipleAttributes() HTML; $this->document = new Document($html); - $result = Document\Query::execute( + $result = Document\Query::execute( 'input[type="hidden"][value="1"]', $this->document, Document\Query::TYPE_CSS @@ -328,7 +339,7 @@ public function testAllowsSpecifyingEncodingViaSetter() public function testSpecifyingEncodingSetsEncodingOnDomDocument() { $this->document = new Document($this->getHtml(), null, 'utf-8'); - $result = Document\Query::execute('.foo', $this->document, Document\Query::TYPE_CSS); + $result = Document\Query::execute('.foo', $this->document, Document\Query::TYPE_CSS); $this->assertInstanceOf(Document\NodeList::class, $result); $this->assertInstanceOf(DOMDocument::class, $this->document->getDomDocument()); $this->assertEquals('utf-8', $this->document->getEncoding()); @@ -346,8 +357,8 @@ public function testXhtmlDocumentWithXmlDeclaration()

Test paragraph.

XML; - $this->document = new Document($xhtmlWithXmlDecl, null, 'utf-8'); - $result = Document\Query::execute('//p', $this->document, Document\Query::TYPE_CSS); + $this->document = new Document($xhtmlWithXmlDecl, null, 'utf-8'); + $result = Document\Query::execute('//p', $this->document, Document\Query::TYPE_CSS); $this->assertEquals(1, $result->count()); } @@ -370,14 +381,14 @@ public function testXhtmlDocumentWithXmlAndDoctypeDeclaration() XML; - $this->document = new Document($xhtmlWithXmlDecl, null, 'utf-8'); - $result = Document\Query::execute('//p', $this->document, Document\Query::TYPE_CSS); + $this->document = new Document($xhtmlWithXmlDecl, null, 'utf-8'); + $result = Document\Query::execute('//p', $this->document, Document\Query::TYPE_CSS); $this->assertEquals(1, $result->count()); } public function testLoadingXmlContainingDoctypeShouldFailToPreventXxeAndXeeAttacks() { - $xml = << ]> @@ -392,9 +403,9 @@ public function testLoadingXmlContainingDoctypeShouldFailToPreventXxeAndXeeAttac public function testContextNode() { $this->loadHtml(); - $results = Document\Query::execute('//div[@id="subnav"]', $this->document, Document\Query::TYPE_XPATH); + $results = Document\Query::execute('//div[@id="subnav"]', $this->document, Document\Query::TYPE_XPATH); $contextNode = $results[0]; - $results = Document\Query::execute('.//li', $this->document, Document\Query::TYPE_XPATH, $contextNode); + $results = Document\Query::execute('.//li', $this->document, Document\Query::TYPE_XPATH, $contextNode); $this->assertSame('Item 1', $results[0]->nodeValue); } } From f87fd6276c69e42b29436cf5e7555fcf08a549e3 Mon Sep 17 00:00:00 2001 From: Filippo Tessarotto Date: Mon, 11 Jan 2021 14:06:02 +0100 Subject: [PATCH 3/3] Manual CS fixes Signed-off-by: Filippo Tessarotto --- src/DOMXPath.php | 4 ++-- src/Document.php | 13 +++++++------ src/Document/NodeList.php | 4 ++-- src/Document/Query.php | 8 ++++---- src/NodeList.php | 4 ++-- src/Query.php | 12 ++++++------ test/DOMXPathTest.php | 8 ++++---- test/Document/QueryTest.php | 7 ++++--- test/DocumentTest.php | 7 +++---- 9 files changed, 34 insertions(+), 33 deletions(-) diff --git a/src/DOMXPath.php b/src/DOMXPath.php index 64133733..e07940a1 100644 --- a/src/DOMXPath.php +++ b/src/DOMXPath.php @@ -70,14 +70,14 @@ public function queryWithErrorException($expression, ?DOMNode $contextNode = nul */ public function addError($errno, $errstr = '', $errfile = '', $errline = 0) { - $last_error = end($this->errors); + $lastError = end($this->errors); $this->errors[] = new ErrorException( $errstr, 0, $errno, $errfile, $errline, - $last_error + $lastError ); } } diff --git a/src/Document.php b/src/Document.php index 4ba429d7..2969e3a4 100644 --- a/src/Document.php +++ b/src/Document.php @@ -31,9 +31,9 @@ class Document /**#@+ * Document types */ - const DOC_HTML = 'DOC_HTML'; - const DOC_XHTML = 'DOC_XHTML'; - const DOC_XML = 'DOC_XML'; + public const DOC_HTML = 'DOC_HTML'; + public const DOC_XHTML = 'DOC_XHTML'; + public const DOC_XML = 'DOC_XML'; /**#@-*/ /** @@ -120,7 +120,7 @@ protected function setStringDocument($document, $forcedType = null, $forcedEncod } // Breaking XML declaration to make syntax highlighting work - if ('<' . '?xml' == substr(trim($document), 0, 5)) { + if ('<' . '?xml' === substr(trim($document), 0, 5)) { $type = static::DOC_XML; if (preg_match('/]*xmlns="([^"]+)"[^>]*>/i', $document, $matches)) { $this->xpathNamespaces[] = $matches[1]; @@ -166,7 +166,7 @@ protected function setType($type) * Get DOMDocument generated from set raw document * * @return DOMDocument - * @throws Exception\RuntimeException If cannot get DOMDocument; no document registered + * @throws Exception\RuntimeException If cannot get DOMDocument; no document registered. */ public function getDomDocument() { @@ -244,6 +244,7 @@ protected function setErrors($errors) /** * Get DOMDocument from set raw document * + * @param string $stringDocument * @return DOMDocument * @throws Exception\RuntimeException */ @@ -342,7 +343,7 @@ public function registerXpathPhpFunctions($xpathPhpFunctions = true) * * @return bool */ - private static function disableEntityLoader($flag = true) + private static function disableEntityLoader(bool $flag = true) { if (LIBXML_VERSION < 20900) { return libxml_disable_entity_loader($flag); diff --git a/src/Document/NodeList.php b/src/Document/NodeList.php index 7aabb267..32080b10 100644 --- a/src/Document/NodeList.php +++ b/src/Document/NodeList.php @@ -130,7 +130,7 @@ public function offsetGet($key) * * @param mixed $key * @param mixed $value - * @throws Exception\BadMethodCallException when attempting to write to a read-only item + * @throws Exception\BadMethodCallException When attempting to write to a read-only item. */ public function offsetSet($key, $value) { @@ -141,7 +141,7 @@ public function offsetSet($key, $value) * ArrayAccess: unset offset * * @param mixed $key - * @throws Exception\BadMethodCallException when attempting to unset a read-only item + * @throws Exception\BadMethodCallException When attempting to unset a read-only item. */ public function offsetUnset($key) { diff --git a/src/Document/Query.php b/src/Document/Query.php index 5caa1bd2..dc36b4fc 100644 --- a/src/Document/Query.php +++ b/src/Document/Query.php @@ -39,8 +39,8 @@ class Query /**#@+ * Query types */ - const TYPE_XPATH = 'TYPE_XPATH'; - const TYPE_CSS = 'TYPE_CSS'; + public const TYPE_XPATH = 'TYPE_XPATH'; + public const TYPE_CSS = 'TYPE_CSS'; /**#@-*/ /** @@ -124,7 +124,7 @@ function ($matches) use ($placeholder) { foreach ($segments as $key => $segment) { $pathSegment = static::_tokenize($segment); - if (0 == $key) { + if (0 === $key) { if (0 === strpos($pathSegment, '[contains(')) { $paths[0] .= '*' . ltrim($pathSegment, '*'); } else { @@ -144,7 +144,7 @@ function ($matches) use ($placeholder) { } } - if (1 == count($paths)) { + if (1 === count($paths)) { return $paths[0]; } return implode('|', $paths); diff --git a/src/NodeList.php b/src/NodeList.php index 51737c9b..8e5c4b09 100644 --- a/src/NodeList.php +++ b/src/NodeList.php @@ -215,7 +215,7 @@ public function offsetGet($key) * * @param mixed $key * @param mixed $value - * @throws Exception\BadMethodCallException when attempting to write to a read-only item + * @throws Exception\BadMethodCallException When attempting to write to a read-only item. */ public function offsetSet($key, $value) { @@ -226,7 +226,7 @@ public function offsetSet($key, $value) * ArrayAccess: unset offset * * @param mixed $key - * @throws Exception\BadMethodCallException when attempting to unset a read-only item + * @throws Exception\BadMethodCallException When attempting to unset a read-only item. */ public function offsetUnset($key) { diff --git a/src/Query.php b/src/Query.php index d2be92ef..d4a1faa8 100644 --- a/src/Query.php +++ b/src/Query.php @@ -39,9 +39,9 @@ class Query /**#@+ * Document types */ - const DOC_XML = 'docXml'; - const DOC_HTML = 'docHtml'; - const DOC_XHTML = 'docXhtml'; + public const DOC_XML = 'docXml'; + public const DOC_HTML = 'docHtml'; + public const DOC_XHTML = 'docXhtml'; /**#@-*/ /** @var string */ @@ -129,7 +129,7 @@ public function setDocument($document, $encoding = null) return $this; } // breaking XML declaration to make syntax highlighting work - if ('<' . '?xml' == substr(trim($document), 0, 5)) { + if ('<' . '?xml' === substr(trim($document), 0, 5)) { if (preg_match('/]*xmlns="([^"]+)"[^>]*>/i', $document, $matches)) { $this->xpathNamespaces[] = $matches[1]; return $this->setDocumentXhtml($document, $encoding); @@ -320,7 +320,7 @@ public function registerXpathPhpFunctions($xpathPhpFunctions = true) * @param DOMDocument $document * @param string|array $xpathQuery * @return DOMNodeList - * @throws ErrorException If query cannot be executed + * @throws ErrorException If query cannot be executed. */ protected function getNodeList($document, $xpathQuery, ?DOMNode $contextNode = null) { @@ -349,7 +349,7 @@ protected function getNodeList($document, $xpathQuery, ?DOMNode $contextNode = n * * @return bool */ - private static function disableEntityLoader($flag = true) + private static function disableEntityLoader(bool $flag = true) { if (LIBXML_VERSION < 20900) { return libxml_disable_entity_loader($flag); diff --git a/test/DOMXPathTest.php b/test/DOMXPathTest.php index ead689d4..7e034b6d 100644 --- a/test/DOMXPathTest.php +++ b/test/DOMXPathTest.php @@ -26,19 +26,19 @@ protected function setUp(): void public function testQueryWithErrorExceptionSuccess() { - $domXPath = new DOMXPath($this->document); + $domXpath = new DOMXPath($this->document); - $result = $domXPath->queryWithErrorException('any'); + $result = $domXpath->queryWithErrorException('any'); $this->assertInstanceOf(DOMNodeList::class, $result); } public function testQueryWithErrorExceptionThrowExceptionWhenQueryExpresionIsInvalid() { - $domXPath = new DOMXPath($this->document); + $domXpath = new DOMXPath($this->document); $this->expectException(ErrorException::class); $this->expectExceptionMessage('Invalid expression'); - $domXPath->queryWithErrorException('any#any'); + $domXpath->queryWithErrorException('any#any'); } } diff --git a/test/Document/QueryTest.php b/test/Document/QueryTest.php index f53fec4d..66533006 100644 --- a/test/Document/QueryTest.php +++ b/test/Document/QueryTest.php @@ -8,6 +8,7 @@ namespace LaminasTest\Dom\Document; +use Generator; use Laminas\Dom\Document\Query; use PHPUnit\Framework\TestCase; @@ -136,7 +137,7 @@ public function testShouldAllowMatchingOfAttributeValues() $this->assertEquals("//tag[@id='id']//@attribute", $test); } - public function descendantSelector() + public function descendantSelector(): Generator { yield 'space before' => ['child >leaf']; yield 'space after' => ['child> leaf']; @@ -186,7 +187,7 @@ public function testCanTransformWithAttributeAndDot() $this->assertEquals("//a[@href='http://example.com']", $test); } - public function nestedAttributeSelectors() + public function nestedAttributeSelectors(): array { return [ 'with-double-quotes' => [ @@ -211,7 +212,7 @@ public function nestedAttributeSelectors() /** * @dataProvider nestedAttributeSelectors */ - public function testTransformNestedAttributeSelectors($selector, $expectedXpath) + public function testTransformNestedAttributeSelectors(string $selector, string $expectedXpath) { $this->assertEquals($expectedXpath, Query::cssToXpath($selector)); } diff --git a/test/DocumentTest.php b/test/DocumentTest.php index 510f3175..8f062c2c 100644 --- a/test/DocumentTest.php +++ b/test/DocumentTest.php @@ -46,7 +46,7 @@ public function setUp(): void $this->document = new Document(); } - public function getHtml() + private function getHtml(): string { if (null === $this->html) { $this->html = file_get_contents(__DIR__ . '/_files/sample.xhtml'); @@ -54,14 +54,13 @@ public function getHtml() return $this->html; } - public function loadHtml() + private function loadHtml() { $this->document = new Document($this->getHtml()); } - public function handleError($msg, $code = 0) + public function handleError(string $msg, int $code = 0) { - $this->error = $msg; } public function testConstructorShouldNotRequireArguments()