Passed
Push — master ( 517a56...adf5dc )
by Alexander
02:50
created

InspectController::command()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 18
ccs 0
cts 8
cp 0
rs 9.9666
cc 2
nc 2
nop 2
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug\Api\Controller;
6
7
use Closure;
8
use InvalidArgumentException;
9
use Psr\Container\ContainerInterface;
10
use Psr\Http\Message\ResponseInterface;
11
use Psr\Http\Message\ServerRequestInterface;
12
use ReflectionClass;
13
use Yiisoft\Config\ConfigInterface;
14
use Yiisoft\DataResponse\DataResponseFactoryInterface;
15
use Yiisoft\VarDumper\VarDumper;
16
use Yiisoft\Yii\Debug\Api\Inspector\ApplicationState;
17
use Yiisoft\Yii\Debug\Api\Inspector\Command\CodeceptionCommand;
18
use Yiisoft\Yii\Debug\Api\Inspector\Command\PHPUnitCommand;
19
20
class InspectController
21
{
22
    public function __construct(
23
        private DataResponseFactoryInterface $responseFactory,
24
    ) {
25
    }
26
27
    public function config(ContainerInterface $container, ServerRequestInterface $request): ResponseInterface
28
    {
29
        $config = $container->get(ConfigInterface::class);
30
31
        $request = $request->getQueryParams();
32
        $group = $request['group'] ?? 'web';
33
34
        $data = $config->get($group);
35
        ksort($data);
36
37
        foreach ($data as &$value) {
38
            if ($value instanceof Closure) {
39
                $value = VarDumper::create($value)->asString();
40
            }
41
        }
42
43
        return $this->responseFactory->createResponse($data);
44
    }
45
46
    public function params(): ResponseInterface
47
    {
48
        $params = ApplicationState::$params;
49
        ksort($params);
50
51
        return $this->responseFactory->createResponse($params);
52
    }
53
54
    public function classes(ContainerInterface $container): ResponseInterface
0 ignored issues
show
Unused Code introduced by
The parameter $container is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

54
    public function classes(/** @scrutinizer ignore-unused */ ContainerInterface $container): ResponseInterface

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
55
    {
56
        // TODO: how to get params for console or other param groups?
57
        $classes = [];
58
59
        $inspected = array_merge(get_declared_classes(), get_declared_interfaces());
60
        // TODO: think how to ignore heavy objects
61
        $patterns = [
62
            fn (string $class) => !str_starts_with($class, 'ComposerAutoloaderInit'),
63
            fn (string $class) => !str_starts_with($class, 'Composer\\'),
64
            fn (string $class) => !str_starts_with($class, 'Yiisoft\\Yii\\Debug\\'),
65
            fn (string $class) => !str_starts_with($class, 'Yiisoft\\ErrorHandler\\ErrorHandler'),
66
            fn (string $class) => !str_contains($class, '@anonymous'),
67
        ];
68
        foreach ($patterns as $patternFunction) {
69
            $inspected = array_filter($inspected, $patternFunction);
70
        }
71
72
        foreach ($inspected as $className) {
73
            $class = new ReflectionClass($className);
74
75
            if ($class->isInternal()) {
76
                continue;
77
            }
78
79
            $classes[] = $className;
80
        }
81
        sort($classes);
82
83
        return $this->responseFactory->createResponse($classes);
84
    }
85
86
    public function object(ContainerInterface $container, ServerRequestInterface $request): ResponseInterface
87
    {
88
        $request = $request->getQueryParams();
89
        $className = $request['classname'];
90
91
        $class = new ReflectionClass($className);
92
93
        if ($class->isInternal()) {
94
            throw new InvalidArgumentException('error');
95
        }
96
97
        $result = VarDumper::create($container->get($className))->asString();
98
99
        return $this->responseFactory->createResponse($result);
100
    }
101
102
    public function command(ServerRequestInterface $request, ContainerInterface $container): ResponseInterface
103
    {
104
        // TODO: would be great to recognise test engine automatically
105
        $map = [
106
            'test/phpunit' => PHPUnitCommand::class,
107
            'test/codeception' => CodeceptionCommand::class,
108
        ];
109
110
        $request = $request->getQueryParams();
111
        $commandName = $request['command'] ?? 'test/codeception';
112
113
        if (!array_key_exists($commandName, $map)) {
114
            throw new InvalidArgumentException('Unknown command');
115
        }
116
117
        $result = $container->get($map[$commandName])->run();
118
119
        return $this->responseFactory->createResponse($result);
120
    }
121
}
122