Skip to content
Merged
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
8 changes: 8 additions & 0 deletions Rules/NameNodeHolderFindRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use PHPStan\Rules\Rule;
use PHPStan\Node\InClassNode;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\ArrayType;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;

class NameNodeHolderFindRule implements Rule
Expand All @@ -30,6 +32,12 @@ public function processNode(Node $node, Scope $scope): array
$propertyName = $prop->name->toString();
$propertyReflection = $classReflection->getNativeProperty($propertyName);
$propertyType = $propertyReflection->getReadableType();
if ($propertyType instanceof ArrayType) {
$propertyType = $propertyType->getItemType();
}
if ($propertyType instanceof MixedType) {
continue;
}
$trinary = $propertyType->isSuperTypeOf(new ObjectType('PhpParser\Node\Name'));
if (!$trinary->no()) {
return [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ public function testRule(string $file, array $expected): void
public function data(): array
{
return [
[__DIR__ . '/data/name-holder.php', [['App\DummyFuncCall', 8]]],
[__DIR__ . '/data/non-name-holder.php', []],
[__DIR__ . '/data/name-holder/name-node.php', [['App\DummyFuncCall', 8]]],
[__DIR__ . '/data/name-holder/array-including-name-node.php', [['App\DummyTraitUse', 8]]],
[__DIR__ . '/data/name-holder/union-including-name-node.php', [['App\DummyUnionType', 10]]],
[__DIR__ . '/data/non-name-holder/no-name-node.php', []],
[__DIR__ . '/data/non-name-holder/mixed-type-exists.php', []],
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php declare(strict_types=1);

namespace App;

use PhpParser\Node;
use PhpParser\Node\Stmt\TraitUseAdaptation;

class DummyTraitUse extends Node\Stmt
{
/** @var Node\Name[] Traits */
public $traits;
/** @var TraitUseAdaptation[] Adaptations */
public $adaptations;

/**
* Constructs a trait use node.
*
* @param Node\Name[] $traits Traits
* @param TraitUseAdaptation[] $adaptations Adaptations
* @param array $attributes Additional attributes
*/
public function __construct(array $traits, array $adaptations = [], array $attributes = []) {
$this->attributes = $attributes;
$this->traits = $traits;
$this->adaptations = $adaptations;
}

public function getSubNodeNames() : array {
return ['traits', 'adaptations'];
}

public function getType() : string {
return 'Stmt_TraitUse';
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php declare(strict_types=1);

namespace App;

use PhpParser\Node\ComplexType;
use PhpParser\Node\Identifier;
use PhpParser\Node\IntersectionType;
use PhpParser\Node\Name;

class DummyUnionType extends ComplexType
{
/** @var (Identifier|Name|IntersectionType)[] Types */
public $types;

/**
* Constructs a union type.
*
* @param (Identifier|Name|IntersectionType)[] $types Types
* @param array $attributes Additional attributes
*/
public function __construct(array $types, array $attributes = []) {
$this->attributes = $attributes;
$this->types = $types;
}

public function getSubNodeNames() : array {
return ['types'];
}

public function getType() : string {
return 'UnionType';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php declare(strict_types=1);

namespace App;

use PhpParser\Node\Expr;

class DummyShellExec extends Expr
{
/** @var array Encapsed string array */
public $parts;

/**
* Constructs a shell exec (backtick) node.
*
* @param array $parts Encapsed string array
* @param array $attributes Additional attributes
*/
public function __construct(array $parts, array $attributes = []) {
$this->attributes = $attributes;
$this->parts = $parts;
}

public function getSubNodeNames() : array {
return ['parts'];
}

public function getType() : string {
return 'Expr_ShellExec';
}
}