Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
crissi committed Jul 1, 2022
1 parent ab0cca9 commit dbfeba7
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
* fix: Resolve correct model factory instance when application namespace is empty

* fix: fix return types for App::environment() [#1303](https://github.com/nunomaduro/larastan/pull/1303)
### Fixed

* fix: Remove stubs for `having()` and `orHaving()` QueryBuilder methods by @Maxoulak in [#1258](https://github.com/nunomaduro/larastan/pull/1258)
Expand Down
4 changes: 4 additions & 0 deletions extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,10 @@ services:
class: NunoMaduro\Larastan\Types\GenericEloquentBuilderTypeNodeResolverExtension
tags:
- phpstan.phpDoc.typeNodeResolverExtension
-
class: NunoMaduro\Larastan\Types\AppEnvironmentReturnTypeExtension
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension

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

declare(strict_types=1);

namespace NunoMaduro\Larastan\Types;

use PHPStan\Type\Type;
use PHPStan\Analyser\Scope;
use PHPStan\Type\ObjectType;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\ShouldNotHappenException;
use Illuminate\Foundation\Application;
use Illuminate\Database\Eloquent\Model;
use PHPStan\Reflection\FunctionVariant;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\StringType;
use PHPStan\Type\BooleanType;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\DynamicMethodReturnTypeExtension;

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();
}
}
19 changes: 19 additions & 0 deletions tests/Features/ReturnTypes/AppAccessTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Tests\Features\ReturnTypes;

use Illuminate\Auth\AuthManager;
use Illuminate\Container\Container;
use function PHPStan\Testing\assertType;

class AppAccessTest
{
public function testEnvironment(): void
{
assertType('string', app()->environment());
assertType('bool', app()->environment('local'));
assertType('bool', app()->environment(['local', 'production']));
}
}

0 comments on commit dbfeba7

Please sign in to comment.