61 lines
1.4 KiB
PHP
61 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace Phabel\Plugin;
|
|
|
|
use Phabel\Plugin;
|
|
use PhpParser\Node;
|
|
use PhpParser\Node\FunctionLike;
|
|
use PhpParser\Node\NullableType;
|
|
use PhpParser\Node\Param;
|
|
use PhpParser\Node\UnionType;
|
|
|
|
/**
|
|
* Replace all usages of a certain type in typehints.
|
|
*/
|
|
class TypeHintStripper extends Plugin
|
|
{
|
|
/**
|
|
* Strip typehint.
|
|
*
|
|
* @param null|Identifier|Name|NullableType|UnionType &$type Type
|
|
*
|
|
* @return void
|
|
*/
|
|
private function strip(?Node &$type): void
|
|
{
|
|
if (!$type || $type instanceof UnionType) {
|
|
return;
|
|
}
|
|
if ($type instanceof NullableType && $this->getConfig('nullable')) {
|
|
$type = null;
|
|
return;
|
|
}
|
|
$throwableType = $type instanceof NullableType ? $type->type : $type;
|
|
// Make this less ugly when we implement a namespace context
|
|
if (\in_array($throwableType->toString(), $this->getConfig('types'))) {
|
|
$type = null;
|
|
}
|
|
}
|
|
/**
|
|
* Remove param.
|
|
*
|
|
* @param Param $node Parameter
|
|
* @return void
|
|
*/
|
|
public function enterParam(Param $node): void
|
|
{
|
|
$this->strip($node->type);
|
|
}
|
|
/**
|
|
* Strip return throwable type.
|
|
*
|
|
* @param FunctionLike $func Function
|
|
*
|
|
* @return void
|
|
*/
|
|
public function enterFuncReturn(FunctionLike $func): void
|
|
{
|
|
$this->strip($func->getReturnType());
|
|
}
|
|
}
|