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

Commit

Permalink
php7.3 vendor/bin/phpcbf
Browse files Browse the repository at this point in the history
Signed-off-by: Filippo Tessarotto <zoeslam@gmail.com>
  • Loading branch information
Slamdunk committed Jan 11, 2021
1 parent 0d0baed commit ecd2d54
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 82 deletions.
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
19 changes: 14 additions & 5 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,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,
Expand Down
42 changes: 31 additions & 11 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';
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;
Expand Down Expand Up @@ -105,13 +124,13 @@ protected function setStringDocument($document, $forcedType = null, $forcedEncod
$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 @@ -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 @@ -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:
Expand Down Expand Up @@ -301,6 +320,7 @@ public function getXpathPhpFunctions()
{
return $this->xpathPhpFunctions;
}

/**
* Register PHP Functions to use in internal DOMXPath
*
Expand Down
9 changes: 3 additions & 6 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 Down
29 changes: 24 additions & 5 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';
const TYPE_XPATH = 'TYPE_XPATH';
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 @@ -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 Down
25 changes: 12 additions & 13 deletions src/NodeList.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,61 +15,60 @@
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;

/**
* Constructor
*
* @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;
Expand Down
Loading

0 comments on commit ecd2d54

Please sign in to comment.