Skip to content

Commit 10106fd

Browse files
committed
Properties and methods has types
1 parent 8f33139 commit 10106fd

File tree

4 files changed

+56
-9
lines changed

4 files changed

+56
-9
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"prefer-stable": true,
77
"require": {
88
"php": "~7.0",
9-
"phpstan/phpstan": "dev-dev#1cb3904e17",
9+
"phpstan/phpstan": "dev-dev#3ba62cbb49",
1010
"nette/utils": "~2.3.0"
1111
},
1212
"autoload": {

src/Reflection/NetteObject/NetteObjectClassReflectionExtension.php

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44

55
use Nette\Object;
66
use PHPStan\Reflection\ClassReflection;
7-
use PHPStan\Reflection\ClassReflectionExtension;
87
use PHPStan\Reflection\MethodReflection;
8+
use PHPStan\Reflection\MethodsClassReflectionExtension;
9+
use PHPStan\Reflection\PropertiesClassReflectionExtension;
910
use PHPStan\Reflection\PropertyReflection;
1011

11-
class NetteObjectClassReflectionExtension implements ClassReflectionExtension
12+
class NetteObjectClassReflectionExtension implements MethodsClassReflectionExtension, PropertiesClassReflectionExtension
1213
{
1314

1415
public function hasProperty(ClassReflection $classReflection, string $propertyName): bool
@@ -23,14 +24,33 @@ public function hasProperty(ClassReflection $classReflection, string $propertyNa
2324

2425
// todo setter taky?
2526
27+
$getterMethod = $this->getMethodByProperty($classReflection, $propertyName);
28+
if ($getterMethod === null) {
29+
return false;
30+
}
31+
32+
return $getterMethod->isPublic();
33+
}
34+
35+
/**
36+
* @param \PHPStan\Reflection\ClassReflection $classReflection
37+
* @param string $propertyName
38+
* @return \PHPStan\Reflection\MethodReflection|null
39+
*/
40+
private function getMethodByProperty(ClassReflection $classReflection, string $propertyName)
41+
{
2642
$getterMethodName = sprintf('get%s', ucfirst($propertyName));
43+
if (!$classReflection->hasMethod($getterMethodName)) {
44+
return null;
45+
}
2746

28-
return $classReflection->hasMethod($getterMethodName) && $classReflection->getMethod($getterMethodName)->isPublic();
47+
return $classReflection->getMethod($getterMethodName);
2948
}
3049

3150
public function getProperty(ClassReflection $classReflection, string $propertyName): PropertyReflection
3251
{
33-
return new NetteObjectPropertyReflection($classReflection);
52+
$getterMethod = $this->getMethodByProperty($classReflection, $propertyName);
53+
return new NetteObjectPropertyReflection($classReflection, $getterMethod->getReturnType());
3454
}
3555

3656
public function hasMethod(ClassReflection $classReflection, string $methodName): bool
@@ -48,6 +68,6 @@ public function hasMethod(ClassReflection $classReflection, string $methodName):
4868

4969
public function getMethod(ClassReflection $classReflection, string $methodName): MethodReflection
5070
{
51-
return new NetteObjectEventListenerMethodReflection($classReflection);
71+
return new NetteObjectEventListenerMethodReflection($methodName, $classReflection);
5272
}
5373
}

src/Reflection/NetteObject/NetteObjectEventListenerMethodReflection.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,29 @@
44

55
use PHPStan\Reflection\ClassReflection;
66
use PHPStan\Reflection\MethodReflection;
7+
use PHPStan\Type\NullType;
8+
use PHPStan\Type\Type;
79

810
class NetteObjectEventListenerMethodReflection implements MethodReflection
911
{
1012

13+
/** @var string */
14+
private $name;
15+
1116
/** @var \PHPStan\Reflection\ClassReflection */
1217
private $declaringClass;
1318

14-
public function __construct(ClassReflection $declaringClass)
19+
public function __construct(string $name, ClassReflection $declaringClass)
1520
{
21+
$this->name = $name;
1622
$this->declaringClass = $declaringClass;
1723
}
1824

25+
public function getName(): string
26+
{
27+
return $this->name;
28+
}
29+
1930
public function getDeclaringClass(): ClassReflection
2031
{
2132
return $this->declaringClass;
@@ -48,4 +59,10 @@ public function isPublic(): bool
4859
{
4960
return true;
5061
}
51-
}
62+
63+
public function getReturnType(): Type
64+
{
65+
return new NullType();
66+
}
67+
68+
}

src/Reflection/NetteObject/NetteObjectPropertyReflection.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,21 @@
44

55
use PHPStan\Reflection\ClassReflection;
66
use PHPStan\Reflection\PropertyReflection;
7+
use PHPStan\Type\Type;
78

89
class NetteObjectPropertyReflection implements PropertyReflection
910
{
1011

1112
/** @var \PHPStan\Reflection\ClassReflection */
1213
private $declaringClass;
1314

14-
public function __construct(ClassReflection $declaringClass)
15+
/** @var \PHPStan\Type\Type */
16+
private $type;
17+
18+
public function __construct(ClassReflection $declaringClass, Type $type)
1519
{
1620
$this->declaringClass = $declaringClass;
21+
$this->type = $type;
1722
}
1823

1924
public function getDeclaringClass(): ClassReflection
@@ -36,4 +41,9 @@ public function isPublic(): bool
3641
return true;
3742
}
3843

44+
public function getType(): Type
45+
{
46+
return $this->type;
47+
}
48+
3949
}

0 commit comments

Comments
 (0)