Skip to content

Commit 9f45510

Browse files
committed
added static method reflection class
1 parent 5713a3c commit 9f45510

10 files changed

+206
-73
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
* [PHPStan](https://github.com/phpstan/phpstan)
44
* [Money](https://github.com/moneyphp/money)
55

6-
# THIS IS NOT FINISHED!!!
76

87
## Usage
98

composer.json

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
{
22
"name": "johnstoncode/phpstan-moneyphp",
3-
"description": "moneyphp Money class reflection extension for PHPStan",
3+
"description": "Moneyphp Money class reflection extension for PHPStan",
4+
"license": ["MIT"],
45
"minimum-stability": "dev",
6+
"prefer-stable": true,
57
"authors": [
68
{
79
"name": "Chris Johnston",
@@ -13,16 +15,18 @@
1315
"phpstan/phpstan": "^0.10",
1416
"moneyphp/money": "^1.3"
1517
},
16-
"autoload": {
17-
"psr-4": {
18-
"JohnstonCode\\": "src/",
19-
"JohnstonCode\\Tests\\": "tests/"
20-
}
21-
},
2218
"require-dev": {
2319
"phpunit/phpunit": "^7.2",
2420
"phpstan/phpstan-php-parser": "^0.10.0",
2521
"phpstan/phpstan-strict-rules": "^0.10.1",
2622
"phpstan/phpstan-phpunit": "^0.10.0"
23+
},
24+
"autoload": {
25+
"psr-4": {
26+
"JohnstonCode\\": "src/"
27+
}
28+
},
29+
"autoload-dev": {
30+
"classmap": ["tests/"]
2731
}
2832
}

src/Reflection/Money/MoneyMethodReflection.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,11 @@ public function getName(): string
5656
return $this->name;
5757
}
5858

59-
60-
public function getParameters(): array
61-
{
62-
return [];
63-
}
64-
6559
public function isVariadic(): bool
6660
{
6761
return false;
6862
}
6963

70-
public function getReturnType()
71-
{
72-
return $this->return;
73-
}
74-
7564
/**
7665
* @return \PHPStan\Reflection\ParametersAcceptor[]
7766
*/

src/Reflection/Money/MoneyMethodsReflectionExtension.php

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22

33
namespace JohnstonCode\Reflection\Money;
44

5-
use Money\{Money, Currency};
5+
use Money\Currency;
66
use PHPStan\Reflection\ClassReflection;
77
use PHPStan\Reflection\MethodReflection;
88
use PHPStan\Reflection\MethodsClassReflectionExtension;
9-
use PHPStan\Type\ObjectType;
109

1110
class MoneyMethodsReflectionExtension implements MethodsClassReflectionExtension
1211
{
@@ -16,32 +15,22 @@ public function hasMethod(ClassReflection $classReflection, string $methodName):
1615
return false;
1716
}
1817

19-
try {
20-
$method = $classReflection->getNativeReflection()->getMethod($methodName);
21-
22-
if (!$method->isStatic()) {
23-
return true;
24-
}
25-
} catch (\Exception $e) {
26-
27-
}
28-
2918
$currencies = Currency::getCurrencies();
3019

31-
return array_key_exists($methodName, $currencies);
20+
return ($classReflection->getNativeReflection()->hasMethod($methodName) || array_key_exists($methodName, $currencies));
3221
}
3322

3423
public function getMethod(ClassReflection $classReflection, string $methodName): MethodReflection
3524
{
3625
$currencies = Currency::getCurrencies();
3726

3827
if (array_key_exists($methodName, $currencies)) {
39-
$returnType = new ObjectType('Money\Money');
40-
return new MoneyMethodReflection($classReflection, $methodName, true, false, $returnType);
28+
return new MoneyStaticMethodReflection($classReflection, $methodName);
4129
}
4230

4331
$method = $classReflection->getNativeReflection()->getMethod($methodName);
4432

4533
return new MoneyMethodReflection($classReflection, $methodName, $method->isStatic(), $method->isPrivate());
4634
}
35+
4736
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace JohnstonCode\Reflection\Money;
4+
5+
use PHPStan\Reflection\ClassMemberReflection;
6+
use PHPStan\Reflection\ClassReflection;
7+
use PHPStan\Reflection\MethodReflection;
8+
use PHPStan\Reflection\FunctionVariant;
9+
use PHPStan\Type\Type;
10+
use PHPStan\Type\ObjectType;
11+
use PHPStan\Type\IntegerType;
12+
13+
class MoneyStaticMethodReflection implements MethodReflection
14+
{
15+
private $classReflection;
16+
private $name;
17+
18+
public function __construct(ClassReflection $classReflection, string $name)
19+
{
20+
$this->classReflection = $classReflection;
21+
$this->name = $name;
22+
}
23+
24+
public function getDeclaringClass(): ClassReflection
25+
{
26+
return $this->classReflection;
27+
}
28+
29+
public function getPrototype(): ClassMemberReflection
30+
{
31+
return $this;
32+
}
33+
34+
public function isStatic(): bool
35+
{
36+
return true;
37+
}
38+
39+
public function isPrivate(): bool
40+
{
41+
return false;
42+
}
43+
44+
public function isPublic(): bool
45+
{
46+
return true;
47+
}
48+
49+
public function getName(): string
50+
{
51+
return $this->name;
52+
}
53+
54+
public function isVariadic(): bool
55+
{
56+
return false;
57+
}
58+
59+
/**
60+
* @return \PHPStan\Reflection\ParametersAcceptor[]
61+
*/
62+
public function getVariants(): array
63+
{
64+
return [
65+
new FunctionVariant(
66+
[new MoneyStaticParameterReflection()],
67+
false,
68+
new ObjectType('Money\Money')
69+
),
70+
];
71+
}
72+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace JohnstonCode\Reflection\Money;
4+
5+
use PHPStan\Reflection\PassedByReference;
6+
use PHPStan\Reflection\ParameterReflection;
7+
use PHPStan\Type\IntegerType;
8+
use PHPStan\Type\Type;
9+
10+
class MoneyStaticParameterReflection implements ParameterReflection
11+
{
12+
public function getName(): string
13+
{
14+
return 'amount';
15+
}
16+
17+
public function isOptional(): bool
18+
{
19+
return false;
20+
}
21+
22+
public function getType(): Type
23+
{
24+
return new IntegerType();
25+
}
26+
27+
public function passedByReference(): PassedByReference
28+
{
29+
return PassedByReference::createNo();
30+
}
31+
32+
public function isVariadic(): bool
33+
{
34+
return false;
35+
}
36+
}

src/Reflection/Moneytest.txt

Whitespace-only changes.

src/Reflection/Moneytext.txt

Whitespace-only changes.

tests/Reflection/Money/MoneyAwareClassReflectionExtensionTest.php

Lines changed: 0 additions & 39 deletions
This file was deleted.
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace JohnstonCode\Reflection\Money;
4+
5+
use PHPStan\Reflection\ParametersAcceptorSelector;
6+
use PHPStan\Type\VerbosityLevel;
7+
8+
class MoneyMethodsReflectionExtensionTest extends \PHPStan\Testing\TestCase
9+
{
10+
private $broker;
11+
private $extension;
12+
13+
protected function setUp(): void
14+
{
15+
$this->broker = $this->createBroker();
16+
$this->extension = new MoneyMethodsReflectionExtension();
17+
}
18+
19+
public function testHasMethod(): void
20+
{
21+
$classReflection = $this->broker->getClass('Money\Money');
22+
$this->assertTrue($this->extension->hasMethod($classReflection, 'getUnits'));
23+
}
24+
25+
public function testHasStaticMethod(): void
26+
{
27+
$classReflection = $this->broker->getClass('Money\Money');
28+
$this->assertTrue($this->extension->hasMethod($classReflection, 'GBP'));
29+
}
30+
31+
public function testInvalidMethod(): void
32+
{
33+
$classReflection = $this->broker->getClass('Money\Money');
34+
$this->assertFalse($this->extension->hasMethod($classReflection, 'ZZZ'));
35+
}
36+
37+
public function testHasPrivateMethod(): void
38+
{
39+
$classReflection = $this->broker->getClass('Money\Money');
40+
$this->assertTrue($this->extension->hasMethod($classReflection, 'assertSameCurrency'));
41+
}
42+
43+
public function testGetPublicMethod(): void
44+
{
45+
$classReflection = $this->broker->getClass('Money\Money');
46+
$methodReflection = $this->extension->getMethod($classReflection, 'getUnits');
47+
48+
$this->assertEquals('getUnits', $methodReflection->getName());
49+
$this->assertEquals($classReflection, $methodReflection->getDeclaringClass());
50+
$this->assertFalse($methodReflection->isStatic());
51+
$this->assertFalse($methodReflection->isVariadic());
52+
$this->assertFalse($methodReflection->isPrivate());
53+
$this->assertTrue($methodReflection->isPublic());
54+
}
55+
56+
public function testGetPrivateMethod(): void
57+
{
58+
$classReflection = $this->broker->getClass('Money\Money');
59+
$methodReflection = $this->extension->getMethod($classReflection, 'assertSameCurrency');
60+
61+
$this->assertEquals('assertSameCurrency', $methodReflection->getName());
62+
$this->assertEquals($classReflection, $methodReflection->getDeclaringClass());
63+
$this->assertFalse($methodReflection->isStatic());
64+
$this->assertFalse($methodReflection->isVariadic());
65+
$this->assertTrue($methodReflection->isPrivate());
66+
$this->assertFalse($methodReflection->isPublic());
67+
}
68+
69+
public function testGetStaticMethod(): void
70+
{
71+
$classReflection = $this->broker->getClass('Money\Money');
72+
$methodReflection = $this->extension->getMethod($classReflection, 'GBP');
73+
$parametersAcceptor = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants());
74+
75+
$this->assertEquals('GBP', $methodReflection->getName());
76+
$this->assertEquals($classReflection, $methodReflection->getDeclaringClass());
77+
$this->assertTrue($methodReflection->isStatic());
78+
$this->assertFalse($methodReflection->isVariadic());
79+
$this->assertFalse($methodReflection->isPrivate());
80+
$this->assertTrue($methodReflection->isPublic());
81+
$this->assertEquals(\Money\Money::class, $parametersAcceptor->getReturnType()->describe(VerbosityLevel::value()));
82+
}
83+
}

0 commit comments

Comments
 (0)