Skip to content

Commit

Permalink
Merge pull request #89 from ray-di/nullable-param
Browse files Browse the repository at this point in the history
#88 set nullable param for php 7.1+
  • Loading branch information
koriym authored Jun 16, 2018
2 parents b00ad97 + 163af20 commit 1b25062
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 13 deletions.
12 changes: 6 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ env:
before_script:
- composer self-update
- composer update $DEPENDENCIES
- if [ "$TRAVIS_PHP_VERSION" = "7.1" ]; then wget http://cs.sensiolabs.org/download/php-cs-fixer-v2.phar; fi
- if [ "$TRAVIS_PHP_VERSION" = "7.1" ]; then composer require phpstan/phpstan-shim; fi
- if [[ $TRAVIS_PHP_VERSION = '7.1' ]]; then COVERAGE="--coverage-clover=coverage.clover"; else phpenv config-rm xdebug.ini; fi
- if [[ $TRAVIS_PHP_VERSION = '7.1' ]]; then composer require --dev phpstan/phpstan-shim friendsofphp/php-cs-fixer; fi
script:
- ./vendor/bin/phpunit --coverage-clover=coverage.clover;
- if [ "$TRAVIS_PHP_VERSION" = "7.1" ]; then CHANGED_FILES=$(git diff --name-only --diff-filter=ACMRTUXB "${TRAVIS_COMMIT_RANGE}") & if ! echo "${CHANGED_FILES}" | grep -qE "^(\\.php_cs(\\.dist)?|composer\\.lock)$"; then IFS=$'\n' EXTRA_ARGS=('--path-mode=intersection' '--' ${CHANGED_FILES[@]}); fi & php php-cs-fixer-v2.phar fix --config=.php_cs -v --dry-run --stop-on-violation --using-cache=no "${EXTRA_ARGS[@]}"; fi
- if [ "$TRAVIS_PHP_VERSION" = "7.1" ]; then ./vendor/bin/phpstan analyse -l max -c phpstan.neon src --no-progress --no-interaction; fi
- ./vendor/bin/phpunit $COVERAGE;
- if [[ $TRAVIS_PHP_VERSION = '7.1' ]]; then ./vendor/bin/php-cs-fixer --dry-run -v fix; fi
- if [[ $TRAVIS_PHP_VERSION = '7.1' ]]; then ./vendor/bin/phpstan analyse -l max -c phpstan.neon src tests --no-progress --no-interaction; fi
after_script:
- if [ "$TRAVIS_PHP_VERSION" = "7.1" ]; then wget https://scrutinizer-ci.com/ocular.phar && php ocular.phar code-coverage:upload --format=php-clover coverage.clover; fi
- if [[ $TRAVIS_PHP_VERSION = '7.1' ]]; then wget https://scrutinizer-ci.com/ocular.phar && php ocular.phar code-coverage:upload --format=php-clover coverage.clover; fi
12 changes: 10 additions & 2 deletions src/CodeGenMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,17 @@ private function setDefault(\ReflectionParameter $param, Param $paramStmt)
private function setParameterType(\ReflectionParameter $param, Param $paramStmt)
{
$type = $param->getType();
if ($type) {
$paramStmt->setTypeHint((string) $type);
if (! $type) {
return;
}
$paramString = (string) $param;
$isNullableType = is_int(strpos($paramString, '<required>')) && strpos($paramString, 'or NULL');
if ($isNullableType) {
$paramStmt->setTypeHint(new NullableType((string) $type));

return;
}
$paramStmt->setTypeHint((string) $type);
}

private function setReturnType(\ReflectionType $returnType, Method $methodStmt)
Expand Down
13 changes: 11 additions & 2 deletions tests/CodeGenPhp71.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function testReturnTypeVoid()
{
$bind = new Bind;
$bind->bindInterceptors('returnTypeVoid', []);
$code = $this->codeGen->generate('a', new \ReflectionClass(FakePhp71ReturnTypeClass::class), $bind);
$code = $this->codeGen->generate('a', new \ReflectionClass(FakePhp71NullableClass::class), $bind);
$expected = 'function returnTypeVoid() : void';
$this->assertContains($expected, $code);
}
Expand All @@ -37,7 +37,7 @@ public function testReturnTypeNullable()
{
$bind = new Bind;
$bind->bindInterceptors('returnNullable', []);
$code = $this->codeGen->generate('a', new \ReflectionClass(FakePhp71ReturnTypeClass::class), $bind);
$code = $this->codeGen->generate('a', new \ReflectionClass(FakePhp71NullableClass::class), $bind);
$expected = 'function returnNullable(string $str) : ?';
$this->assertContains($expected, $code);

Expand All @@ -52,4 +52,13 @@ public function testContainsStatement(string $code)
$this->assertContains("declare (strict_types=1);\n", $code);
$this->assertContains("use Composer\Autoload;\n", $code);
}

public function testNullableParam()
{
$bind = new Bind;
$bind->bindInterceptors('nullableParam', []);
$code = $this->codeGen->generate('a', new \ReflectionClass(FakePhp71NullableClass::class), $bind);
$expected = 'function nullableParam(?int $id, string $name = null)';
$this->assertContains($expected, $code);
}
}
4 changes: 2 additions & 2 deletions tests/CompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,14 @@ public function testAnnotation()
{
$class = $this->compiler->compile(FakeAnnotateClass::class, $this->bind);
$annotations = (new AnnotationReader)->getMethodAnnotations(new \ReflectionMethod($class, 'getDouble'));
$this->assertSame(4, count($annotations));
$this->assertCount(4, $annotations);
}

public function testNoNamespace()
{
$class = $this->compiler->compile(FakeAnnotateClassNoName::class, $this->bind);
$annotations = (new AnnotationReader)->getMethodAnnotations(new \ReflectionMethod($class, 'getDouble'));
$this->assertSame(3, count($annotations));
$this->assertCount(3, $annotations);
}

public function testArrayTypehintedAndCallable()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Composer\Autoload;
use Reflection;

class FakePhp71ReturnTypeClass
class FakePhp71NullableClass
{
public function returnTypeVoid() : void
{
Expand All @@ -16,4 +16,8 @@ public function returnNullable(string $str) : ?int
{
return null;
}

public function nullableParam(?int $id, string $name = null) : int
{
}
}

0 comments on commit 1b25062

Please sign in to comment.