-
Notifications
You must be signed in to change notification settings - Fork 650
/
MethodIdentifier.php
70 lines (61 loc) · 1.7 KB
/
MethodIdentifier.php
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
63
64
65
66
67
68
69
70
<?php
namespace Psalm\Internal;
use InvalidArgumentException;
use function explode;
use function is_string;
use function preg_replace;
use function strpos;
use function strtolower;
/**
* @psalm-immutable
*/
class MethodIdentifier
{
public $fq_class_name;
public $method_name;
/**
* @param lowercase-string $method_name
*/
public function __construct(string $fq_class_name, string $method_name)
{
$this->fq_class_name = $fq_class_name;
$this->method_name = $method_name;
}
/**
* Takes any valid reference to a method id and converts
* it into a MethodIdentifier
*
* @param string|MethodIdentifier $method_id
*
* @psalm-pure
*/
public static function wrap($method_id): self
{
return is_string($method_id) ? static::fromMethodIdReference($method_id) : $method_id;
}
/**
* @psalm-pure
*/
public static function isValidMethodIdReference(string $method_id): bool
{
return strpos($method_id, '::') !== false;
}
/**
* @psalm-pure
*/
public static function fromMethodIdReference(string $method_id): self
{
if (!static::isValidMethodIdReference($method_id)) {
throw new InvalidArgumentException('Invalid method id reference provided: ' . $method_id);
}
// remove leading backslash if it exists
$method_id = ltrim($method_id, '\\');
$method_id_parts = explode('::', $method_id);
return new self($method_id_parts[0], strtolower($method_id_parts[1]));
}
/** @return non-empty-string */
public function __toString(): string
{
return $this->fq_class_name . '::' . $this->method_name;
}
}