Skip to content
This repository has been archived by the owner on Dec 5, 2024. It is now read-only.

Commit

Permalink
Merge pull request #10 from Slamdunk/cs_v2
Browse files Browse the repository at this point in the history
Update to laminas-coding-standard:v2
  • Loading branch information
Ocramius authored Jan 11, 2021
2 parents 65c5e46 + f87fd62 commit 7eeb653
Show file tree
Hide file tree
Showing 13 changed files with 195 additions and 110 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion phpcs.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<ruleset name="Laminas coding standard">
<rule ref="./vendor/laminas/laminas-coding-standard/ruleset.xml"/>
<rule ref="LaminasCodingStandard"/>

<!-- Paths to check -->
<file>src</file>
Expand Down
8 changes: 8 additions & 0 deletions src/Css2Xpath.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,7 +26,9 @@ class Css2Xpath
* Transform CSS expression to XPath
*
* @deprecated
*
* @see Document\Query
*
* @param string $path
* @return string
*/
Expand Down
21 changes: 15 additions & 6 deletions src/DOMXPath.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -26,19 +36,18 @@ 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];

if ($contextNode === null) {
$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();

Expand All @@ -61,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
);
}
}
49 changes: 35 additions & 14 deletions src/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -18,43 +31,49 @@ 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';
/**#@-*/

/**
* 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;
Expand Down Expand Up @@ -101,17 +120,17 @@ 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('/<html[^>]*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);
Expand Down Expand Up @@ -147,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()
{
Expand All @@ -165,9 +184,9 @@ public function getDomDocument()
/**
* Set DOMDocument
*
* @param DOMDocument $domDocument
* @return self
* @deprecated
*
* @return self
*/
protected function setDomDocument(DOMDocument $domDocument)
{
Expand Down Expand Up @@ -225,6 +244,7 @@ protected function setErrors($errors)
/**
* Get DOMDocument from set raw document
*
* @param string $stringDocument
* @return DOMDocument
* @throws Exception\RuntimeException
*/
Expand All @@ -233,9 +253,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:
Expand Down Expand Up @@ -301,6 +321,7 @@ public function getXpathPhpFunctions()
{
return $this->xpathPhpFunctions;
}

/**
* Register PHP Functions to use in internal DOMXPath
*
Expand All @@ -322,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);
Expand Down
13 changes: 5 additions & 8 deletions src/Document/NodeList.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -133,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)
{
Expand All @@ -144,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)
{
Expand Down
33 changes: 26 additions & 7 deletions src/Document/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -19,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';
/**#@-*/

/**
Expand All @@ -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) {
Expand Down Expand Up @@ -105,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 {
Expand All @@ -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) {
Expand All @@ -125,7 +144,7 @@ function ($matches) use ($placeholder) {
}
}

if (1 == count($paths)) {
if (1 === count($paths)) {
return $paths[0];
}
return implode('|', $paths);
Expand Down
Loading

0 comments on commit 7eeb653

Please sign in to comment.