From b7f5545d42ea83e73216364b4c0a97f03753c4d9 Mon Sep 17 00:00:00 2001 From: Ivan Shcherbak Date: Thu, 11 Jan 2024 11:58:34 +0200 Subject: [PATCH] Move to php 8.1 (#292) * Add code style tool * Run Code style check in CI * Fix code style * Remove getTranslator method * Update to php 8.1 * Reformat code according to php 8.1 --- .github/workflows/test.yaml | 5 +- .idea/ElementFinder.iml | 2 + .idea/php.xml | 7 +- composer.json | 5 +- ecs.php | 21 ++++++ rector.php | 2 +- src/Collection/ElementCollection.php | 20 ++---- .../StringFilter/RegexStringFilter.php | 9 +-- .../StringFilter/StringFilterInterface.php | 3 - .../Modify/StringModify/RegexReplace.php | 10 ++- src/Collection/ObjectCollection.php | 20 ++---- src/Collection/StringCollection.php | 29 +++----- .../CssExpressionTranslator.php | 9 +-- .../CssOrXpathExpressionTranslator.php | 28 ++------ .../DomNodeListActionInterface.php | 1 + src/DomNodeListAction/RemoveNodes.php | 3 +- src/ElementFinder.php | 31 +++----- src/ElementFinder/Element.php | 1 + src/ElementFinderInterface.php | 5 -- .../ExpressionTranslatorInterface.php | 9 ++- src/ExpressionTranslator/XpathExpression.php | 4 +- src/Helper/FormHelper.php | 6 +- src/Helper/NodeHelper.php | 3 +- src/Helper/StringHelper.php | 2 +- tests/Collection/Dummy/JoinedBy.php | 5 +- tests/Collection/Dummy/WithLetterFilter.php | 5 +- tests/Collection/ElementCollectionTest.php | 23 +++--- .../StringFilter/RegexStringFilterTest.php | 5 +- .../Modify/StringModify/RegexReplaceTest.php | 2 +- tests/Collection/ObjectCollectionTest.php | 15 ++-- tests/Collection/StringCollectionTest.php | 70 +++++++++---------- .../CssExpressionTranslatorTest.php | 7 +- .../CssOrXpathExpressionTranslatorTest.php | 15 ++-- tests/ElementFinderTest.php | 47 +------------ .../XpathExpressionTest.php | 4 +- tests/Helper/FormHelperTest.php | 2 - tests/Helper/StringHelperTest.php | 2 +- 37 files changed, 167 insertions(+), 270 deletions(-) create mode 100644 ecs.php diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 6e2d887..8d0b586 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -15,7 +15,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - php-versions: ['8.0', '8.1', '8.2', '8.3'] + php-versions: [ '8.1', '8.2', '8.3'] name: PHP ${{ matrix.php-versions }} steps: - name: Checkout @@ -45,3 +45,6 @@ jobs: - name: Run rector run: ./vendor/bin/rector --dry-run + + - name: Run code style check + run: ./vendor/bin/ecs diff --git a/.idea/ElementFinder.iml b/.idea/ElementFinder.iml index 75c480b..1d1865b 100644 --- a/.idea/ElementFinder.iml +++ b/.idea/ElementFinder.iml @@ -25,6 +25,8 @@ + + diff --git a/.idea/php.xml b/.idea/php.xml index fee9c7f..b0deb3c 100644 --- a/.idea/php.xml +++ b/.idea/php.xml @@ -8,7 +8,6 @@ - @@ -73,6 +72,12 @@ + + + + + + diff --git a/composer.json b/composer.json index 3b4062a..033d134 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ } ], "require": { - "php": "^8.0", + "php": "^8.1", "ext-dom": "*", "ext-libxml": "*", "symfony/css-selector": "^5.2" @@ -25,7 +25,8 @@ "require-dev": { "friendsofphp/php-cs-fixer": "^3.16", "phpunit/phpunit": "^9.5.2", - "rector/rector": "^0.19.0" + "rector/rector": "^0.19", + "symplify/easy-coding-standard": "^12.1" }, "autoload": { "psr-4": { diff --git a/ecs.php b/ecs.php new file mode 100644 index 0000000..b3c703e --- /dev/null +++ b/ecs.php @@ -0,0 +1,21 @@ +paths([ + __DIR__ . '/src', + __DIR__ . '/tests', + __FILE__, + ]); + + $ecsConfig->rules([NoUnusedImportsFixer::class, VoidReturnFixer::class]); + + // this way you can add sets - group of rules + $ecsConfig->sets([SetList::SPACES, SetList::ARRAY, SetList::DOCBLOCK, SetList::NAMESPACES, SetList::COMMENTS, SetList::PSR_12]); +}; diff --git a/rector.php b/rector.php index f8f5abe..9dadc0b 100644 --- a/rector.php +++ b/rector.php @@ -17,5 +17,5 @@ $rectorConfig->rule(TypedPropertyFromStrictConstructorRector::class); $rectorConfig->rule(SeparateMultiUseImportsRector::class); $rectorConfig->importNames(); - $rectorConfig->sets([LevelSetList::UP_TO_PHP_80, PHPUnitSetList::PHPUNIT_90]); + $rectorConfig->sets([LevelSetList::UP_TO_PHP_81, PHPUnitSetList::PHPUNIT_90]); }; diff --git a/src/Collection/ElementCollection.php b/src/Collection/ElementCollection.php index 7e66b72..836843b 100644 --- a/src/Collection/ElementCollection.php +++ b/src/Collection/ElementCollection.php @@ -4,11 +4,11 @@ namespace Xparse\ElementFinder\Collection; -use IteratorAggregate; +use ArrayIterator; use Countable; use InvalidArgumentException; +use IteratorAggregate; use Traversable; -use ArrayIterator; use Xparse\ElementFinder\ElementFinder\Element; /** @@ -21,16 +21,15 @@ class ElementCollection implements IteratorAggregate, Countable */ private $validated = false; - /** * @param Element[] $items * @throws InvalidArgumentException */ - public function __construct(private array $items = []) - { + public function __construct( + private readonly array $items = [] + ) { } - /** * @throws InvalidArgumentException */ @@ -39,7 +38,6 @@ final public function count(): int return \count($this->all()); } - /** * @throws InvalidArgumentException */ @@ -64,7 +62,6 @@ final public function first(): ?Element return reset($items); } - /** * @throws InvalidArgumentException */ @@ -73,16 +70,15 @@ final public function get(int $index): ?Element return $this->all()[$index] ?? null; } - /** * @return Element[] * @throws InvalidArgumentException */ final public function all(): array { - if (!$this->validated) { + if (! $this->validated) { foreach ($this->items as $key => $item) { - if (!$item instanceof Element) { + if (! $item instanceof Element) { $className = ($item === null) ? \gettype($item) : $item::class; throw new InvalidArgumentException( sprintf( @@ -107,7 +103,6 @@ final public function merge(ElementCollection $collection): ElementCollection return new ElementCollection(array_merge($this->all(), $collection->all())); } - /** * @throws InvalidArgumentException */ @@ -118,7 +113,6 @@ final public function add(Element $element): ElementCollection return new ElementCollection($items); } - /** * Retrieve an external iterator * diff --git a/src/Collection/Filters/StringFilter/RegexStringFilter.php b/src/Collection/Filters/StringFilter/RegexStringFilter.php index 70c38d2..f4d5589 100644 --- a/src/Collection/Filters/StringFilter/RegexStringFilter.php +++ b/src/Collection/Filters/StringFilter/RegexStringFilter.php @@ -4,16 +4,13 @@ namespace Xparse\ElementFinder\Collection\Filters\StringFilter; -/** - * - */ class RegexStringFilter implements StringFilterInterface { - public function __construct(private string $regex) - { + public function __construct( + private readonly string $regex + ) { } - final public function valid(string $input): bool { return preg_match($this->regex, $input) === 1; diff --git a/src/Collection/Filters/StringFilter/StringFilterInterface.php b/src/Collection/Filters/StringFilter/StringFilterInterface.php index 4689427..396b472 100644 --- a/src/Collection/Filters/StringFilter/StringFilterInterface.php +++ b/src/Collection/Filters/StringFilter/StringFilterInterface.php @@ -4,9 +4,6 @@ namespace Xparse\ElementFinder\Collection\Filters\StringFilter; -/** - * - */ interface StringFilterInterface { public function valid(string $input): bool; diff --git a/src/Collection/Modify/StringModify/RegexReplace.php b/src/Collection/Modify/StringModify/RegexReplace.php index d50d84a..07334fa 100644 --- a/src/Collection/Modify/StringModify/RegexReplace.php +++ b/src/Collection/Modify/StringModify/RegexReplace.php @@ -4,16 +4,14 @@ namespace Xparse\ElementFinder\Collection\Modify\StringModify; -/** - * - */ class RegexReplace implements StringModifyInterface { - public function __construct(private string $from, private string $to) - { + public function __construct( + private readonly string $from, + private readonly string $to + ) { } - final public function modify(string $input): string { return preg_replace($this->from, $this->to, $input); diff --git a/src/Collection/ObjectCollection.php b/src/Collection/ObjectCollection.php index 874236f..6fb1682 100644 --- a/src/Collection/ObjectCollection.php +++ b/src/Collection/ObjectCollection.php @@ -4,12 +4,12 @@ namespace Xparse\ElementFinder\Collection; -use IteratorAggregate; +use ArrayIterator; use Countable; use Exception; use InvalidArgumentException; +use IteratorAggregate; use Traversable; -use ArrayIterator; use Xparse\ElementFinder\ElementFinderInterface; /** @@ -22,16 +22,15 @@ class ObjectCollection implements IteratorAggregate, Countable */ private $validated = false; - /** * @param ElementFinderInterface[] $items * @throws Exception */ - public function __construct(private array $items = []) - { + public function __construct( + private readonly array $items = [] + ) { } - /** * @throws InvalidArgumentException */ @@ -40,7 +39,6 @@ final public function count(): int return \count($this->all()); } - /** * @throws InvalidArgumentException */ @@ -65,16 +63,15 @@ final public function first(): ?ElementFinderInterface return reset($items); } - /** * @return ElementFinderInterface[] * @throws InvalidArgumentException */ final public function all(): array { - if (!$this->validated) { + if (! $this->validated) { foreach ($this->items as $key => $item) { - if (!$item instanceof ElementFinderInterface) { + if (! $item instanceof ElementFinderInterface) { $className = ($item === null) ? \gettype($item) : $item::class; throw new InvalidArgumentException( sprintf( @@ -99,7 +96,6 @@ final public function merge(ObjectCollection $collection): ObjectCollection return new ObjectCollection(array_merge($this->all(), $collection->all())); } - /** * @throws Exception */ @@ -110,7 +106,6 @@ final public function add(ElementFinderInterface $element): ObjectCollection return new ObjectCollection($items); } - /** * @throws InvalidArgumentException */ @@ -119,7 +114,6 @@ final public function get(int $index): ?ElementFinderInterface return $this->all()[$index] ?? null; } - /** * @return ElementFinderInterface[]|Traversable * @throws InvalidArgumentException diff --git a/src/Collection/StringCollection.php b/src/Collection/StringCollection.php index a51b0bd..b44640e 100644 --- a/src/Collection/StringCollection.php +++ b/src/Collection/StringCollection.php @@ -4,12 +4,12 @@ namespace Xparse\ElementFinder\Collection; -use IteratorAggregate; +use ArrayIterator; use Countable; use Exception; use InvalidArgumentException; +use IteratorAggregate; use Traversable; -use ArrayIterator; use Xparse\ElementFinder\Collection\Filters\StringFilter\StringFilterInterface; use Xparse\ElementFinder\Collection\Modify\StringModify\StringModifyInterface; @@ -21,14 +21,13 @@ class StringCollection implements IteratorAggregate, Countable /** * @var string[] */ - private array $items; + private readonly array $items; /** * @var bool */ private $validated = false; - /** * @param string[] $items */ @@ -37,7 +36,6 @@ public function __construct(array $items = []) $this->items = array_values($items); } - /** * @throws Exception */ @@ -46,7 +44,6 @@ final public function count(): int return \count($this->all()); } - /** * @throws Exception */ @@ -56,7 +53,7 @@ final public function last(): ?string if (\count($items) === 0) { return null; } - return (string)end($items); + return (string) end($items); } /** @@ -68,7 +65,7 @@ final public function first(): ?string if (\count($items) === 0) { return null; } - return (string)reset($items); + return (string) reset($items); } /** @@ -77,9 +74,9 @@ final public function first(): ?string */ final public function all(): array { - if (!$this->validated) { + if (! $this->validated) { foreach ($this->items as $key => $item) { - if (!\is_string($item)) { + if (! \is_string($item)) { throw new InvalidArgumentException( sprintf('Expect string. Check %s item', $key) ); @@ -90,7 +87,6 @@ final public function all(): array return $this->items; } - /** * @throws Exception */ @@ -103,7 +99,6 @@ final public function map(StringModifyInterface $modifier): StringCollection return new StringCollection($items); } - /** * @throws Exception */ @@ -118,7 +113,6 @@ final public function filter(StringFilterInterface $filter): StringCollection return new StringCollection($items); } - /** * @throws Exception */ @@ -131,7 +125,6 @@ final public function replace(string $regexp, string $to): StringCollection return new StringCollection($result); } - /** * @throws Exception */ @@ -141,7 +134,7 @@ final public function match(string $regexp, int $index = 1): StringCollection foreach ($this->all() as $string) { preg_match_all($regexp, $string, $matchedData); if (isset($matchedData[$index])) { - foreach ((array)$matchedData[$index] as $matchedString) { + foreach ((array) $matchedData[$index] as $matchedString) { $result[] = $matchedString; } } @@ -149,7 +142,6 @@ final public function match(string $regexp, int $index = 1): StringCollection return new StringCollection($result); } - /** * @throws Exception */ @@ -164,7 +156,6 @@ final public function split(string $regexp): StringCollection return new StringCollection($items); } - /** * @throws Exception */ @@ -173,7 +164,6 @@ final public function unique(): StringCollection return new StringCollection(array_unique($this->all())); } - /** * @throws Exception */ @@ -182,7 +172,6 @@ final public function merge(StringCollection $collection): StringCollection return new StringCollection(array_merge($this->all(), $collection->all())); } - /** * @throws Exception */ @@ -193,7 +182,6 @@ final public function add(string $item): StringCollection return new StringCollection($items); } - /** * @throws Exception */ @@ -202,7 +190,6 @@ final public function get(int $index): ?string return $this->all()[$index] ?? null; } - /** * @link http://php.net/manual/en/iteratoraggregate.getiterator.php * @return string[]|Traversable diff --git a/src/CssExpressionTranslator/CssExpressionTranslator.php b/src/CssExpressionTranslator/CssExpressionTranslator.php index 2f809b9..19d4627 100644 --- a/src/CssExpressionTranslator/CssExpressionTranslator.php +++ b/src/CssExpressionTranslator/CssExpressionTranslator.php @@ -12,13 +12,12 @@ */ class CssExpressionTranslator extends CssSelectorConverter implements ExpressionTranslatorInterface { - - public function convertToXpath(string $expression): string + final public function convertToXpath(string $expression): string { $xpathExpression = []; foreach (explode(', ', $expression) as $part) { preg_match('!(.+) (@.+|.+\(\))$!', $part, $matchExpression); - if (!array_key_exists(2, $matchExpression)) { + if (! array_key_exists(2, $matchExpression)) { $xpathExpression[] = $this->toXPath($part); } else { $xpathExpression[] = $this->toXPath($matchExpression[1]) . '/' . $matchExpression[2]; @@ -26,6 +25,4 @@ public function convertToXpath(string $expression): string } return implode(' | ', $xpathExpression); } - - -} \ No newline at end of file +} diff --git a/src/CssExpressionTranslator/CssOrXpathExpressionTranslator.php b/src/CssExpressionTranslator/CssOrXpathExpressionTranslator.php index 3329105..254e706 100644 --- a/src/CssExpressionTranslator/CssOrXpathExpressionTranslator.php +++ b/src/CssExpressionTranslator/CssOrXpathExpressionTranslator.php @@ -14,28 +14,15 @@ */ class CssOrXpathExpressionTranslator implements ExpressionTranslatorInterface { + public function __construct( + private readonly ExpressionTranslatorInterface $cssTranslator = new CssExpressionTranslator() + ) { + } /** - * @var ?ExpressionTranslatorInterface + * @throws InvalidArgumentException */ - private static $translator; - - - public function __construct(private ExpressionTranslatorInterface $cssTranslator) - { - } - - - public static function getTranslator(): ExpressionTranslatorInterface - { - if (self::$translator === null) { - self::$translator = new CssOrXpathExpressionTranslator(new CssExpressionTranslator()); - } - return self::$translator; - } - - - public function convertToXpath(string $expression): string + final public function convertToXpath(string $expression): string { $expression = trim($expression); if ($expression === '') { @@ -53,5 +40,4 @@ public function convertToXpath(string $expression): string } return $this->cssTranslator->convertToXpath($expression); } - -} \ No newline at end of file +} diff --git a/src/DomNodeListAction/DomNodeListActionInterface.php b/src/DomNodeListAction/DomNodeListActionInterface.php index a2b80ba..a4e04a9 100644 --- a/src/DomNodeListAction/DomNodeListActionInterface.php +++ b/src/DomNodeListAction/DomNodeListActionInterface.php @@ -5,6 +5,7 @@ namespace Xparse\ElementFinder\DomNodeListAction; use DOMNodeList; + interface DomNodeListActionInterface { public function execute(DOMNodeList $nodeList): void; diff --git a/src/DomNodeListAction/RemoveNodes.php b/src/DomNodeListAction/RemoveNodes.php index 50ca783..bf43884 100644 --- a/src/DomNodeListAction/RemoveNodes.php +++ b/src/DomNodeListAction/RemoveNodes.php @@ -4,8 +4,9 @@ namespace Xparse\ElementFinder\DomNodeListAction; -use DOMNodeList; use DOMAttr; +use DOMNodeList; + class RemoveNodes implements DomNodeListActionInterface { final public function execute(DOMNodeList $nodeList): void diff --git a/src/ElementFinder.php b/src/ElementFinder.php index 29de428..80c022e 100644 --- a/src/ElementFinder.php +++ b/src/ElementFinder.php @@ -5,23 +5,23 @@ namespace Xparse\ElementFinder; use DOMDocument; +use DOMElement; +use DOMNodeList; use DomXPath; -use LibXMLError; use Exception; -use DOMElement; -use RuntimeException; use InvalidArgumentException; -use DOMNodeList; +use LibXMLError; +use RuntimeException; use Xparse\ElementFinder\Collection\ElementCollection; use Xparse\ElementFinder\Collection\ObjectCollection; use Xparse\ElementFinder\Collection\StringCollection; use Xparse\ElementFinder\DomNodeListAction\DomNodeListActionInterface; use Xparse\ElementFinder\DomNodeListAction\RemoveNodes; use Xparse\ElementFinder\ElementFinder\Element; -use Xparse\ElementFinder\Helper\NodeHelper; -use Xparse\ElementFinder\Helper\StringHelper; use Xparse\ElementFinder\ExpressionTranslator\ExpressionTranslatorInterface; use Xparse\ElementFinder\ExpressionTranslator\XpathExpression; +use Xparse\ElementFinder\Helper\NodeHelper; +use Xparse\ElementFinder\Helper\StringHelper; /** * @author Ivan Shcherbak @@ -33,16 +33,17 @@ class ElementFinder implements ElementFinderInterface * * @var int */ - public const DOCUMENT_HTML = 0; + final public const DOCUMENT_HTML = 0; /** * Xml document type * * @var int */ - public const DOCUMENT_XML = 1; + final public const DOCUMENT_XML = 1; private int $type; + private DOMDocument $dom; private DomXPath $xpath; @@ -54,7 +55,6 @@ class ElementFinder implements ElementFinderInterface */ private array $loadErrors = []; - /** * Example: * new ElementFinder("
test
", ElementFinder::HTML); @@ -73,13 +73,11 @@ public function __construct( $this->setData($data ?: ''); } - public function __destruct() { unset($this->dom, $this->xpath); } - public function __clone() { $this->dom = clone $this->dom; @@ -103,7 +101,6 @@ final public function content(string $expression, bool $outerContent = false): S return new StringCollection($result); } - /** * You can remove elements and attributes * @@ -117,7 +114,6 @@ final public function remove(string $expression): ElementFinderInterface return $this->modify($expression, new RemoveNodes()); } - final public function modify(string $expression, DomNodeListActionInterface $action): ElementFinderInterface { $result = clone $this; @@ -142,7 +138,6 @@ final public function value(string $expression): StringCollection return new StringCollection($result); } - /** * Return array of keys and values * @@ -162,7 +157,6 @@ final public function keyValue(string $keyExpression, string $valueExpression): return $result; } - /** * @throws Exception * @throws InvalidArgumentException @@ -180,7 +174,7 @@ final public function object(string $expression, bool $outerHtml = false): Objec if (trim($html) === '') { $html = ''; } - if ($this->type === static::DOCUMENT_XML and !str_contains($html, 'type === static::DOCUMENT_XML and ! str_contains($html, '' . $html . ''; } $result[] = new ElementFinder($html, $type, $this->expressionTranslator); @@ -188,7 +182,6 @@ final public function object(string $expression, bool $outerHtml = false): Objec return new ObjectCollection($result); } - /** * @throws InvalidArgumentException */ @@ -202,15 +195,11 @@ final public function element(string $expression): ElementCollection return new ElementCollection($items); } - - /** - */ final public function getLoadErrors(): array { return $this->loadErrors; } - /** * @return $this * @throws Exception diff --git a/src/ElementFinder/Element.php b/src/ElementFinder/Element.php index fcbdf7f..6b6d7b4 100644 --- a/src/ElementFinder/Element.php +++ b/src/ElementFinder/Element.php @@ -5,6 +5,7 @@ namespace Xparse\ElementFinder\ElementFinder; use DOMElement; + /** * @author Ivan Shcherbak */ diff --git a/src/ElementFinderInterface.php b/src/ElementFinderInterface.php index 9febfb3..2de4365 100644 --- a/src/ElementFinderInterface.php +++ b/src/ElementFinderInterface.php @@ -21,7 +21,6 @@ interface ElementFinderInterface */ public function content(string $expression, bool $outerContent = false): StringCollection; - /** * You can remove elements and attributes * @@ -29,7 +28,6 @@ public function content(string $expression, bool $outerContent = false): StringC * $html = $html->remove("//span/@class"); * $html = $html->remove("//input"); * ``` - * */ public function remove(string $expression): ElementFinderInterface; @@ -42,7 +40,6 @@ public function modify(string $expression, DomNodeListActionInterface $action): */ public function value(string $expression): StringCollection; - /** * Return array of keys and values * @@ -50,14 +47,12 @@ public function value(string $expression): StringCollection; */ public function keyValue(string $keyExpression, string $valueExpression): array; - /** * @throws Exception * @throws InvalidArgumentException */ public function object(string $expression, bool $outerHtml = false): ObjectCollection; - /** * @throws InvalidArgumentException */ diff --git a/src/ExpressionTranslator/ExpressionTranslatorInterface.php b/src/ExpressionTranslator/ExpressionTranslatorInterface.php index 6d9befa..0f86e28 100644 --- a/src/ExpressionTranslator/ExpressionTranslatorInterface.php +++ b/src/ExpressionTranslator/ExpressionTranslatorInterface.php @@ -7,12 +7,11 @@ /** * @author Ivan Shcherbak */ -interface ExpressionTranslatorInterface { - +interface ExpressionTranslatorInterface +{ /** * Translate expression to xpath * For example you can use css */ - public function convertToXpath(string $expression) : string; - -} \ No newline at end of file + public function convertToXpath(string $expression): string; +} diff --git a/src/ExpressionTranslator/XpathExpression.php b/src/ExpressionTranslator/XpathExpression.php index 08ea746..d0fbc00 100644 --- a/src/ExpressionTranslator/XpathExpression.php +++ b/src/ExpressionTranslator/XpathExpression.php @@ -9,10 +9,8 @@ */ class XpathExpression implements ExpressionTranslatorInterface { - final public function convertToXpath(string $expression): string { return $expression; } - -} \ No newline at end of file +} diff --git a/src/Helper/FormHelper.php b/src/Helper/FormHelper.php index 6f9649b..befe9ed 100644 --- a/src/Helper/FormHelper.php +++ b/src/Helper/FormHelper.php @@ -13,11 +13,11 @@ */ class FormHelper { - public function __construct(private ElementFinderInterface $page) - { + public function __construct( + private readonly ElementFinderInterface $page + ) { } - /** * Get data from
element * diff --git a/src/Helper/NodeHelper.php b/src/Helper/NodeHelper.php index 94f6194..399aa18 100644 --- a/src/Helper/NodeHelper.php +++ b/src/Helper/NodeHelper.php @@ -4,8 +4,8 @@ namespace Xparse\ElementFinder\Helper; -use DOMNode; use DOMDocument; +use DOMNode; use Xparse\ElementFinder\ElementFinder; /** @@ -26,7 +26,6 @@ final public static function getOuterContent(DOMNode $node, int $documentType): return $content; } - final public static function getInnerContent(DOMNode $itemObj, int $documentType): string { $content = ''; diff --git a/src/Helper/StringHelper.php b/src/Helper/StringHelper.php index 5b7b145..fce1b59 100644 --- a/src/Helper/StringHelper.php +++ b/src/Helper/StringHelper.php @@ -12,7 +12,7 @@ class StringHelper final public static function safeEncodeStr(string $str): string { return preg_replace_callback('/&#([a-z\d]+);/i', static function ($m) { - $value = (string)$m[0]; + $value = (string) $m[0]; $value = mb_convert_encoding($value, 'UTF-8', 'HTML-ENTITIES'); return $value; }, $str); diff --git a/tests/Collection/Dummy/JoinedBy.php b/tests/Collection/Dummy/JoinedBy.php index 37b8da5..86ebce4 100644 --- a/tests/Collection/Dummy/JoinedBy.php +++ b/tests/Collection/Dummy/JoinedBy.php @@ -8,8 +8,9 @@ final class JoinedBy implements StringModifyInterface { - public function __construct(private string $str) - { + public function __construct( + private readonly string $str + ) { } public function modify(string $input): string diff --git a/tests/Collection/Dummy/WithLetterFilter.php b/tests/Collection/Dummy/WithLetterFilter.php index 29e76e1..86d51a5 100644 --- a/tests/Collection/Dummy/WithLetterFilter.php +++ b/tests/Collection/Dummy/WithLetterFilter.php @@ -8,8 +8,9 @@ final class WithLetterFilter implements StringFilterInterface { - public function __construct(private string $letter) - { + public function __construct( + private readonly string $letter + ) { } public function valid(string $input): bool diff --git a/tests/Collection/ElementCollectionTest.php b/tests/Collection/ElementCollectionTest.php index cb2e99b..449dc2d 100644 --- a/tests/Collection/ElementCollectionTest.php +++ b/tests/Collection/ElementCollectionTest.php @@ -13,7 +13,7 @@ */ class ElementCollectionTest extends TestCase { - public function testIterate() + public function testIterate(): void { $collection = new ElementCollection( [ @@ -31,8 +31,7 @@ public function testIterate() self::assertSame(2, $collectedItems); } - - public function testMerge() + public function testMerge(): void { $collection = (new ElementCollection([new Element('a', 'link')]))->merge(new ElementCollection([new Element('b', 'bold')])); self::assertSame(['a.link', 'b.bold'], [ @@ -41,36 +40,31 @@ public function testMerge() ]); } - - public function testGetLast() + public function testGetLast(): void { $collection = new ElementCollection([new Element('a', 'link'), new Element('b', 'bold')]); self::assertSame('b', $collection->last()->tagName); } - - public function testGetLastOnEmptyCollection() + public function testGetLastOnEmptyCollection(): void { $collection = new ElementCollection(); self::assertNull($collection->last()); } - - public function testGetFirst() + public function testGetFirst(): void { $collection = new ElementCollection([new Element('a', 'link'), new Element('b', 'bold')]); self::assertSame('a', $collection->first()->tagName); } - - public function testGetFirstOnEmptyCollection() + public function testGetFirstOnEmptyCollection(): void { $collection = new ElementCollection(); self::assertNull($collection->first()); } - - public function testAdd() + public function testAdd(): void { $collection = new ElementCollection(); $newCollection = $collection->add(new Element('a', 'link')); @@ -79,8 +73,7 @@ public function testAdd() self::assertCount(1, $newCollection); } - - public function testGet() + public function testGet(): void { $collection = new ElementCollection([new Element('span', 'link')]); self::assertSame('span', $collection->get(0)->tagName); diff --git a/tests/Collection/Filters/StringFilter/RegexStringFilterTest.php b/tests/Collection/Filters/StringFilter/RegexStringFilterTest.php index f5291ef..d3e100c 100644 --- a/tests/Collection/Filters/StringFilter/RegexStringFilterTest.php +++ b/tests/Collection/Filters/StringFilter/RegexStringFilterTest.php @@ -7,15 +7,14 @@ class RegexStringFilterTest extends TestCase { - public function testRegexSuccess() + public function testRegexSuccess(): void { self::assertTrue( (new RegexStringFilter('!^[a-z]+$!'))->valid('test') ); } - - public function testRegexFailure() + public function testRegexFailure(): void { self::assertFalse( (new RegexStringFilter('![0-9]+$!'))->valid('123user') diff --git a/tests/Collection/Modify/StringModify/RegexReplaceTest.php b/tests/Collection/Modify/StringModify/RegexReplaceTest.php index d36ed92..c598676 100644 --- a/tests/Collection/Modify/StringModify/RegexReplaceTest.php +++ b/tests/Collection/Modify/StringModify/RegexReplaceTest.php @@ -8,7 +8,7 @@ class RegexReplaceTest extends TestCase { - public function testReplace() + public function testReplace(): void { $collection = new StringCollection(['test-1', 'test--123', '--3']); $collection = $collection->map(new RegexReplace('!([a-z]+)-+!', '$1::')); diff --git a/tests/Collection/ObjectCollectionTest.php b/tests/Collection/ObjectCollectionTest.php index 13a6d27..1478f21 100644 --- a/tests/Collection/ObjectCollectionTest.php +++ b/tests/Collection/ObjectCollectionTest.php @@ -21,8 +21,7 @@ public function testInvalidObjectIndex(): void self::assertEquals(null, $collection->get(2)); } - - public function testIterate() : void + public function testIterate(): void { $collection = new ObjectCollection( [ @@ -35,14 +34,13 @@ public function testIterate() : void foreach ($collection as $index => $item) { $collectedItems++; $data = $item->content('.')->match('!(.*)!')->first(); - self::assertSame((string)$index, $data); + self::assertSame((string) $index, $data); } self::assertSame(2, $collectedItems); } - - public function testMerge() : void + public function testMerge(): void { $sourceCollection = new ObjectCollection([new ElementFinder('0'), new ElementFinder('1')]); $newCollection = new ObjectCollection([new ElementFinder('0')]); @@ -55,8 +53,7 @@ public function testMerge() : void self::assertSame(['0', '1', '0'], $result); } - - public function testAdd() : void + public function testAdd(): void { $sourceCollection = new ObjectCollection([new ElementFinder('0'), new ElementFinder('1')]); $newCollection = $sourceCollection->add(new ElementFinder('2')); @@ -65,8 +62,7 @@ public function testAdd() : void self::assertSame('2', $newCollection->last()->content('//a')->first()); } - - public function testGet() : void + public function testGet(): void { $collection = new ObjectCollection([new ElementFinder('0'), new ElementFinder('data1')]); self::assertNotNull('data1', $collection->get(0)->content('//b')->first()); @@ -74,7 +70,6 @@ public function testGet() : void self::assertNull($collection->get(2)); } - public function testInvalidDataType(): void { $this->expectException(InvalidArgumentException::class); diff --git a/tests/Collection/StringCollectionTest.php b/tests/Collection/StringCollectionTest.php index e66fe59..2f0b5d3 100644 --- a/tests/Collection/StringCollectionTest.php +++ b/tests/Collection/StringCollectionTest.php @@ -14,15 +14,14 @@ */ class StringCollectionTest extends TestCase { - public function testInvalidObjectIndex() + public function testInvalidObjectIndex(): void { $collection = new StringCollection(['a-1', 'b.2', 'c,3']); self::assertEquals('a-1', $collection->get(0)); self::assertEquals(null, $collection->get(3)); } - - public function testReplace() + public function testReplace(): void { $mainCollection = new StringCollection(['a-1', 'b.2', 'c,3']); $collection = $mainCollection->replace('![-,.]!', '::'); @@ -30,24 +29,21 @@ public function testReplace() self::assertSame(['a-1', 'b.2', 'c,3'], $mainCollection->all()); } - - public function testMatch() + public function testMatch(): void { $collection = new StringCollection(['a-1', 'b.2', 'c,3']); $collection = $collection->match('/[a-z][-,](\d)/'); self::assertSame(['1', '3'], $collection->all()); } - - public function testSplit() + public function testSplit(): void { $collection = new StringCollection(['a-1', 'b.2']); $collection = $collection->split('/[.-]/'); self::assertSame(['a', '1', 'b', '2'], $collection->all()); } - - public function testUnique() + public function testUnique(): void { $collection = new StringCollection(['1', '2', '2']); self::assertCount(3, $collection); @@ -56,8 +52,7 @@ public function testUnique() self::assertCount(2, $collection); } - - public function testIterate() + public function testIterate(): void { $collection = new StringCollection( [ @@ -76,53 +71,57 @@ public function testIterate() self::assertSame(3, $collectedItems); } - - public function testMergeWithItems() + public function testMergeWithItems(): void { $collection = (new StringCollection(['a', 'b']))->merge(new StringCollection(['a', 'c'])); self::assertSame(['a', 'b', 'a', 'c'], $collection->all()); } - - public function testMergeWithoutItems() + public function testMergeWithoutItems(): void { $collection = (new StringCollection())->merge(new StringCollection()); self::assertSame([], $collection->all()); } - - public function testMergeWithPartialItems() + public function testMergeWithPartialItems(): void { - $collection = (new StringCollection([1 => 'a']))->merge(new StringCollection([1 => 'b', 'c'])); + $collection = (new StringCollection([ + 1 => 'a', + ]))->merge(new StringCollection([ + 1 => 'b', + 'c', + ])); self::assertSame(['a', 'b', 'c'], $collection->all()); } - - public function testAdd() + public function testAdd(): void { - $sourceCollection = new StringCollection([1 => 'a']); + $sourceCollection = new StringCollection([ + 1 => 'a', + ]); $newCollection = $sourceCollection->add('b'); self::assertSame(['a'], $sourceCollection->all()); self::assertSame(['a', 'b'], $newCollection->all()); } - - public function testGet() + public function testGet(): void { - $collection = new StringCollection([1 => 'a']); + $collection = new StringCollection([ + 1 => 'a', + ]); self::assertSame('a', $collection->get(0)); self::assertNull($collection->get(1)); } - - public function testGetLast() + public function testGetLast(): void { - $collection = new StringCollection([1 => 'word']); + $collection = new StringCollection([ + 1 => 'word', + ]); self::assertSame('word', $collection->last()); } - - public function testFilter() + public function testFilter(): void { $collection = new StringCollection(['foo', 'bar', 'baz']); $collection = $collection->filter(new WithLetterFilter('a')); @@ -135,8 +134,7 @@ public function testFilter() ); } - - public function testMap() + public function testMap(): void { $collection = new StringCollection(['123', 'abc', 'test']); $collection = $collection->map(new JoinedBy('..')); @@ -162,11 +160,11 @@ public function testLast(array $items, mixed $expected): void public static function lastDataProvider(): array { return [ - [['a', 'b', 'c'], 'c'], - [['a', 'b', 'c', 'd'], 'd'], - [['a', 'b', 'c', 'd', 'e'], 'e'], - [['a', 'b', 'c', 'd', 'e', 'f'], 'f'], - [[], null], + [['a', 'b', 'c'], 'c'], + [['a', 'b', 'c', 'd'], 'd'], + [['a', 'b', 'c', 'd', 'e'], 'e'], + [['a', 'b', 'c', 'd', 'e', 'f'], 'f'], + [[], null], ]; } } diff --git a/tests/CssExpressionTranslator/CssExpressionTranslatorTest.php b/tests/CssExpressionTranslator/CssExpressionTranslatorTest.php index 2e39104..32f651a 100644 --- a/tests/CssExpressionTranslator/CssExpressionTranslatorTest.php +++ b/tests/CssExpressionTranslator/CssExpressionTranslatorTest.php @@ -12,7 +12,6 @@ */ class CssExpressionTranslatorTest extends TestCase { - /** * @return string[][] */ @@ -23,11 +22,10 @@ final public function getConvertWithAttributesDataProvider(): array ['a @a', 'descendant-or-self::a/@a'], ['a text()', 'descendant-or-self::a/text()'], ['a.foo @href', "descendant-or-self::a[@class and contains(concat(' ', normalize-space(@class), ' '), ' foo ')]/@href"], - ['a.foo @href, b.bar @href', "descendant-or-self::a[@class and contains(concat(' ', normalize-space(@class), ' '), ' foo ')]/@href | descendant-or-self::b[@class and contains(concat(' ', normalize-space(@class), ' '), ' bar ')]/@href"] + ['a.foo @href, b.bar @href', "descendant-or-self::a[@class and contains(concat(' ', normalize-space(@class), ' '), ' foo ')]/@href | descendant-or-self::b[@class and contains(concat(' ', normalize-space(@class), ' '), ' bar ')]/@href"], ]; } - /** * @dataProvider getConvertWithAttributesDataProvider */ @@ -38,5 +36,4 @@ final public function testConvertWithAttributes(string $input, string $expect): (new CssExpressionTranslator())->convertToXpath($input) ); } - -} \ No newline at end of file +} diff --git a/tests/CssExpressionTranslator/CssOrXpathExpressionTranslatorTest.php b/tests/CssExpressionTranslator/CssOrXpathExpressionTranslatorTest.php index 741ac82..7b82ab2 100644 --- a/tests/CssExpressionTranslator/CssOrXpathExpressionTranslatorTest.php +++ b/tests/CssExpressionTranslator/CssOrXpathExpressionTranslatorTest.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Xparse\ElementFinder\CssExpressionTranslator\Test; +namespace Test\Xparse\ElementFinder\CssExpressionTranslator; use InvalidArgumentException; use PHPUnit\Framework\TestCase; @@ -13,7 +13,6 @@ */ final class CssOrXpathExpressionTranslatorTest extends TestCase { - /** * @return string[][] */ @@ -91,29 +90,25 @@ public function getQueriesDataProvider(): array ]; } - /** * @dataProvider getQueriesDataProvider */ public function testQueries(string $input, string $expect): void { - $output = CssOrXpathExpressionTranslator::getTranslator() + $output = (new CssOrXpathExpressionTranslator()) ->convertToXpath($input); self::assertEquals($expect, $output); } - public function testEmptyString(): void { self::expectException(InvalidArgumentException::class); - CssOrXpathExpressionTranslator::getTranslator()->convertToXpath(''); + (new CssOrXpathExpressionTranslator())->convertToXpath(''); } - public function testEmptyStringWithSpaces(): void { self::expectException(InvalidArgumentException::class); - CssOrXpathExpressionTranslator::getTranslator()->convertToXpath(' '); + (new CssOrXpathExpressionTranslator())->convertToXpath(' '); } - -} \ No newline at end of file +} diff --git a/tests/ElementFinderTest.php b/tests/ElementFinderTest.php index 46488ec..44b4b93 100644 --- a/tests/ElementFinderTest.php +++ b/tests/ElementFinderTest.php @@ -5,8 +5,8 @@ namespace Test\Xparse\ElementFinder; use InvalidArgumentException; -use RuntimeException; use PHPUnit\Framework\TestCase; +use RuntimeException; use Test\Xparse\ElementFinder\Dummy\ItemsByClassExpressionTranslator; use Xparse\ElementFinder\DomNodeListAction\RemoveNodes; use Xparse\ElementFinder\ElementFinder; @@ -23,14 +23,12 @@ public function testLoad(): void self::assertStringContainsString('test doc', $html->content('.')->first()); } - public function testInvalidType(): void { $this->expectException(InvalidArgumentException::class); new ElementFinder('', -1); } - public function testLoadEmptyDoc(): void { $elementFinder = new ElementFinder(''); @@ -38,10 +36,6 @@ public function testLoadEmptyDoc(): void self::assertEquals('', $elementFinder->value('//*')->get(0)); } - - /** - * - */ public function testLoadDocumentWithZero(): void { self::assertSame( @@ -50,7 +44,6 @@ public function testLoadDocumentWithZero(): void ); } - public function testAttributes(): void { $html = $this->getHtmlTestObject(); @@ -69,7 +62,6 @@ public function testAttributes(): void ); } - public function testObjects(): void { $html = $this->getHtmlTestObject(); @@ -89,7 +81,6 @@ public function testObjects(): void self::assertCount(1, $spanItems); } - public function testModify(): void { $page = new ElementFinder('user'); @@ -109,7 +100,6 @@ public function testObjectWithOuterHtml(): void ); } - public function testDeleteNode(): void { $html = $this->getHtmlTestObject(); @@ -124,7 +114,6 @@ public function testDeleteNode(): void self::assertNull($title); } - public function testDeleteAttribute(): void { $html = $this->getHtmlTestObject(); @@ -139,7 +128,6 @@ public function testDeleteAttribute(): void self::assertNull($title); } - public function testHtmlSelector(): void { $html = $this->getHtmlTestObject(); @@ -155,7 +143,6 @@ public function testHtmlSelector(): void self::assertEquals('', $html->content('//td/@df')->get(0)); } - public function testMatch(): void { $html = $this->getHtmlDataObject(); @@ -173,7 +160,6 @@ public function testMatch(): void self::assertCount(0, $phones); } - public function testMatchWithEmptyElements(): void { self::assertEmpty( @@ -181,7 +167,6 @@ public function testMatchWithEmptyElements(): void ); } - public function testObjectWithInnerContent(): void { # inner @@ -192,7 +177,6 @@ public function testObjectWithInnerContent(): void self::assertStringContainsString('1 ', $spanItems->get(0)->content('.')->first()); } - /** * How would you find all nodes between all H2's? * @@ -201,7 +185,6 @@ public function testObjectWithInnerContent(): void * `$ns1[count(.|$ns2) = count($ns2)]` * * you can select all the nodes that belong both to the node sets $ns1 and $ns2. - * */ public function testGetAllNodesBetweenSiblings(): void { @@ -227,7 +210,6 @@ public function testGetAllNodesBetweenSiblings(): void self::assertEquals($result[3], 'Text 4'); } - public function testInitClassWithInvalidContent(): void { $elementFinder = new ElementFinder(' @@ -246,14 +228,12 @@ public function testInitClassWithInvalidContent(): void self::assertStringContainsString("Unexpected end tag : span\n", $errors[0]->message); } - public function testInitClassWithValidContent(): void { $errors = $this->getHtmlDataObject()->getLoadErrors(); self::assertCount(0, $errors); } - public function testGetObjectWithEmptyHtml(): void { $objects = (new ElementFinder('
'))->object('//div'); @@ -266,17 +246,12 @@ public function testGetObjectWithEmptyHtml(): void self::assertEquals('df', $linkText); } - - /** - * - */ public function testValidDocumentType(): void { $document = new ElementFinder('123', ElementFinder::DOCUMENT_XML); self::assertStringContainsString('123', $document->content('.')->first()); } - public function testFetchTextNode(): void { $html = new ElementFinder(' @@ -289,14 +264,12 @@ public function testFetchTextNode(): void '); - $firstTextNodes = $html->value('//b/following-sibling::text()[1]')->all(); self::assertEquals([ 't1', 'other', ], $firstTextNodes); - $allFollowingSiblingTextNodes = $html->value('//b/following-sibling::text()')->all(); self::assertEquals([ @@ -304,7 +277,6 @@ public function testFetchTextNode(): void ], $allFollowingSiblingTextNodes); } - public function testKeyValue(): void { $html = new ElementFinder(' @@ -334,7 +306,6 @@ public function testKeyValue(): void ], $values); } - public function testKeyValueFail(): void { $html = new ElementFinder(' @@ -358,7 +329,6 @@ public function testKeyValueFail(): void $html->keyValue('//table//td[1]', '//table//td[2]'); } - public function testXmlData(): void { $xml = new ElementFinder($this->getValidXml(), ElementFinder::DOCUMENT_XML); @@ -380,14 +350,12 @@ public function testXmlData(): void self::assertEquals('5.95 USD', $xml->content('.')->match('!replace('!^\\$(.+)!iu', '$1 USD')->first()); } - public function testXmlRootNode(): void { $food = (new ElementFinder($this->getValidXml(), ElementFinder::DOCUMENT_XML))->object('//food')->get(2); - self::assertEquals(900, (int)$food->value('/root/calories')->first()); + self::assertEquals(900, (int) $food->value('/root/calories')->first()); } - public function testLoadXmlWithoutErrors(): void { $xml = new ElementFinder($this->getValidXml(), ElementFinder::DOCUMENT_XML); @@ -395,7 +363,6 @@ public function testLoadXmlWithoutErrors(): void self::assertCount(0, $xml->getLoadErrors()); } - public function testLoadXmlWithErrors(): void { $errors = (new ElementFinder($this->getInvalidXml(), ElementFinder::DOCUMENT_XML))->getLoadErrors(); @@ -404,7 +371,6 @@ public function testLoadXmlWithErrors(): void self::assertStringContainsString('Opening and ending tag mismatch: from', $errors[0]->message); } - public function testXmlRootNodes(): void { $xml = new ElementFinder($this->getInvalidRootNodesXml(), ElementFinder::DOCUMENT_XML); @@ -414,7 +380,6 @@ public function testXmlRootNodes(): void self::assertStringContainsString('Extra content at the end of the document', $errors[0]->message); } - public function testShareExpressionTranslator(): void { $page = new ElementFinder(' @@ -441,19 +406,16 @@ public function testShareExpressionTranslator(): void } } - public function getHtmlTestObject(): ElementFinderInterface { return $this->initFromFile('test.html'); } - public function getHtmlDataObject(): ElementFinderInterface { return $this->initFromFile('data.html'); } - public function getNodeItemsHtmlObject(): ElementFinder { return $this->initFromFile('node-items.html'); @@ -479,20 +441,17 @@ public function testElement(): void } } - private function getDemoDataDirectoryPath(): string { return __DIR__ . '/demo-data/'; } - private function initFromFile(string $file): ElementFinder { $fileData = file_get_contents($this->getDemoDataDirectoryPath() . DIRECTORY_SEPARATOR . $file); return new ElementFinder($fileData); } - private function getInvalidRootNodesXml(): string { return ' @@ -511,7 +470,6 @@ private function getInvalidRootNodesXml(): string '; } - private function getInvalidXml(): string { return ' @@ -524,7 +482,6 @@ private function getInvalidXml(): string '; } - private function getValidXml(): string { return ' diff --git a/tests/ExpressionTranslator/XpathExpressionTest.php b/tests/ExpressionTranslator/XpathExpressionTest.php index 83d9a74..d25fc0d 100644 --- a/tests/ExpressionTranslator/XpathExpressionTest.php +++ b/tests/ExpressionTranslator/XpathExpressionTest.php @@ -4,7 +4,6 @@ namespace Test\Xparse\ElementFinder\ExpressionTranslator; - use PHPUnit\Framework\TestCase; use Xparse\ElementFinder\ExpressionTranslator\XpathExpression; @@ -13,7 +12,6 @@ */ class XpathExpressionTest extends TestCase { - final public function testAssertSameIO(): void { self::assertSame( @@ -21,4 +19,4 @@ final public function testAssertSameIO(): void (new XpathExpression())->convertToXpath('custom-expression') ); } -} \ No newline at end of file +} diff --git a/tests/Helper/FormHelperTest.php b/tests/Helper/FormHelperTest.php index a8165b4..cea4a47 100644 --- a/tests/Helper/FormHelperTest.php +++ b/tests/Helper/FormHelperTest.php @@ -91,7 +91,6 @@ public function testFormData(): void $formData = (new FormHelper(new ElementFinder($html)))->getFormData('//form'); - self::assertSame([ 'comment' => 'Enter you comment here', 'gender' => 'male', @@ -109,7 +108,6 @@ public function testFormData(): void ], $formData); } - public function testInvalidFormPath(): void { $page = new ElementFinder('
'); diff --git a/tests/Helper/StringHelperTest.php b/tests/Helper/StringHelperTest.php index 3e22274..27b9d05 100644 --- a/tests/Helper/StringHelperTest.php +++ b/tests/Helper/StringHelperTest.php @@ -12,7 +12,7 @@ */ class StringHelperTest extends TestCase { - public function testEncode() + public function testEncode(): void { $data = [ 'AA<<' => 'AA<<',