Skip to content

Commit

Permalink
Fix rector deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
SerafimArts committed Sep 10, 2024
1 parent 8359f13 commit 0a71127
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php: [ '7.4', '8.0', '8.1', '8.2', '8.3', '8.4' ]
php: [ '8.1', '8.2', '8.3', '8.4' ]
os: [ ubuntu-latest, macos-latest, windows-latest ]
stability: [ prefer-lowest, prefer-stable ]
steps:
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
],
"require": {
"php": "^8.1",
"phplrt/ast-contracts": "^4.0"
"phplrt/ast-contracts": "^3.7"
},
"autoload": {
"psr-4": {
Expand All @@ -37,8 +37,8 @@
},
"extra": {
"branch-alias": {
"dev-master": "4.x-dev",
"dev-main": "4.x-dev"
"dev-master": "3.x-dev",
"dev-main": "3.x-dev"
}
},
"config": {
Expand Down
31 changes: 17 additions & 14 deletions src/Executor.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,17 @@ class Executor implements ExecutorInterface
private const ERROR_NESTED_ARRAY =
'Nested arrays are not a valid traversable AST structure';

/**
* @var array|VisitorInterface[]
*/
private array $visitors = [];

private bool $stop = false;

public function __construct(
/**
* @var array|VisitorInterface[]
*/
private array $visitors = []
) {}
public function __construct(array $visitors = [])
{
$this->visitors = $visitors;
}

/**
* @param iterable<array-key, object> $nodes
Expand Down Expand Up @@ -224,12 +227,12 @@ protected function traverseArray(array $nodes): array

case \is_array($return):
$error = self::ERROR_ENTER_RETURN_ARRAY;
$error = \sprintf($error, $visitor::class, \gettype($visitor));
$error = \sprintf($error, \get_class($visitor), \gettype($visitor));

throw new BadMethodException($error, static::ERROR_CODE_ARRAY_ENTERING);
default:
$error = self::ERROR_ENTER_RETURN_TYPE;
$error = \sprintf($error, $visitor::class, \gettype($visitor));
$error = \sprintf($error, \get_class($visitor), \gettype($visitor));

throw new BadReturnTypeException($error, static::ERROR_CODE_ARRAY_ENTERING);
}
Expand Down Expand Up @@ -268,7 +271,7 @@ protected function traverseArray(array $nodes): array

default:
$error = self::ERROR_LEAVE_RETURN_TYPE;
$error = \sprintf($error, $visitor::class, \gettype($return));
$error = \sprintf($error, \get_class($visitor), \gettype($return));

throw new BadReturnTypeException($error, static::ERROR_CODE_ARRAY_LEAVING);
}
Expand Down Expand Up @@ -335,7 +338,7 @@ protected function traverseNode(NodeInterface $node): NodeInterface

default:
$error = self::ERROR_ENTER_RETURN_TYPE;
$error = \sprintf($error, $visitor::class, \gettype($return));
$error = \sprintf($error, \get_class($visitor), \gettype($return));

throw new BadReturnTypeException($error, static::ERROR_CODE_NODE_ENTERING);
}
Expand Down Expand Up @@ -370,12 +373,12 @@ protected function traverseNode(NodeInterface $node): NodeInterface

case \is_array($return):
$error = self::ERROR_MODIFY_BY_ARRAY;
$error = \sprintf($error, $visitor::class);
$error = \sprintf($error, \get_class($visitor));

throw new BadReturnTypeException($error, static::ERROR_CODE_NODE_LEAVING);
default:
$error = self::ERROR_LEAVE_RETURN_TYPE;
$error = \sprintf($error, $visitor::class, \gettype($return));
$error = \sprintf($error, \get_class($visitor), \gettype($return));

throw new BadReturnTypeException($error, static::ERROR_CODE_NODE_LEAVING);
}
Expand All @@ -397,12 +400,12 @@ private function updateNodeValue(NodeInterface $node, int|string $key, mixed $va
// @phpstan-ignore-next-line
$node->$key = $value;
} catch (\Error $e) {
if (!\str_starts_with($e->getMessage(), 'Cannot access')) {
if (\strpos($e->getMessage(), 'Cannot access') !== 0) {
throw $e;
}

$error = self::ERROR_READONLY_MODIFY;
$error = \sprintf($error, $key, $node::class);
$error = \sprintf($error, $key, \get_class($node));

throw new AttributeException($error);
}
Expand Down
14 changes: 10 additions & 4 deletions src/Traverser.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,18 @@

class Traverser implements TraverserInterface
{
/**
* @var list<VisitorInterface>
*/
private array $visitors = [];

/**
* @param list<VisitorInterface> $visitors
*/
final public function __construct(
private array $visitors = [],
) {}
final public function __construct(array $visitors = [])
{
$this->visitors = $visitors;
}

public static function through(VisitorInterface ...$visitors): self
{
Expand All @@ -60,7 +66,7 @@ public static function through(VisitorInterface ...$visitors): self

public function with(VisitorInterface $visitor, bool $prepend = false): TraverserInterface
{
$fn = $prepend ? \array_unshift(...) : \array_push(...);
$fn = $prepend ? '\\array_unshift' : '\\array_push';
$fn($this->visitors, $visitor);

return $this;
Expand Down
18 changes: 9 additions & 9 deletions tests/Unit/Mutations/AfterTraversingMutationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@
use Phplrt\Visitor\Visitor;
use PHPUnit\Framework\Attributes\TestDox;

#[TestDox('A set of tests that verify an AST modification using the Visitor::after() method.')]
#[TestDox('A set of tests that verify an AST modification using the Visitor::after() method')]
class AfterTraversingMutationsTest extends TestCase
{
#[TestDox('Modifying a collection of AST nodes using array return')]
public function testUpdateRootsByArrayWhenEntering(): void
{
$actual = $this->traverse($original = $this->nodes(2), new class () extends Visitor {
public function after(iterable $nodes): ?iterable
public function after(iterable $node): ?iterable
{
return \is_array($nodes) ? [] : null;
return \is_array($node) ? [] : null;
}
});

Expand All @@ -30,9 +30,9 @@ public function after(iterable $nodes): ?iterable
public function testUpdateRootByArrayWhenEntering(): void
{
$actual = $this->traverse($original = $this->node(), new class () extends Visitor {
public function after(iterable $nodes): ?iterable
public function after(iterable $node): ?iterable
{
return $nodes instanceof Node && $nodes->getId() === 0 ? [] : $nodes;
return $node instanceof Node && $node->getId() === 0 ? [] : $node;
}
});

Expand All @@ -44,9 +44,9 @@ public function after(iterable $nodes): ?iterable
public function testUpdateRootsByNodeWhenEntering(): void
{
$actual = $this->traverse($original = $this->nodes(2), new class () extends Visitor {
public function after(iterable $nodes): ?iterable
public function after(iterable $node): ?iterable
{
return \is_array($nodes) ? new Node(42) : null;
return \is_array($node) ? new Node(42) : null;
}
});

Expand All @@ -58,9 +58,9 @@ public function after(iterable $nodes): ?iterable
public function testUpdateRootByNodeWhenEntering(): void
{
$actual = $this->traverse($original = $this->node(), new class () extends Visitor {
public function after(iterable $nodes): ?iterable
public function after(iterable $node): ?iterable
{
return $nodes instanceof Node && $nodes->getId() === 0 ? new Node(42) : $nodes;
return $node instanceof Node && $node->getId() === 0 ? new Node(42) : $node;
}
});

Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Mutations/BeforeTraversingMutationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Phplrt\Visitor\Visitor;
use PHPUnit\Framework\Attributes\TestDox;

#[TestDox('A set of tests that verify an AST modification using the Visitor::before() method.')]
#[TestDox('A set of tests that verify an AST modification using the Visitor::before() method')]
class BeforeTraversingMutationsTest extends TestCase
{
#[TestDox('Modifying a collection of AST nodes using array return')]
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Mutations/EnteringMutationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Phplrt\Visitor\Visitor;
use PHPUnit\Framework\Attributes\TestDox;

#[TestDox('A set of tests that verify an AST modification using the Visitor::enter() method.')]
#[TestDox('A set of tests that verify an AST modification using the Visitor::enter() method')]
class EnteringMutationsTest extends TestCase
{
#[TestDox('Modifying a collection of AST nodes using array return')]
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Mutations/LeavingMutationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Phplrt\Visitor\Visitor;
use PHPUnit\Framework\Attributes\TestDox;

#[TestDox('A set of tests that verify an AST modification using the Visitor::leave() method.')]
#[TestDox('A set of tests that verify an AST modification using the Visitor::leave() method')]
class LeavingMutationsTest extends TestCase
{
#[TestDox('Modifying a collection of AST nodes using array return')]
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/TraversableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Phplrt\Visitor\Tests\Unit;

use Phplrt\Visitor\Tests\Unit\Stub\Counter;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\TestDox;

#[TestDox('A set of tests that count the number of passes by nodes.')]
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/VisitorsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Phplrt\Visitor\Traverser;
use PHPUnit\Framework\Attributes\TestDox;

#[TestDox('A set of tests that check the interaction of Visitor instances with the Traversable container.')]
#[TestDox('A set of tests that check the interaction of Visitor instances with the Traversable container')]
class VisitorsTest extends TestCase
{
#[TestDox('Check that the visitor worked if added')]
Expand Down

0 comments on commit 0a71127

Please sign in to comment.