Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
crissi authored and Christian Nielsen committed Jul 4, 2022
1 parent ab0cca9 commit f7a1cc6
Show file tree
Hide file tree
Showing 5 changed files with 57 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
39 changes: 39 additions & 0 deletions src/Types/AppEnvironmentReturnTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace NunoMaduro\Larastan\Types;

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 @@ -20,6 +20,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__.'/data/model-properties-relations.php');
yield from $this->gatherAssertTypes(__DIR__.'/data/route.php');
yield from $this->gatherAssertTypes(__DIR__.'/data/conditionable.php');
yield from $this->gatherAssertTypes(__DIR__.'/data/blank-helper.php');
}

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

namespace BlankHelper;

use App\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Traits\Conditionable;
use function PHPStan\Testing\assertType;

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

0 comments on commit f7a1cc6

Please sign in to comment.