Skip to content

Commit 53edd54

Browse files
xificurkondrejmirtes
authored andcommitted
DoNotExtendNetteObjectRule: check only direct inherintance, add test, fix PHP 7.2 compatibility.
1 parent a8f79aa commit 53edd54

File tree

5 files changed

+77
-1
lines changed

5 files changed

+77
-1
lines changed

phpstan.neon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
parameters:
2+
excludes_analyse:
3+
- %rootDir%/../../../tests/notAutoloaded/*
4+
25
ignoreErrors:
36
- '#PHPUnit_Framework_MockObject_MockObject given#'
7+
- '#Access to an undefined property PHPUnit_Framework_MockObject_MockObject::\$[a-zA-Z0-9_]+#'

src/Rule/Nette/DoNotExtendNetteObjectRule.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ public function processNode(Node $node, Scope $scope): array
4343
}
4444

4545
$classReflection = $this->broker->getClass($className);
46-
if ($classReflection->isSubclassOf('Nette\Object')) {
46+
$parentClass = $classReflection->getNativeReflection()->getParentClass();
47+
if ($parentClass !== false && $parentClass->getName() === 'Nette\Object') {
4748
return [
4849
sprintf(
4950
"Class %s extends %s - it's better to use %s trait.",
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rule\Nette;
4+
5+
use PhpParser\Node;
6+
use PHPStan\Analyser\Scope;
7+
use PHPStan\Broker\Broker;
8+
use PHPStan\Reflection\ClassReflection;
9+
10+
class DoNotExtendNetteObjectRuleTest extends \PHPUnit\Framework\TestCase
11+
{
12+
13+
/** @var \PHPStan\Rule\Nette\DoNotExtendNetteObjectRule */
14+
private $rule;
15+
16+
protected function setUp()
17+
{
18+
$broker = $this->createMock(Broker::class);
19+
$broker->method('hasClass')->will($this->returnValue(true));
20+
$broker->method('getClass')->will($this->returnCallback(function (string $className) {
21+
$nativeReflection = new \ReflectionClass($className);
22+
$classReflection = $this->createMock(ClassReflection::class);
23+
$classReflection->method('getNativeReflection')->will($this->returnValue($nativeReflection));
24+
return $classReflection;
25+
}));
26+
27+
$this->rule = new DoNotExtendNetteObjectRule($broker);
28+
}
29+
30+
public function testSmartObjectChild()
31+
{
32+
$scope = $this->createMock(Scope::class);
33+
$node = $this->createMock(Node::class);
34+
$node->namespacedName = 'PHPStan\Tests\SmartObjectChild';
35+
36+
$result = $this->rule->processNode($node, $scope);
37+
38+
$this->assertEmpty($result);
39+
}
40+
41+
public function testNetteObjectChild()
42+
{
43+
if (PHP_VERSION_ID >= 70200) {
44+
$this->markTestSkipped('PHP 7.2 is incompatible with Nette\Object.');
45+
}
46+
$scope = $this->createMock(Scope::class);
47+
$node = $this->createMock(Node::class);
48+
$node->namespacedName = 'PHPStan\Tests\NetteObjectChild';
49+
50+
$result = $this->rule->processNode($node, $scope);
51+
52+
$this->assertSame(['Class PHPStan\Tests\NetteObjectChild extends Nette\Object - it\'s better to use Nette\SmartObject trait.'], $result);
53+
}
54+
55+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php declare(strict_types = 1);
2+
namespace PHPStan\Tests;
3+
4+
class NetteObjectChild extends \Nette\Object
5+
{
6+
7+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php declare(strict_types = 1);
2+
namespace PHPStan\Tests;
3+
4+
class SmartObjectChild
5+
{
6+
7+
use \Nette\SmartObject;
8+
9+
}

0 commit comments

Comments
 (0)