-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThisObjectMethodsMatcher.php
More file actions
62 lines (53 loc) · 1.84 KB
/
ThisObjectMethodsMatcher.php
File metadata and controls
62 lines (53 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);
namespace Ibexa\Behat\Core\Debug\Matcher;
use InvalidArgumentException;
use Psy\TabCompletion\Matcher\AbstractMatcher;
use Psy\TabCompletion\Matcher\ObjectMethodsMatcher;
use ReflectionClass;
use ReflectionMethod;
class ThisObjectMethodsMatcher extends ObjectMethodsMatcher
{
/**
* @param list<mixed> $tokens
* @param array<string, mixed> $info
*
* @return list<string>
*/
public function getMatches(array $tokens, array $info = []): array
{
$input = $this->getInput($tokens);
$firstToken = \array_pop($tokens);
if (self::tokenIs($firstToken, self::T_STRING)) {
// second token is the object operator
\array_pop($tokens);
}
$objectToken = \array_pop($tokens);
if (!\is_array($objectToken)) {
return [];
}
$objectName = \str_replace('$', '', $objectToken[1]);
try {
$object = $this->getVariable($objectName);
} catch (InvalidArgumentException $e) {
return [];
}
if (!\is_object($object) || $objectName !== 'this') {
return [];
}
$reflectionClass = new ReflectionClass(get_class($object));
$methods = array_column($reflectionClass->getMethods(\ReflectionMethod::IS_PROTECTED | ReflectionMethod::IS_PRIVATE), 'name');
return array_map(static function (string $function) {
return $function . '()';
}, array_values(\array_filter(
$methods,
static function ($methodName) use ($input) {
return AbstractMatcher::startsWith($input, $methodName);
}
)));
}
}