|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace jin2chen\ApiBundle\Response; |
|
6
|
|
|
|
|
7
|
|
|
use jin2chen\ApiBundle\Response\ExceptionConverter\GenericConverter; |
|
8
|
|
|
use Symfony\Component\DependencyInjection\ServiceLocator; |
|
9
|
|
|
use Throwable; |
|
10
|
|
|
|
|
11
|
|
|
use function array_unshift; |
|
12
|
|
|
use function class_parents; |
|
13
|
|
|
use function get_class; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @psalm-import-type ExceptionResponseArray from ExceptionConverterInterface |
|
17
|
|
|
*/ |
|
18
|
|
|
final class ExceptionConverter implements ExceptionConverterInterface |
|
19
|
|
|
{ |
|
20
|
|
|
private ServiceLocator $converters; |
|
21
|
|
|
/** |
|
22
|
|
|
* @var array<string, string> |
|
23
|
|
|
*/ |
|
24
|
|
|
private array $mappings; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* ExceptionConverter constructor. |
|
28
|
|
|
* |
|
29
|
|
|
* @param ServiceLocator $converters |
|
30
|
|
|
* @param array<string, string> $mappings |
|
31
|
|
|
*/ |
|
32
|
3 |
|
public function __construct(ServiceLocator $converters, array $mappings = []) |
|
33
|
|
|
{ |
|
34
|
3 |
|
$this->converters = $converters; |
|
35
|
3 |
|
$this->mappings = $mappings; |
|
36
|
3 |
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param Throwable $e |
|
40
|
|
|
* |
|
41
|
|
|
* @return ExceptionConverterInterface |
|
42
|
|
|
*/ |
|
43
|
3 |
|
private function map(Throwable $e): ExceptionConverterInterface |
|
44
|
|
|
{ |
|
45
|
|
|
/** @var class-string[] $classNames */ |
|
46
|
3 |
|
$classNames = array_values(class_parents($e, false)); |
|
47
|
3 |
|
array_unshift($classNames, get_class($e)); |
|
48
|
3 |
|
$class = GenericConverter::class; |
|
49
|
3 |
|
foreach ($classNames as $className) { |
|
50
|
3 |
|
if (isset($this->mappings[$className])) { |
|
51
|
2 |
|
$class = $this->mappings[$className]; |
|
52
|
2 |
|
break; |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** @noinspection PhpUnnecessaryLocalVariableInspection */ |
|
57
|
|
|
/** @var ExceptionConverterInterface $service */ |
|
58
|
3 |
|
$service = $this->converters->get($class); |
|
59
|
3 |
|
return $service; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @return ExceptionResponseArray |
|
|
|
|
|
|
64
|
|
|
*/ |
|
65
|
3 |
|
public function convert(Throwable $e): array |
|
66
|
|
|
{ |
|
67
|
3 |
|
return $this->map($e)->convert($e); |
|
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths