Skip to content

Commit

Permalink
Fixed Expected node type PhpParser\Node\Stmt\Property, NULL occurred
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Nov 2, 2021
1 parent 1e50372 commit e5f8e72
Show file tree
Hide file tree
Showing 6 changed files with 318 additions and 156 deletions.
47 changes: 4 additions & 43 deletions src/Dependency/ExportedNode/ExportedClassConstantNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,10 @@ class ExportedClassConstantNode implements ExportedNode, JsonSerializable

private string $value;

private bool $public;

private bool $private;

private bool $final;

private ?ExportedPhpDocNode $phpDoc;

public function __construct(string $name, string $value, bool $public, bool $private, bool $final, ?ExportedPhpDocNode $phpDoc)
public function __construct(string $name, string $value)
{
$this->name = $name;
$this->value = $value;
$this->public = $public;
$this->private = $private;
$this->final = $final;
$this->phpDoc = $phpDoc;
}

public function equals(ExportedNode $node): bool
Expand All @@ -36,23 +24,8 @@ public function equals(ExportedNode $node): bool
return false;
}

if ($this->phpDoc === null) {
if ($node->phpDoc !== null) {
return false;
}
} elseif ($node->phpDoc !== null) {
if (!$this->phpDoc->equals($node->phpDoc)) {
return false;
}
} else {
return false;
}

return $this->name === $node->name
&& $this->value === $node->value
&& $this->public === $node->public
&& $this->private === $node->private
&& $this->final === $node->final;
&& $this->value === $node->value;
}

/**
Expand All @@ -63,11 +36,7 @@ public static function __set_state(array $properties): ExportedNode
{
return new self(
$properties['name'],
$properties['value'],
$properties['public'],
$properties['private'],
$properties['final'],
$properties['phpDoc']
$properties['value']
);
}

Expand All @@ -79,11 +48,7 @@ public static function decode(array $data): ExportedNode
{
return new self(
$data['name'],
$data['value'],
$data['public'],
$data['private'],
$data['final'],
$data['phpDoc'] !== null ? ExportedPhpDocNode::decode($data['phpDoc']['data']) : null
$data['value']
);
}

Expand All @@ -98,10 +63,6 @@ public function jsonSerialize()
'data' => [
'name' => $this->name,
'value' => $this->value,
'public' => $this->public,
'private' => $this->private,
'final' => $this->final,
'phpDoc' => $this->phpDoc,
],
];
}
Expand Down
120 changes: 120 additions & 0 deletions src/Dependency/ExportedNode/ExportedClassConstantsNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php declare(strict_types = 1);

namespace PHPStan\Dependency\ExportedNode;

use JsonSerializable;
use PHPStan\Dependency\ExportedNode;

class ExportedClassConstantsNode implements ExportedNode, JsonSerializable
{

/** @var ExportedClassConstantNode[] */
private array $constants;

private bool $public;

private bool $private;

private bool $final;

private ?ExportedPhpDocNode $phpDoc;

/**
* @param ExportedClassConstantNode[] $constants
*/
public function __construct(array $constants, bool $public, bool $private, bool $final, ?ExportedPhpDocNode $phpDoc)
{
$this->constants = $constants;
$this->public = $public;
$this->private = $private;
$this->final = $final;
$this->phpDoc = $phpDoc;
}

public function equals(ExportedNode $node): bool
{
if (!$node instanceof self) {
return false;
}

if ($this->phpDoc === null) {
if ($node->phpDoc !== null) {
return false;
}
} elseif ($node->phpDoc !== null) {
if (!$this->phpDoc->equals($node->phpDoc)) {
return false;
}
} else {
return false;
}

if (count($this->constants) !== count($node->constants)) {
return false;
}

foreach ($this->constants as $i => $constant) {
if (!$constant->equals($node->constants[$i])) {
return false;
}
}

return $this->public === $node->public
&& $this->private === $node->private
&& $this->final === $node->final;
}

/**
* @param mixed[] $properties
* @return self
*/
public static function __set_state(array $properties): ExportedNode
{
return new self(
$properties['constants'],
$properties['public'],
$properties['private'],
$properties['final'],
$properties['phpDoc']
);
}

/**
* @param mixed[] $data
* @return self
*/
public static function decode(array $data): ExportedNode
{
return new self(
array_map(static function (array $constantData): ExportedClassConstantNode {
if ($constantData['type'] !== ExportedClassConstantNode::class) {
throw new \PHPStan\ShouldNotHappenException();
}
return ExportedClassConstantNode::decode($constantData['data']);
}, $data['constants']),
$data['public'],
$data['private'],
$data['final'],
$data['phpDoc'] !== null ? ExportedPhpDocNode::decode($data['phpDoc']['data']) : null
);
}

/**
* @return mixed
*/
#[\ReturnTypeWillChange]
public function jsonSerialize()
{
return [
'type' => self::class,
'data' => [
'constants' => $this->constants,
'public' => $this->public,
'private' => $this->private,
'final' => $this->final,
'phpDoc' => $this->phpDoc,
],
];
}

}
31 changes: 28 additions & 3 deletions src/Dependency/ExportedNode/ExportedClassNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ class ExportedClassNode implements ExportedNode, JsonSerializable
/** @var ExportedTraitUseAdaptation[] */
private array $traitUseAdaptations;

/** @var ExportedNode[] */
private array $statements;

/**
* @param string $name
* @param ExportedPhpDocNode|null $phpDoc
Expand All @@ -36,6 +39,7 @@ class ExportedClassNode implements ExportedNode, JsonSerializable
* @param string[] $implements
* @param string[] $usedTraits
* @param ExportedTraitUseAdaptation[] $traitUseAdaptations
* @param ExportedNode[] $statements
*/
public function __construct(
string $name,
Expand All @@ -45,7 +49,8 @@ public function __construct(
?string $extends,
array $implements,
array $usedTraits,
array $traitUseAdaptations
array $traitUseAdaptations,
array $statements
)
{
$this->name = $name;
Expand All @@ -56,6 +61,7 @@ public function __construct(
$this->implements = $implements;
$this->usedTraits = $usedTraits;
$this->traitUseAdaptations = $traitUseAdaptations;
$this->statements = $statements;
}

public function equals(ExportedNode $node): bool
Expand Down Expand Up @@ -87,6 +93,18 @@ public function equals(ExportedNode $node): bool
}
}

if (count($this->statements) !== count($node->statements)) {
return false;
}

foreach ($this->statements as $i => $statement) {
if ($statement->equals($node->statements[$i])) {
continue;
}

return false;
}

return $this->name === $node->name
&& $this->abstract === $node->abstract
&& $this->final === $node->final
Expand All @@ -109,7 +127,8 @@ public static function __set_state(array $properties): ExportedNode
$properties['extends'],
$properties['implements'],
$properties['usedTraits'],
$properties['traitUseAdaptations']
$properties['traitUseAdaptations'],
$properties['statements']
);
}

Expand All @@ -130,6 +149,7 @@ public function jsonSerialize()
'implements' => $this->implements,
'usedTraits' => $this->usedTraits,
'traitUseAdaptations' => $this->traitUseAdaptations,
'statements' => $this->statements,
],
];
}
Expand All @@ -153,7 +173,12 @@ public static function decode(array $data): ExportedNode
throw new \PHPStan\ShouldNotHappenException();
}
return ExportedTraitUseAdaptation::decode($traitUseAdaptationData['data']);
}, $data['traitUseAdaptations'])
}, $data['traitUseAdaptations']),
array_map(static function (array $node): ExportedNode {
$nodeType = $node['type'];

return $nodeType::decode($node['data']);
}, $data['statements']),
);
}

Expand Down
30 changes: 27 additions & 3 deletions src/Dependency/ExportedNode/ExportedInterfaceNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,21 @@ class ExportedInterfaceNode implements ExportedNode, JsonSerializable
/** @var string[] */
private array $extends;

/** @var ExportedNode[] */
private array $statements;

/**
* @param string $name
* @param ExportedPhpDocNode|null $phpDoc
* @param string[] $extends
* @param ExportedNode[] $statements
*/
public function __construct(string $name, ?ExportedPhpDocNode $phpDoc, array $extends)
public function __construct(string $name, ?ExportedPhpDocNode $phpDoc, array $extends, array $statements)
{
$this->name = $name;
$this->phpDoc = $phpDoc;
$this->extends = $extends;
$this->statements = $statements;
}

public function equals(ExportedNode $node): bool
Expand All @@ -45,6 +50,18 @@ public function equals(ExportedNode $node): bool
return false;
}

if (count($this->statements) !== count($node->statements)) {
return false;
}

foreach ($this->statements as $i => $statement) {
if ($statement->equals($node->statements[$i])) {
continue;
}

return false;
}

return $this->name === $node->name
&& $this->extends === $node->extends;
}
Expand All @@ -58,7 +75,8 @@ public static function __set_state(array $properties): ExportedNode
return new self(
$properties['name'],
$properties['phpDoc'],
$properties['extends']
$properties['extends'],
$properties['statements']
);
}

Expand All @@ -74,6 +92,7 @@ public function jsonSerialize()
'name' => $this->name,
'phpDoc' => $this->phpDoc,
'extends' => $this->extends,
'statements' => $this->statements,
],
];
}
Expand All @@ -87,7 +106,12 @@ public static function decode(array $data): ExportedNode
return new self(
$data['name'],
$data['phpDoc'] !== null ? ExportedPhpDocNode::decode($data['phpDoc']['data']) : null,
$data['extends']
$data['extends'],
array_map(static function (array $node): ExportedNode {
$nodeType = $node['type'];

return $nodeType::decode($node['data']);
}, $data['statements']),
);
}

Expand Down
Loading

0 comments on commit e5f8e72

Please sign in to comment.