Skip to content

Commit

Permalink
added static method reflection class
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnstonCode committed Aug 3, 2018
1 parent 5713a3c commit 9f45510
Show file tree
Hide file tree
Showing 10 changed files with 206 additions and 73 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* [PHPStan](https://github.com/phpstan/phpstan)
* [Money](https://github.com/moneyphp/money)

# THIS IS NOT FINISHED!!!

## Usage

Expand Down
18 changes: 11 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{
"name": "johnstoncode/phpstan-moneyphp",
"description": "moneyphp Money class reflection extension for PHPStan",
"description": "Moneyphp Money class reflection extension for PHPStan",
"license": ["MIT"],
"minimum-stability": "dev",
"prefer-stable": true,
"authors": [
{
"name": "Chris Johnston",
Expand All @@ -13,16 +15,18 @@
"phpstan/phpstan": "^0.10",
"moneyphp/money": "^1.3"
},
"autoload": {
"psr-4": {
"JohnstonCode\\": "src/",
"JohnstonCode\\Tests\\": "tests/"
}
},
"require-dev": {
"phpunit/phpunit": "^7.2",
"phpstan/phpstan-php-parser": "^0.10.0",
"phpstan/phpstan-strict-rules": "^0.10.1",
"phpstan/phpstan-phpunit": "^0.10.0"
},
"autoload": {
"psr-4": {
"JohnstonCode\\": "src/"
}
},
"autoload-dev": {
"classmap": ["tests/"]
}
}
11 changes: 0 additions & 11 deletions src/Reflection/Money/MoneyMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,11 @@ public function getName(): string
return $this->name;
}


public function getParameters(): array
{
return [];
}

public function isVariadic(): bool
{
return false;
}

public function getReturnType()
{
return $this->return;
}

/**
* @return \PHPStan\Reflection\ParametersAcceptor[]
*/
Expand Down
19 changes: 4 additions & 15 deletions src/Reflection/Money/MoneyMethodsReflectionExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

namespace JohnstonCode\Reflection\Money;

use Money\{Money, Currency};
use Money\Currency;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\MethodsClassReflectionExtension;
use PHPStan\Type\ObjectType;

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

try {
$method = $classReflection->getNativeReflection()->getMethod($methodName);

if (!$method->isStatic()) {
return true;
}
} catch (\Exception $e) {

}

$currencies = Currency::getCurrencies();

return array_key_exists($methodName, $currencies);
return ($classReflection->getNativeReflection()->hasMethod($methodName) || array_key_exists($methodName, $currencies));
}

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

if (array_key_exists($methodName, $currencies)) {
$returnType = new ObjectType('Money\Money');
return new MoneyMethodReflection($classReflection, $methodName, true, false, $returnType);
return new MoneyStaticMethodReflection($classReflection, $methodName);
}

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

return new MoneyMethodReflection($classReflection, $methodName, $method->isStatic(), $method->isPrivate());
}

}
72 changes: 72 additions & 0 deletions src/Reflection/Money/MoneyStaticMethodReflection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php declare(strict_types=1);

namespace JohnstonCode\Reflection\Money;

use PHPStan\Reflection\ClassMemberReflection;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\FunctionVariant;
use PHPStan\Type\Type;
use PHPStan\Type\ObjectType;
use PHPStan\Type\IntegerType;

class MoneyStaticMethodReflection implements MethodReflection
{
private $classReflection;
private $name;

public function __construct(ClassReflection $classReflection, string $name)
{
$this->classReflection = $classReflection;
$this->name = $name;
}

public function getDeclaringClass(): ClassReflection
{
return $this->classReflection;
}

public function getPrototype(): ClassMemberReflection
{
return $this;
}

public function isStatic(): bool
{
return true;
}

public function isPrivate(): bool
{
return false;
}

public function isPublic(): bool
{
return true;
}

public function getName(): string
{
return $this->name;
}

public function isVariadic(): bool
{
return false;
}

/**
* @return \PHPStan\Reflection\ParametersAcceptor[]
*/
public function getVariants(): array
{
return [
new FunctionVariant(
[new MoneyStaticParameterReflection()],
false,
new ObjectType('Money\Money')
),
];
}
}
36 changes: 36 additions & 0 deletions src/Reflection/Money/MoneyStaticParameterReflection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php declare(strict_types=1);

namespace JohnstonCode\Reflection\Money;

use PHPStan\Reflection\PassedByReference;
use PHPStan\Reflection\ParameterReflection;
use PHPStan\Type\IntegerType;
use PHPStan\Type\Type;

class MoneyStaticParameterReflection implements ParameterReflection
{
public function getName(): string
{
return 'amount';
}

public function isOptional(): bool
{
return false;
}

public function getType(): Type
{
return new IntegerType();
}

public function passedByReference(): PassedByReference
{
return PassedByReference::createNo();
}

public function isVariadic(): bool
{
return false;
}
}
Empty file removed src/Reflection/Moneytest.txt
Empty file.
Empty file removed src/Reflection/Moneytext.txt
Empty file.
39 changes: 0 additions & 39 deletions tests/Reflection/Money/MoneyAwareClassReflectionExtensionTest.php

This file was deleted.

83 changes: 83 additions & 0 deletions tests/Reflection/Money/MoneyMethodsReflectionExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php declare(strict_types=1);

namespace JohnstonCode\Reflection\Money;

use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\VerbosityLevel;

class MoneyMethodsReflectionExtensionTest extends \PHPStan\Testing\TestCase
{
private $broker;
private $extension;

protected function setUp(): void
{
$this->broker = $this->createBroker();
$this->extension = new MoneyMethodsReflectionExtension();
}

public function testHasMethod(): void
{
$classReflection = $this->broker->getClass('Money\Money');
$this->assertTrue($this->extension->hasMethod($classReflection, 'getUnits'));
}

public function testHasStaticMethod(): void
{
$classReflection = $this->broker->getClass('Money\Money');
$this->assertTrue($this->extension->hasMethod($classReflection, 'GBP'));
}

public function testInvalidMethod(): void
{
$classReflection = $this->broker->getClass('Money\Money');
$this->assertFalse($this->extension->hasMethod($classReflection, 'ZZZ'));
}

public function testHasPrivateMethod(): void
{
$classReflection = $this->broker->getClass('Money\Money');
$this->assertTrue($this->extension->hasMethod($classReflection, 'assertSameCurrency'));
}

public function testGetPublicMethod(): void
{
$classReflection = $this->broker->getClass('Money\Money');
$methodReflection = $this->extension->getMethod($classReflection, 'getUnits');

$this->assertEquals('getUnits', $methodReflection->getName());
$this->assertEquals($classReflection, $methodReflection->getDeclaringClass());
$this->assertFalse($methodReflection->isStatic());
$this->assertFalse($methodReflection->isVariadic());
$this->assertFalse($methodReflection->isPrivate());
$this->assertTrue($methodReflection->isPublic());
}

public function testGetPrivateMethod(): void
{
$classReflection = $this->broker->getClass('Money\Money');
$methodReflection = $this->extension->getMethod($classReflection, 'assertSameCurrency');

$this->assertEquals('assertSameCurrency', $methodReflection->getName());
$this->assertEquals($classReflection, $methodReflection->getDeclaringClass());
$this->assertFalse($methodReflection->isStatic());
$this->assertFalse($methodReflection->isVariadic());
$this->assertTrue($methodReflection->isPrivate());
$this->assertFalse($methodReflection->isPublic());
}

public function testGetStaticMethod(): void
{
$classReflection = $this->broker->getClass('Money\Money');
$methodReflection = $this->extension->getMethod($classReflection, 'GBP');
$parametersAcceptor = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants());

$this->assertEquals('GBP', $methodReflection->getName());
$this->assertEquals($classReflection, $methodReflection->getDeclaringClass());
$this->assertTrue($methodReflection->isStatic());
$this->assertFalse($methodReflection->isVariadic());
$this->assertFalse($methodReflection->isPrivate());
$this->assertTrue($methodReflection->isPublic());
$this->assertEquals(\Money\Money::class, $parametersAcceptor->getReturnType()->describe(VerbosityLevel::value()));
}
}

0 comments on commit 9f45510

Please sign in to comment.