Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: better return types for App::environment #1303

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Improvements
* feat: add Rule checking for useless 'with()' and 'value()' function calls by @PrinsFrank in https://github.com/nunomaduro/larastan/pull/1226
* feat: Add rule for missing 'provides' method in Deferrable ServiceProviders
* feat: Support for `__()` helper and simplified existing `trans()` helper by @niekbr in [#1296](https://github.com/nunomaduro/larastan/pull/1296).

### Fixed
* fix: Resolve correct model factory instance when application namespace is empty
* fix: Remove stubs for `having()` and `orHaving()` QueryBuilder methods by @Maxoulak in [#1258](https://github.com/nunomaduro/larastan/pull/1258)
* added: Support for `__()` helper and simplified existing `trans()` helper by @niekbr in [#1296](https://github.com/nunomaduro/larastan/pull/1296).
* fix: fix return types for App::environment() [#1303](https://github.com/nunomaduro/larastan/pull/1303)

## [2.1.8] - 2022-06-04

Expand Down
4 changes: 4 additions & 0 deletions extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,10 @@ services:
class: NunoMaduro\Larastan\Types\GenericEloquentBuilderTypeNodeResolverExtension
tags:
- phpstan.phpDoc.typeNodeResolverExtension
-
class: NunoMaduro\Larastan\ReturnTypes\AppEnvironmentReturnTypeExtension
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension

-
class: NunoMaduro\Larastan\Types\ModelProperty\ModelPropertyTypeNodeResolverExtension
Expand Down
39 changes: 39 additions & 0 deletions src/ReturnTypes/AppEnvironmentReturnTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace NunoMaduro\Larastan\ReturnTypes;

use Illuminate\Foundation\Application;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\BooleanType;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;

class AppEnvironmentReturnTypeExtension implements DynamicMethodReturnTypeExtension
{
public function getClass(): string
{
return Application::class;
}

public function isMethodSupported(MethodReflection $methodReflection): bool
{
return $methodReflection->getName() === 'environment';
}

public function getTypeFromMethodCall(
MethodReflection $methodReflection,
MethodCall $methodCall,
Scope $scope
): Type {
if (count($methodCall->getArgs()) === 0) {
return new StringType();
}

return new BooleanType();
}
}
1 change: 1 addition & 0 deletions tests/Type/GeneralTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__.'/data/conditionable.php');
yield from $this->gatherAssertTypes(__DIR__.'/data/translate.php');
yield from $this->gatherAssertTypes(__DIR__.'/data/model-factories.php');
yield from $this->gatherAssertTypes(__DIR__.'/data/environment-helper.php');
}

/**
Expand Down
9 changes: 9 additions & 0 deletions tests/Type/data/environment-helper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace EnvironmentHelper;

use function PHPStan\Testing\assertType;

assertType('string', app()->environment());
assertType('bool', app()->environment('local'));
assertType('bool', app()->environment(['local', 'production']));