Skip to content

Commit

Permalink
Up phpstan baseline
Browse files Browse the repository at this point in the history
  • Loading branch information
SerafimArts committed Sep 11, 2024
1 parent 0a71127 commit 10b1f54
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 38 deletions.
31 changes: 14 additions & 17 deletions src/Executor.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,14 @@ 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(array $visitors = [])
{
$this->visitors = $visitors;
}
public function __construct(
/**
* @var array<array-key, VisitorInterface>
*/
private readonly array $visitors = [],
) {}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

public static function through(VisitorInterface ...$visitors): self
{
Expand Down
4 changes: 4 additions & 0 deletions src/VisitorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@ public function before(iterable $nodes): ?iterable;

/**
* Called when entering a node.
*
* @return mixed|void will be replaced by the "mixed" type since phplrt 4.0
*/
public function enter(NodeInterface $node);

/**
* Called when leaving a node.
*
* @return mixed|void will be replaced by the "mixed" type since phplrt 4.0
*/
public function leave(NodeInterface $node);

Expand Down
8 changes: 4 additions & 4 deletions tests/Unit/Mutations/EnteringMutationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function testUpdateRootsByArrayWhenEntering(): void
$this->expectExceptionCode(Executor::ERROR_CODE_ARRAY_ENTERING);

$this->traverse($original = $this->nodes(2), new class () extends Visitor {
public function enter(NodeInterface $node)
public function enter(NodeInterface $node): mixed
{
return $node instanceof Node && $node->getId() === 0 ? [] : $node;
}
Expand All @@ -36,7 +36,7 @@ public function testUpdateRootByArrayWhenEntering(): void
$this->expectExceptionCode(Executor::ERROR_CODE_ARRAY_ENTERING);

$this->traverse($original = $this->node(), new class () extends Visitor {
public function enter(NodeInterface $node)
public function enter(NodeInterface $node): mixed
{
return $node instanceof Node && $node->getId() === 0 ? [] : $node;
}
Expand All @@ -47,7 +47,7 @@ public function enter(NodeInterface $node)
public function testUpdateRootsByNodeWhenEntering(): void
{
$actual = $this->traverse($original = $this->nodes(2), new class () extends Visitor {
public function enter(NodeInterface $node)
public function enter(NodeInterface $node): mixed
{
return $node instanceof Node && $node->getId() === 0 ? new Node(42) : $node;
}
Expand All @@ -61,7 +61,7 @@ public function enter(NodeInterface $node)
public function testUpdateRootByNodeWhenEntering(): void
{
$actual = $this->traverse($original = $this->node(), new class () extends Visitor {
public function enter(NodeInterface $node)
public function enter(NodeInterface $node): mixed
{
return $node instanceof Node && $node->getId() === 0 ? new Node(42) : $node;
}
Expand Down
8 changes: 4 additions & 4 deletions tests/Unit/Mutations/LeavingMutationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class LeavingMutationsTest extends TestCase
public function testUpdateRootsByArrayWhenLeaving(): void
{
$actual = $this->traverse($original = $this->nodes(2), new class () extends Visitor {
public function leave(NodeInterface $node)
public function leave(NodeInterface $node): mixed
{
return $node instanceof Node && $node->getId() === 0 ? [] : $node;
}
Expand All @@ -34,7 +34,7 @@ public function testUpdateRootByArrayWhenLeaving(): void
$this->expectException(BadMethodException::class);

$this->traverse($original = $this->node(), new class () extends Visitor {
public function leave(NodeInterface $node)
public function leave(NodeInterface $node): mixed
{
return $node instanceof Node && $node->getId() === 0 ? [] : $node;
}
Expand All @@ -45,7 +45,7 @@ public function leave(NodeInterface $node)
public function testUpdateRootsByNodeWhenLeaving(): void
{
$actual = $this->traverse($original = $this->nodes(2), new class () extends Visitor {
public function leave(NodeInterface $node)
public function leave(NodeInterface $node): mixed
{
return $node instanceof Node && $node->getId() === 0 ? new Node(42) : $node;
}
Expand All @@ -59,7 +59,7 @@ public function leave(NodeInterface $node)
public function testUpdateRootByNodeWhenLeaving(): void
{
$actual = $this->traverse($original = $this->node(), new class () extends Visitor {
public function leave(NodeInterface $node)
public function leave(NodeInterface $node): mixed
{
return $node instanceof Node && $node->getId() === 0 ? new Node(42) : $node;
}
Expand Down
8 changes: 4 additions & 4 deletions tests/Unit/Stub/Counter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@

class Counter implements VisitorInterface
{
public $before = 0;
public $after = 0;
public $enter = 0;
public $leave = 0;
public int $before = 0;
public int $after = 0;
public int $enter = 0;
public int $leave = 0;

public function before(iterable $nodes): ?iterable
{
Expand Down

0 comments on commit 10b1f54

Please sign in to comment.