Skip to content
Open
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
145 changes: 76 additions & 69 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,72 +1,79 @@
{
"name": "soyhuce/phpstan-extension",
"description": "Extra rules for phpstan analysis",
"keywords": [
"soyhuce",
"laravel",
"phpstan"
],
"homepage": "https://github.com/soyhuce/phpstan-extension",
"license": "MIT",
"authors": [
{
"name": "Bastien Philippe",
"email": "[email protected]",
"role": "Developer"
}
],
"require": {
"php": "^8.3",
"illuminate/support": "^11.0|^12.0",
"nesbot/carbon": "^3.0",
"phpstan/phpstan": "^2.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.7",
"larastan/larastan": "^3.0",
"nunomaduro/collision": "^8.0",
"orchestra/testbench": "^9.0|^10.0",
"pestphp/pest": "^3.0",
"pestphp/pest-plugin-laravel": "^3.0",
"phpstan/extension-installer": "^1.1",
"phpstan/phpstan-deprecation-rules": "^2.0",
"phpstan/phpstan-phpunit": "^2.0"
},
"autoload": {
"psr-4": {
"Soyhuce\\PhpstanExtension\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Soyhuce\\PhpstanExtension\\Tests\\": "tests"
}
"name": "soyhuce/phpstan-extension",
"description": "Extra rules for phpstan analysis",
"keywords": [
"soyhuce",
"laravel",
"phpstan"
],
"homepage": "https://github.com/soyhuce/phpstan-extension",
"license": "MIT",
"authors": [
{
"name": "Bastien Philippe",
"email": "[email protected]",
"role": "Developer"
}
],
"require": {
"php": "^8.3",
"illuminate/support": "^11.0|^12.0",
"nesbot/carbon": "^3.0",
"phpstan/phpstan": "^2.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.7",
"larastan/larastan": "^3.0",
"nunomaduro/collision": "^8.0",
"orchestra/testbench": "^9.0|^10.0",
"pestphp/pest": "^3.0",
"pestphp/pest-plugin-laravel": "^3.0",
"phpstan/extension-installer": "^1.1",
"phpstan/phpstan-deprecation-rules": "^2.0",
"phpstan/phpstan-phpunit": "^2.0"
},
"autoload": {
"psr-4": {
"Soyhuce\\PhpstanExtension\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Soyhuce\\PhpstanExtension\\Tests\\": "tests",
"Larastan\\Larastan\\ReturnTypes\\": "larastan"
},
"scripts": {
"cs": "vendor/bin/php-cs-fixer fix",
"analyse": "vendor/bin/phpstan analyse",
"test": "vendor/bin/pest",
"test-coverage": "vendor/bin/pest --coverage",
"all": [
"@cs",
"@test",
"@analyse"
]
},
"config": {
"sort-packages": true,
"allow-plugins": {
"pestphp/pest-plugin": true,
"phpstan/extension-installer": true
}
},
"extra": {
"phpstan": {
"includes": [
"extension.neon"
]
}
},
"minimum-stability": "dev",
"prefer-stable": true
"exclude-from-classmap": [
"Larastan\\Larastan\\ReturnTypes\\TransHelperReturnTypeExtension"
],
"files": [
"larastan/TransHelperReturnTypeExtension.php"
]
},
"scripts": {
"cs": "vendor/bin/php-cs-fixer fix",
"analyse": "vendor/bin/phpstan analyse",
"test": "vendor/bin/pest",
"test-coverage": "vendor/bin/pest --coverage",
"all": [
"@cs",
"@test",
"@analyse"
]
},
"config": {
"sort-packages": true,
"allow-plugins": {
"pestphp/pest-plugin": true,
"phpstan/extension-installer": true
}
},
"extra": {
"phpstan": {
"includes": [
"extension.neon"
]
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
78 changes: 78 additions & 0 deletions larastan/TransHelperReturnTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php declare(strict_types=1);

namespace Larastan\Larastan\ReturnTypes;

use Illuminate\Contracts\Translation\Translator;
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\ArrayType;
use PHPStan\Type\BenevolentUnionType;
use PHPStan\Type\BooleanType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\FloatType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;

class TransHelperReturnTypeExtension implements DynamicFunctionReturnTypeExtension
{
public function isFunctionSupported(FunctionReflection $functionReflection): bool
{
return $functionReflection->getName() === 'trans';
}

public function getTypeFromFunctionCall(
FunctionReflection $functionReflection,
FuncCall $functionCall,
Scope $scope,
): Type {
if (count($functionCall->args) === 0) {
return new ObjectType(Translator::class);
}

$firstArg = $functionCall->args[0]->value;
$type = $scope->getType($firstArg);

if (!$type instanceof ConstantStringType) {
return new BenevolentUnionType([
new ArrayType(new MixedType(), new MixedType()),
new StringType(),
]);
}

$key = $type->getValue();

$translatedValue = app(Translator::class)->get($key);

return $this->getTypeFromValue($translatedValue);
}

private function getTypeFromValue(mixed $value): Type
{
if (is_array($value)) {
return new ArrayType(new MixedType(), new MixedType());
}

if (is_string($value)) {
return new StringType();
}

if (is_int($value)) {
return new IntegerType();
}

if (is_float($value)) {
return new FloatType();
}

if (is_bool($value)) {
return new BooleanType();
}

return new MixedType();
}
}
15 changes: 15 additions & 0 deletions tests/Features/Default/TransHelperReturnTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php declare(strict_types=1);

namespace Soyhuce\PhpstanExtension\Tests\Features\Default;

use function PHPStan\Testing\assertType;

class TransHelperReturnTypeExtension
{
public function run(): void
{
assertType('Illuminate\Contracts\Translation\Translator', trans());
assertType('array', trans('example.array'));
assertType('string', trans('example.string'));
}
}
11 changes: 11 additions & 0 deletions tests/Features/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php declare(strict_types=1);

use Illuminate\Translation\Translator;

app(Translator::class)->addLines(
[
'example.array' => ['foo', 'bar'],
'example.string' => 'Hello, World!',
],
'en'
);
8 changes: 8 additions & 0 deletions tests/Features/phpstan-tests.neon
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,11 @@ services:
- class: Soyhuce\PhpstanExtension\ReturnTypes\RequestDateExtension
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension

- class: Larastan\Larastan\ReturnTypes\TransHelperReturnTypeExtension
tags:
- phpstan.broker.dynamicFunctionReturnTypeExtension

parameters:
bootstrapFiles:
- bootstrap.php