-
Notifications
You must be signed in to change notification settings - Fork 665
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
159 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/Psalm/Internal/Provider/ReturnTypeProvider/TriggerErrorReturnTypeProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Psalm\Internal\Provider\ReturnTypeProvider; | ||
|
||
use Psalm\Plugin\EventHandler\Event\FunctionReturnTypeProviderEvent; | ||
use Psalm\Type; | ||
|
||
use function in_array; | ||
|
||
use const E_USER_DEPRECATED; | ||
use const E_USER_ERROR; | ||
use const E_USER_NOTICE; | ||
use const E_USER_WARNING; | ||
|
||
class TriggerErrorReturnTypeProvider implements \Psalm\Plugin\EventHandler\FunctionReturnTypeProviderInterface | ||
{ | ||
/** | ||
* @return array<lowercase-string> | ||
*/ | ||
public static function getFunctionIds(): array | ||
{ | ||
return ['trigger_error']; | ||
} | ||
|
||
public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $event): ?Type\Union | ||
{ | ||
$config = $event->getStatementsSource()->getCodebase()->config; | ||
if ($config->trigger_error_exits === 'always') { | ||
return new Type\Union([new Type\Atomic\TNever()]); | ||
} | ||
|
||
if ($config->trigger_error_exits === 'none') { | ||
return new Type\Union([new Type\Atomic\TTrue()]); | ||
} | ||
|
||
//default behaviour | ||
$call_args = $event->getCallArgs(); | ||
$statements_source = $event->getStatementsSource(); | ||
if (isset($call_args[1]) | ||
&& ($array_arg_type = $statements_source->getNodeTypeProvider()->getType($call_args[1]->value)) | ||
) { | ||
$return_types = []; | ||
foreach ($array_arg_type->getAtomicTypes() as $atomicType) { | ||
if ($atomicType instanceof Type\Atomic\TLiteralInt) { | ||
if (in_array($atomicType->value, [E_USER_WARNING, E_USER_DEPRECATED, E_USER_NOTICE], true)) { | ||
$return_types[] = new Type\Atomic\TTrue(); | ||
} elseif ($atomicType->value === E_USER_ERROR) { | ||
$return_types[] = new Type\Atomic\TNever(); | ||
} else { | ||
// not recognized int literal. return false before PHP8, fatal error since | ||
$return_types[] = new Type\Atomic\TFalse(); | ||
} | ||
} else { | ||
$return_types[] = new Type\Atomic\TBool(); | ||
} | ||
} | ||
|
||
return new Type\Union($return_types); | ||
} | ||
|
||
//default value is E_USER_NOTICE, so return true | ||
return new Type\Union([new Type\Atomic\TTrue()]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters