-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAssuranceMap.php
More file actions
73 lines (59 loc) · 1.94 KB
/
AssuranceMap.php
File metadata and controls
73 lines (59 loc) · 1.94 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
63
64
65
66
67
68
69
70
71
72
73
<?php
/*
* SPDX-License-Identifier: ISC
* SPDX-FileCopyrightText: (c) Respect Project Contributors
* SPDX-FileContributor: Alexandre Gomes Gaigalas <alganet@gmail.com>
*/
declare(strict_types=1);
namespace Respect\FluentAnalysis;
use PHPStan\Reflection\ClassReflection;
use function in_array;
/**
* @phpstan-type AssuranceEntry array{
* type?: string,
* parameterIndex?: int,
* from?: string,
* compose?: string,
* composeRange?: array{int, int|null},
* wrapperModifier?: string,
* }
*/
final readonly class AssuranceMap
{
/**
* @param array<string, array<string, AssuranceEntry>> $assurances
* @param array<string, list<string>> $assertions
*/
public function __construct(
private array $assurances = [],
private array $assertions = [],
) {
}
/** @return AssuranceEntry|null */
public function resolveAssurance(ClassReflection $classReflection, string $methodName): array|null
{
$className = $classReflection->getName();
if (isset($this->assurances[$className][$methodName])) {
return $this->assurances[$className][$methodName];
}
foreach ($this->assurances as $registeredClass => $methods) {
if ($classReflection->is($registeredClass) && isset($methods[$methodName])) {
return $methods[$methodName];
}
}
return null;
}
public function isAssertionMethod(ClassReflection $classReflection, string $methodName): bool
{
$className = $classReflection->getName();
if (isset($this->assertions[$className])) {
return in_array($methodName, $this->assertions[$className], true);
}
foreach ($this->assertions as $registeredClass => $methods) {
if ($classReflection->is($registeredClass) && in_array($methodName, $methods, true)) {
return true;
}
}
return false;
}
}