Skip to content

Commit 8f33139

Browse files
committed
First working version
1 parent d478b94 commit 8f33139

File tree

5 files changed

+158
-1
lines changed

5 files changed

+158
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/composer.lock
2+
/vendor

composer.json

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
{
22
"name": "phpstan/phpstan-nette",
33
"description": "Nette Framework class reflection extension for PHPStan",
4-
"license": ["MIT"]
4+
"license": ["MIT"],
5+
"minimum-stability": "dev",
6+
"prefer-stable": true,
7+
"require": {
8+
"php": "~7.0",
9+
"phpstan/phpstan": "dev-dev#1cb3904e17",
10+
"nette/utils": "~2.3.0"
11+
},
12+
"autoload": {
13+
"psr-4": {
14+
"PHPStan\\": "src/"
15+
}
16+
}
517
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace PHPStan\Reflection\NetteObject;
4+
5+
use Nette\Object;
6+
use PHPStan\Reflection\ClassReflection;
7+
use PHPStan\Reflection\ClassReflectionExtension;
8+
use PHPStan\Reflection\MethodReflection;
9+
use PHPStan\Reflection\PropertyReflection;
10+
11+
class NetteObjectClassReflectionExtension implements ClassReflectionExtension
12+
{
13+
14+
public function hasProperty(ClassReflection $classReflection, string $propertyName): bool
15+
{
16+
if (!$classReflection->isSubclassOf(Object::class)) {
17+
return false;
18+
}
19+
20+
if (substr($propertyName, 0, 2) === 'on' && strlen($propertyName) > 2) {
21+
return false; // prevent infinite loop from hasMethod
22+
}
23+
24+
// todo setter taky?
25+
26+
$getterMethodName = sprintf('get%s', ucfirst($propertyName));
27+
28+
return $classReflection->hasMethod($getterMethodName) && $classReflection->getMethod($getterMethodName)->isPublic();
29+
}
30+
31+
public function getProperty(ClassReflection $classReflection, string $propertyName): PropertyReflection
32+
{
33+
return new NetteObjectPropertyReflection($classReflection);
34+
}
35+
36+
public function hasMethod(ClassReflection $classReflection, string $methodName): bool
37+
{
38+
if (!$classReflection->isSubclassOf(Object::class)) {
39+
return false;
40+
}
41+
42+
if (substr($methodName, 0, 2) !== 'on' || strlen($methodName) <= 2) {
43+
return false;
44+
}
45+
46+
return $classReflection->hasProperty($methodName) && $classReflection->getProperty($methodName)->isPublic();
47+
}
48+
49+
public function getMethod(ClassReflection $classReflection, string $methodName): MethodReflection
50+
{
51+
return new NetteObjectEventListenerMethodReflection($classReflection);
52+
}
53+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace PHPStan\Reflection\NetteObject;
4+
5+
use PHPStan\Reflection\ClassReflection;
6+
use PHPStan\Reflection\MethodReflection;
7+
8+
class NetteObjectEventListenerMethodReflection implements MethodReflection
9+
{
10+
11+
/** @var \PHPStan\Reflection\ClassReflection */
12+
private $declaringClass;
13+
14+
public function __construct(ClassReflection $declaringClass)
15+
{
16+
$this->declaringClass = $declaringClass;
17+
}
18+
19+
public function getDeclaringClass(): ClassReflection
20+
{
21+
return $this->declaringClass;
22+
}
23+
24+
public function isStatic(): bool
25+
{
26+
return false;
27+
}
28+
29+
/**
30+
* @return \PHPStan\Reflection\ParameterReflection[]
31+
*/
32+
public function getParameters(): array
33+
{
34+
return [];
35+
}
36+
37+
public function isVariadic(): bool
38+
{
39+
return true;
40+
}
41+
42+
public function isPrivate(): bool
43+
{
44+
return false;
45+
}
46+
47+
public function isPublic(): bool
48+
{
49+
return true;
50+
}
51+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace PHPStan\Reflection\NetteObject;
4+
5+
use PHPStan\Reflection\ClassReflection;
6+
use PHPStan\Reflection\PropertyReflection;
7+
8+
class NetteObjectPropertyReflection implements PropertyReflection
9+
{
10+
11+
/** @var \PHPStan\Reflection\ClassReflection */
12+
private $declaringClass;
13+
14+
public function __construct(ClassReflection $declaringClass)
15+
{
16+
$this->declaringClass = $declaringClass;
17+
}
18+
19+
public function getDeclaringClass(): ClassReflection
20+
{
21+
return $this->declaringClass;
22+
}
23+
24+
public function isStatic(): bool
25+
{
26+
return false;
27+
}
28+
29+
public function isPrivate(): bool
30+
{
31+
return false;
32+
}
33+
34+
public function isPublic(): bool
35+
{
36+
return true;
37+
}
38+
39+
}

0 commit comments

Comments
 (0)