Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 0 additions & 45 deletions .php_cs

This file was deleted.

15 changes: 7 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ matrix:
include:
- php: 5.5
env:
- EXECUTE_CS_CHECK=true
- CS_CHECK=true
- php: 5.6
env:
- EXECUTE_TEST_COVERALLS=true
- TEST_COVERAGE=true
- DEPLOY_DOCS="$(if [[ $TRAVIS_BRANCH == 'master' && $TRAVIS_PULL_REQUEST == 'false' ]]; then echo -n 'true' ; else echo -n 'false' ; fi)"
- PATH="$HOME/.local/bin:$PATH"
- php: 7
Expand All @@ -43,21 +43,20 @@ notifications:
email: false

before_install:
- if [[ $EXECUTE_TEST_COVERALLS != 'true' ]]; then phpenv config-rm xdebug.ini || return 0 ; fi
- if [[ $TEST_COVERAGE != 'true' ]]; then phpenv config-rm xdebug.ini || return 0 ; fi
- composer self-update
- if [[ $EXECUTE_TEST_COVERALLS == 'true' ]]; then composer require --dev --no-update satooshi/php-coveralls ; fi
- if [[ $TEST_COVERAGE == 'true' ]]; then composer require --dev --no-update satooshi/php-coveralls ; fi

install:
- travis_retry composer install --no-interaction --ignore-platform-reqs

script:
- if [[ $EXECUTE_TEST_COVERALLS == 'true' ]]; then ./vendor/bin/phpunit --coverage-clover clover.xml ; fi
- if [[ $EXECUTE_TEST_COVERALLS != 'true' ]]; then ./vendor/bin/phpunit ; fi
- if [[ $EXECUTE_CS_CHECK == 'true' ]]; then ./vendor/bin/php-cs-fixer fix -v --diff --dry-run ; fi
- if [[ $TEST_COVERAGE == 'true' ]]; then composer test-coverage ; else composer test ; fi
- if [[ $CS_CHECK == 'true' ]]; then composer cs-check ; fi
- if [[ $DEPLOY_DOCS == "true" && "$TRAVIS_TEST_RESULT" == "0" ]]; then wget -O theme-installer.sh "https://raw.githubusercontent.com/zendframework/zf-mkdoc-theme/master/theme-installer.sh" ; chmod 755 theme-installer.sh ; ./theme-installer.sh ; fi

after_success:
- if [[ $DEPLOY_DOCS == "true" ]]; then echo "Preparing to build and deploy documentation" ; ./zf-mkdoc-theme/deploy.sh ; echo "Completed deploying documentation" ; fi

after_script:
- if [[ $EXECUTE_TEST_COVERALLS == 'true' ]]; then ./vendor/bin/coveralls ; fi
- if [[ $TEST_COVERAGE == 'true' ]]; then composer upload-coverage ; fi
14 changes: 7 additions & 7 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,24 +77,24 @@ To do so:

## Running Coding Standards Checks

This component uses [php-cs-fixer](http://cs.sensiolabs.org/) for coding
This component uses [phpcs](https://github.com/squizlabs/PHP_CodeSniffer) for coding
standards checks, and provides configuration for our selected checks.
`php-cs-fixer` is installed by default via Composer.
`phpcs` is installed by default via Composer.

To run checks only:

```console
$ ./vendor/bin/php-cs-fixer fix . -v --diff --dry-run --config-file=.php_cs
$ composer cs-check
```

To have `php-cs-fixer` attempt to fix problems for you, omit the `--dry-run`
flag:
`phpcs` also includes a tool for fixing most CS violations, `phpcbf`:


```console
$ ./vendor/bin/php-cs-fixer fix . -v --diff --config-file=.php_cs
$ composer cs-fix
```

If you allow php-cs-fixer to fix CS issues, please re-run the tests to ensure
If you allow `phpcbf` to fix CS issues, please re-run the tests to ensure
they pass, and make sure you add and commit the changes after verification.

## Recommended Workflow for Contributions
Expand Down
17 changes: 14 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,18 @@
}
},
"require-dev": {
"fabpot/php-cs-fixer": "1.7.*",
"phpunit/PHPUnit": "~4.0"
"phpunit/PHPUnit": "~4.0",
"zendframework/zend-coding-standard": "~1.0.0"
},
"scripts": {
"check": [
"@cs-check",
"@test"
],
"cs-check": "phpcs",
"cs-fix": "phpcbf",
"test": "phpunit --colors=always",
"test-coverage": "phpunit --colors=always --coverage-clover clover.xml",
"upload-coverage": "coveralls -v"
}
}
}
8 changes: 8 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0"?>
<ruleset name="Zend Framework coding standard">
<rule ref="./vendor/zendframework/zend-coding-standard/ruleset.xml"/>

<!-- Paths to check -->
<file>src</file>
<file>test</file>
</ruleset>
9 changes: 8 additions & 1 deletion src/Css2Xpath.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@ class Css2Xpath
*/
public static function transform($path)
{
trigger_error(sprintf('%s is deprecated; please use %s\Document\Query::cssToXpath instead', __METHOD__, __NAMESPACE__), E_USER_DEPRECATED);
trigger_error(
sprintf(
'%s is deprecated; please use %s\Document\Query::cssToXpath instead',
__METHOD__,
__NAMESPACE__
),
E_USER_DEPRECATED
);
return Document\Query::cssToXpath($path);
}
}
11 changes: 6 additions & 5 deletions src/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ class Document
* Constructor
*
* @param string|null $document String containing the document
* @param string|null $type Force the document to be of a certain type, bypassing setStringDocument's detection
* @param string|null $type Force the document to be of a certain type,
* bypassing setStringDocument's detection
* @param string|null $encoding Encoding for the document (used for DOMDocument generation)
*/
public function __construct($document = null, $type = null, $encoding = null)
Expand Down Expand Up @@ -111,9 +112,9 @@ protected function setStringDocument($document, $forcedType = null, $forcedEncod

// Unsetting previously registered DOMDocument
$this->domDocument = null;
$this->stringDocument = !empty($document) ? $document : null;
$this->stringDocument = ! empty($document) ? $document : null;

$this->setType($forcedType ?: (!empty($document) ? $type : null));
$this->setType($forcedType ?: (! empty($document) ? $type : null));
$this->setEncoding($forcedEncoding);
$this->setErrors([]);

Expand Down Expand Up @@ -256,15 +257,15 @@ protected function getDomDocumentFromString($stringDocument)
}

$errors = libxml_get_errors();
if (!empty($errors)) {
if (! empty($errors)) {
$this->setErrors($errors);
libxml_clear_errors();
}

libxml_disable_entity_loader(false);
libxml_use_internal_errors(false);

if (!$success) {
if (! $success) {
throw new Exception\RuntimeException(sprintf('Error parsing document (type == %s)', $type));
}

Expand Down
16 changes: 13 additions & 3 deletions src/Document/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@ class Query
* @param \DOMNode $contextNode
* @return NodeList
*/
public static function execute($expression, Document $document, $type = self::TYPE_XPATH, \DOMNode $contextNode = null)
{
public static function execute(
$expression,
Document $document,
$type = self::TYPE_XPATH,
\DOMNode $contextNode = null
) {
// Expression check
if ($type === static::TYPE_CSS) {
$expression = static::cssToXpath($expression);
Expand All @@ -48,7 +52,11 @@ public static function execute($expression, Document $document, $type = self::TY

if ($xpathPhpfunctions = $document->getXpathPhpFunctions()) {
$xpath->registerNamespace('php', 'http://php.net/xpath');
($xpathPhpfunctions === true) ? $xpath->registerPHPFunctions() : $xpath->registerPHPFunctions($xpathPhpfunctions);
if ($xpathPhpfunctions === true) {
$xpath->registerPhpFunctions();
} else {
$xpath->registerPhpFunctions($xpathPhpfunctions);
}
}

$nodeList = $xpath->queryWithErrorException($expression, $contextNode);
Expand Down Expand Up @@ -109,6 +117,7 @@ public static function cssToXpath($path)
return implode('|', $paths);
}

// @codingStandardsIgnoreStart
/**
* Tokenize CSS expressions to XPath
*
Expand All @@ -117,6 +126,7 @@ public static function cssToXpath($path)
*/
protected static function _tokenize($expression)
{
// @codingStandardsIgnoreEnd
// Child selectors
$expression = str_replace('>', '/', $expression);

Expand Down
9 changes: 7 additions & 2 deletions src/NodeList.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,13 @@ class NodeList implements Iterator, Countable, ArrayAccess
* @param DOMNodeList $nodeList
* @param DOMNode|null $contextNode
*/
public function __construct($cssQuery, $xpathQuery, DOMDocument $document, DOMNodeList $nodeList, DOMNode $contextNode = null)
{
public function __construct(
$cssQuery,
$xpathQuery,
DOMDocument $document,
DOMNodeList $nodeList,
DOMNode $contextNode = null
) {
$this->cssQuery = $cssQuery;
$this->xpathQuery = $xpathQuery;
$this->document = $document;
Expand Down
4 changes: 2 additions & 2 deletions src/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,14 +257,14 @@ public function queryXpath($xpathQuery, $query = null, DOMNode $contextNode = nu
break;
}
$errors = libxml_get_errors();
if (!empty($errors)) {
if (! empty($errors)) {
$this->documentErrors = $errors;
libxml_clear_errors();
}
libxml_disable_entity_loader(false);
libxml_use_internal_errors(false);

if (!$success) {
if (! $success) {
throw new Exception\RuntimeException(sprintf('Error parsing document (type == %s)', $type));
}

Expand Down
2 changes: 2 additions & 0 deletions test/Document/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,14 @@ public function testClassNotationWithoutSpecifiedTagShouldResultInMultipleQuerie
{
$test = Query::cssToXpath('div.foo .bar a .baz span');
$this->assertContains('|', $test);
// @codingStandardsIgnoreStart
$segments = [
"//div[contains(concat(' ', normalize-space(@class), ' '), ' foo ')]//*[contains(concat(' ', normalize-space(@class), ' '), ' bar ')]//a//*[contains(concat(' ', normalize-space(@class), ' '), ' baz ')]//span",
"//div[contains(concat(' ', normalize-space(@class), ' '), ' foo ')]//*[contains(concat(' ', normalize-space(@class), ' '), ' bar ')]//a[contains(concat(' ', normalize-space(@class), ' '), ' baz ')]//span",
"//div[contains(concat(' ', normalize-space(@class), ' '), ' foo ')][contains(concat(' ', normalize-space(@class), ' '), ' bar ')]//a//*[contains(concat(' ', normalize-space(@class), ' '), ' baz ')]//span",
"//div[contains(concat(' ', normalize-space(@class), ' '), ' foo ')][contains(concat(' ', normalize-space(@class), ' '), ' bar ')]//a[contains(concat(' ', normalize-space(@class), ' '), ' baz ')]//span",
];
// @codingStandardsIgnoreEnd
foreach ($segments as $xpath) {
$this->assertContains($xpath, $test);
}
Expand Down
33 changes: 27 additions & 6 deletions test/DocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,10 @@ public function testXpathPhpFunctionsShouldBeDisabledByDefault()
{
$this->loadHtml();
try {
$result = Document\Query::execute('//meta[php:functionString("strtolower", @http-equiv) = "content-type"]', $this->document);
$result = Document\Query::execute(
'//meta[php:functionString("strtolower", @http-equiv) = "content-type"]',
$this->document
);
} catch (\Exception $e) {
return;
}
Expand All @@ -206,7 +209,10 @@ public function testXpathPhpFunctionsShouldBeEnabledWithoutParameter()
{
$this->loadHtml();
$this->document->registerXpathPhpFunctions();
$result = Document\Query::execute('//meta[php:functionString("strtolower", @http-equiv) = "content-type"]', $this->document);
$result = Document\Query::execute(
'//meta[php:functionString("strtolower", @http-equiv) = "content-type"]',
$this->document
);
$this->assertEquals(
'content-type',
strtolower($result->current()->getAttribute('http-equiv'))
Expand All @@ -218,7 +224,10 @@ public function testXpathPhpFunctionsShouldBeNotCalledWhenSpecifiedFunction()
$this->loadHtml();
try {
$this->document->registerXpathPhpFunctions('stripos');
$result = Document\Query::execute('//meta[php:functionString("strtolower", @http-equiv) = "content-type"]', $this->document);
$result = Document\Query::execute(
'//meta[php:functionString("strtolower", @http-equiv) = "content-type"]',
$this->document
);
} catch (\Exception $e) {
// $e->getMessage() - Not allowed to call handler 'strtolower()
return;
Expand Down Expand Up @@ -259,11 +268,23 @@ public function testCssSelectorShouldFindNodesWhenMatchingMultipleAttributes()
HTML;

$this->document = new Document($html);
$result = Document\Query::execute('input[type="hidden"][value="1"]', $this->document, Document\Query::TYPE_CSS);
$result = Document\Query::execute(
'input[type="hidden"][value="1"]',
$this->document,
Document\Query::TYPE_CSS
);
$this->assertEquals(2, count($result));
$result = Document\Query::execute('input[value="1"][type~="hidden"]', $this->document, Document\Query::TYPE_CSS);
$result = Document\Query::execute(
'input[value="1"][type~="hidden"]',
$this->document,
Document\Query::TYPE_CSS
);
$this->assertEquals(2, count($result));
$result = Document\Query::execute('input[type="hidden"][value="0"]', $this->document, Document\Query::TYPE_CSS);
$result = Document\Query::execute(
'input[type="hidden"][value="0"]',
$this->document,
Document\Query::TYPE_CSS
);
$this->assertEquals(1, count($result));
}

Expand Down