Skip to content

PHP 7.2 compatibility vol.2 #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 6, 2017
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
5 changes: 5 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
parameters:
excludes_analyse:
- %rootDir%/../../../tests/notAutoloaded/*

ignoreErrors:
- '#PHPUnit_Framework_MockObject_MockObject given#'
- '# but returns PHPUnit_Framework_MockObject_MockObject.#'
- '#Access to an undefined property PHPUnit_Framework_MockObject_MockObject::\$[a-zA-Z0-9_]+#'
15 changes: 13 additions & 2 deletions src/Reflection/Nette/NetteObjectClassReflectionExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class NetteObjectClassReflectionExtension implements MethodsClassReflectionExten

public function hasProperty(ClassReflection $classReflection, string $propertyName): bool
{
if (!$classReflection->isSubclassOf('Nette\Object')) {
if (!$this->inheritsFromNetteObject($classReflection->getNativeReflection())) {
return false;
}

Expand Down Expand Up @@ -57,7 +57,7 @@ public function getProperty(ClassReflection $classReflection, string $propertyNa
public function hasMethod(ClassReflection $classReflection, string $methodName): bool
{
$traitNames = $this->getTraitNames($classReflection->getNativeReflection());
if (!$classReflection->isSubclassOf('Nette\Object') && !in_array(\Nette\SmartObject::class, $traitNames, true)) {
if (!in_array(\Nette\SmartObject::class, $traitNames, true) && !$this->inheritsFromNetteObject($classReflection->getNativeReflection())) {
return false;
}

Expand Down Expand Up @@ -88,4 +88,15 @@ private function getTraitNames(\ReflectionClass $class): array
return $traitNames;
}

private function inheritsFromNetteObject(\ReflectionClass $class): bool
{
while (($parentClass = $class->getParentClass()) !== false) {
if ($parentClass->getName() === 'Nette\Object') {
return true;
}
}

return false;
}

}
3 changes: 2 additions & 1 deletion src/Rule/Nette/DoNotExtendNetteObjectRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public function processNode(Node $node, Scope $scope): array
}

$classReflection = $this->broker->getClass($className);
if ($classReflection->isSubclassOf('Nette\Object')) {
$parentClass = $classReflection->getNativeReflection()->getParentClass();
if ($parentClass !== false && $parentClass->getName() === 'Nette\Object') {
return [
sprintf(
"Class %s extends %s - it's better to use %s trait.",
Expand Down
157 changes: 157 additions & 0 deletions tests/Reflection/Nette/NetteObjectClassReflectionExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<?php declare(strict_types = 1);

namespace PHPStan\Reflection\Nette;

use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\PropertyReflection;

class NetteObjectClassReflectionExtensionTest extends \PHPUnit\Framework\TestCase
{

/** @var \PHPStan\Reflection\Nette\NetteObjectClassReflectionExtension */
private $extension;

protected function setUp()
{
$this->extension = new NetteObjectClassReflectionExtension();
}

/**
* @return mixed[]
*/
public function dataHasMethod(): array
{
$data = [];
$data[] = [
\PHPStan\Tests\SmartObjectChild::class,
'onPublicEvent',
true,
];
$data[] = [
\PHPStan\Tests\SmartObjectChild::class,
'onProtectedEvent',
false,
];
if (PHP_VERSION_ID < 70200) { // PHP 7.2 is incompatible with Nette\Object.
$data[] = [
'PHPStan\Tests\NetteObjectChild',
'onPublicEvent',
true,
];
$data[] = [
'PHPStan\Tests\NetteObjectChild',
'onProtectedEvent',
false,
];
}
return $data;
}

/**
* @dataProvider dataHasMethod
* @param string $className
* @param string $method
* @param bool $result
*/
public function testHasMethod(string $className, string $method, bool $result)
{
$classReflection = $this->mockClassReflection(new \ReflectionClass($className));
$this->assertSame($result, $this->extension->hasMethod($classReflection, $method));
}

/**
* @return mixed[]
*/
public function dataHasProperty(): array
{
$data = [];
$data[] = [
\PHPStan\Tests\SmartObjectChild::class,
'foo',
false,
];
if (PHP_VERSION_ID < 70200) { // PHP 7.2 is incompatible with Nette\Object.
$data[] = [
'PHPStan\Tests\NetteObjectChild',
'staticProperty',
false,
];
$data[] = [
'PHPStan\Tests\NetteObjectChild',
'publicProperty',
true,
];
$data[] = [
'PHPStan\Tests\NetteObjectChild',
'protectedProperty',
false,
];
}
return $data;
}

/**
* @dataProvider dataHasProperty
* @param string $className
* @param string $property
* @param bool $result
*/
public function testHasProperty(string $className, string $property, bool $result)
{
$classReflection = $this->mockClassReflection(new \ReflectionClass($className));
$this->assertSame($result, $this->extension->hasProperty($classReflection, $property));
}

private function mockClassReflection(\ReflectionClass $class): ClassReflection
{
$classReflection = $this->createMock(ClassReflection::class);
$classReflection->method('getNativeReflection')->will($this->returnValue($class));
$classReflection->method('hasProperty')->will(
$this->returnCallback(
function (string $property) use ($class): bool {
return $class->hasProperty($property);
}
)
);
$classReflection->method('getProperty')->will(
$this->returnCallback(
function (string $property) use ($class): PropertyReflection {
return $this->mockPropertyReflection($class->getProperty($property));
}
)
);
$classReflection->method('hasMethod')->will(
$this->returnCallback(
function (string $method) use ($class): bool {
return $class->hasMethod($method);
}
)
);
$classReflection->method('getMethod')->will(
$this->returnCallback(
function (string $method) use ($class): MethodReflection {
return $this->mockMethodReflection($class->getMethod($method));
}
)
);

return $classReflection;
}

private function mockMethodReflection(\ReflectionMethod $method): MethodReflection
{
$methodReflection = $this->createMock(MethodReflection::class);
$methodReflection->method('isPublic')->will($this->returnValue($method->isPublic()));
$methodReflection->method('isStatic')->will($this->returnValue($method->isStatic()));
return $methodReflection;
}

private function mockPropertyReflection(\ReflectionProperty $property): PropertyReflection
{
$propertyReflection = $this->createMock(PropertyReflection::class);
$propertyReflection->method('isPublic')->will($this->returnValue($property->isPublic()));
return $propertyReflection;
}

}
55 changes: 55 additions & 0 deletions tests/Rule/Nette/DoNotExtendNetteObjectRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rule\Nette;

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Broker\Broker;
use PHPStan\Reflection\ClassReflection;

class DoNotExtendNetteObjectRuleTest extends \PHPUnit\Framework\TestCase
{

/** @var \PHPStan\Rule\Nette\DoNotExtendNetteObjectRule */
private $rule;

protected function setUp()
{
$broker = $this->createMock(Broker::class);
$broker->method('hasClass')->will($this->returnValue(true));
$broker->method('getClass')->will($this->returnCallback(function (string $className) {
$nativeReflection = new \ReflectionClass($className);
$classReflection = $this->createMock(ClassReflection::class);
$classReflection->method('getNativeReflection')->will($this->returnValue($nativeReflection));
return $classReflection;
}));

$this->rule = new DoNotExtendNetteObjectRule($broker);
}

public function testSmartObjectChild()
{
$scope = $this->createMock(Scope::class);
$node = $this->createMock(Node::class);
$node->namespacedName = 'PHPStan\Tests\SmartObjectChild';

$result = $this->rule->processNode($node, $scope);

$this->assertEmpty($result);
}

public function testNetteObjectChild()
{
if (PHP_VERSION_ID >= 70200) {
$this->markTestSkipped('PHP 7.2 is incompatible with Nette\Object.');
}
$scope = $this->createMock(Scope::class);
$node = $this->createMock(Node::class);
$node->namespacedName = 'PHPStan\Tests\NetteObjectChild';

$result = $this->rule->processNode($node, $scope);

$this->assertSame(['Class PHPStan\Tests\NetteObjectChild extends Nette\Object - it\'s better to use Nette\SmartObject trait.'], $result);
}

}
28 changes: 28 additions & 0 deletions tests/notAutoloaded/NetteObjectChild.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php declare(strict_types = 1);
namespace PHPStan\Tests;

class NetteObjectChild extends \Nette\Object
{

/** @var callable[] */
public $onPublicEvent = [];

/** @var callable[] */
protected $onProtectedEvent = [];

public static function getStaticProperty(): string
{
return 'static';
}

public function getPublicProperty(): string
{
return 'public';
}

protected function getProtectedProperty(): string
{
return 'protected';
}

}
15 changes: 15 additions & 0 deletions tests/notAutoloaded/SmartObjectChild.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php declare(strict_types = 1);
namespace PHPStan\Tests;

class SmartObjectChild
{

use \Nette\SmartObject;

/** @var callable[] */
public $onPublicEvent = [];

/** @var callable[] */
protected $onProtectedEvent = [];

}