2019-10-13 07:59:52 +02:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2019-03-22 11:48:41 +01:00
|
|
|
namespace Rector\PhpSpecToPHPUnit;
|
|
|
|
|
|
|
|
use PhpParser\Node\Expr\Array_;
|
|
|
|
use PhpParser\Node\Scalar\String_;
|
|
|
|
use PhpParser\Node\Stmt\Class_;
|
2021-01-20 18:41:35 +07:00
|
|
|
use PhpParser\Node\Stmt\ClassMethod;
|
2019-03-22 11:48:41 +01:00
|
|
|
use PhpParser\Node\Stmt\Return_;
|
|
|
|
final class MatchersManipulator
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*/
|
2021-05-10 22:23:08 +00:00
|
|
|
public function resolveMatcherNamesFromClass(\PhpParser\Node\Stmt\Class_ $class) : array
|
2019-03-22 11:48:41 +01:00
|
|
|
{
|
2020-07-19 20:52:42 +02:00
|
|
|
$classMethod = $class->getMethod('getMatchers');
|
2021-05-10 22:23:08 +00:00
|
|
|
if (!$classMethod instanceof \PhpParser\Node\Stmt\ClassMethod) {
|
2019-03-22 11:48:41 +01:00
|
|
|
return [];
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!isset($classMethod->stmts[0])) {
|
2019-03-22 11:48:41 +01:00
|
|
|
return [];
|
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
if (!$classMethod->stmts[0] instanceof \PhpParser\Node\Stmt\Return_) {
|
2019-03-22 11:48:41 +01:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
/** @var Return_ $return */
|
2020-07-19 20:52:42 +02:00
|
|
|
$return = $classMethod->stmts[0];
|
2021-05-10 22:23:08 +00:00
|
|
|
if (!$return->expr instanceof \PhpParser\Node\Expr\Array_) {
|
2019-03-22 11:48:41 +01:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
$keys = [];
|
|
|
|
foreach ($return->expr->items as $arrayItem) {
|
2020-08-10 23:59:40 +02:00
|
|
|
if ($arrayItem === null) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
if ($arrayItem->key instanceof \PhpParser\Node\Scalar\String_) {
|
2019-03-22 11:48:41 +01:00
|
|
|
$keys[] = $arrayItem->key->value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $keys;
|
|
|
|
}
|
|
|
|
}
|