|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Psalm\LaravelPlugin\Handlers\Helpers; |
| 4 | + |
| 5 | +use Illuminate\Config\Repository; |
| 6 | +use Psalm\LaravelPlugin\Providers\ApplicationProvider; |
| 7 | +use Psalm\Plugin\EventHandler\Event\FunctionReturnTypeProviderEvent; |
| 8 | +use Psalm\Plugin\EventHandler\FunctionReturnTypeProviderInterface; |
| 9 | +use Psalm\Type\Atomic\TArray; |
| 10 | +use Psalm\Type\Atomic\TArrayKey; |
| 11 | +use Psalm\Type\Atomic\TBool; |
| 12 | +use Psalm\Type\Atomic\TClosedResource; |
| 13 | +use Psalm\Type\Atomic\TFloat; |
| 14 | +use Psalm\Type\Atomic\TLiteralFloat; |
| 15 | +use Psalm\Type\Atomic\TLiteralInt; |
| 16 | +use Psalm\Type\Atomic\TLiteralString; |
| 17 | +use Psalm\Type\Atomic\TMixed; |
| 18 | +use Psalm\Type\Atomic\TNamedObject; |
| 19 | +use Psalm\Type\Atomic\TNull; |
| 20 | +use Psalm\Type\Atomic\TResource; |
| 21 | +use Psalm\Type\Union; |
| 22 | + |
| 23 | +use function gettype; |
| 24 | +use function get_class; |
| 25 | + |
| 26 | +class ConfigHandler implements FunctionReturnTypeProviderInterface |
| 27 | +{ |
| 28 | + public static function getFunctionIds(): array |
| 29 | + { |
| 30 | + return ['config']; |
| 31 | + } |
| 32 | + |
| 33 | + public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $event): ?Union |
| 34 | + { |
| 35 | + // we're going to attempt some dynamic analysis to tighten the actual return type here. |
| 36 | + // this could be done statically, but it's quicker + easier to do this dynamically. |
| 37 | + // PRs to make this static in the future more than welcome! |
| 38 | + $call_args = $event->getCallArgs(); |
| 39 | + if (!isset($call_args[0])) { |
| 40 | + return new Union([ |
| 41 | + new TNamedObject(Repository::class), |
| 42 | + ]); |
| 43 | + } |
| 44 | + |
| 45 | + $argumentType = $call_args[0]->value; |
| 46 | + |
| 47 | + if (!$argumentType->value) { |
| 48 | + return null; |
| 49 | + } |
| 50 | + |
| 51 | + $argumentValue = $argumentType->value; |
| 52 | + |
| 53 | + try { |
| 54 | + // dynamic analysis |
| 55 | + $returnValue = ApplicationProvider::getApp()->make('config')->get($argumentValue); |
| 56 | + } catch (\Throwable $t) { |
| 57 | + return null; |
| 58 | + } |
| 59 | + |
| 60 | + // turn actual return value into a psalm type. there's probably a helper in psalm to do this, but i couldn't find one |
| 61 | + switch (gettype($returnValue)) { |
| 62 | + case 'boolean': |
| 63 | + $type = new TBool(); |
| 64 | + break; |
| 65 | + case 'integer': |
| 66 | + $type = new TLiteralInt($returnValue); |
| 67 | + break; |
| 68 | + case 'double': |
| 69 | + $type = new TLiteralFloat($returnValue); |
| 70 | + break; |
| 71 | + case 'string': |
| 72 | + $type = new TLiteralString($returnValue); |
| 73 | + break; |
| 74 | + case 'array': |
| 75 | + $type = new TArray([ |
| 76 | + new Union([new TArrayKey()]), |
| 77 | + new Union([new TMixed()]), |
| 78 | + ]); |
| 79 | + break; |
| 80 | + case 'object': |
| 81 | + $type = new TNamedObject(get_class($returnValue)); |
| 82 | + break; |
| 83 | + case 'resource': |
| 84 | + $type = new TResource(); |
| 85 | + break; |
| 86 | + case 'resource (closed)': |
| 87 | + $type = new TClosedResource(); |
| 88 | + break; |
| 89 | + case 'NULL': |
| 90 | + if (isset($call_args[1])) { |
| 91 | + return $event->getStatementsSource()->getNodeTypeProvider()->getType($call_args[1]->value); |
| 92 | + } |
| 93 | + $type = new TNull(); |
| 94 | + break; |
| 95 | + case 'unknown type': |
| 96 | + default: |
| 97 | + $type = new TMixed(); |
| 98 | + break; |
| 99 | + } |
| 100 | + |
| 101 | + return new Union([ |
| 102 | + $type, |
| 103 | + ]); |
| 104 | + } |
| 105 | +} |
0 commit comments