Passed
Pull Request — master (#49)
by Dmitriy
02:50
created

InspectController::object()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 14
ccs 0
cts 8
cp 0
rs 10
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 InvalidArgumentException;
8
use Closure;
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;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Config\ConfigInterface was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Yiisoft\DataResponse\DataResponseFactoryInterface;
15
use Yiisoft\VarDumper\VarDumper;
16
use Yiisoft\Yii\Debug\Api\ApplicationState;
17
use Yiisoft\Yii\Debug\Api\PhpUnitCommand;
18
19
class InspectController
20
{
21
    public function __construct(
22
        private DataResponseFactoryInterface $responseFactory,
23
    ) {
24
    }
25
26
    public function config(ContainerInterface $container, ServerRequestInterface $request): ResponseInterface
27
    {
28
        $config = $container->get(ConfigInterface::class);
29
30
        $request = $request->getQueryParams();
31
        $group = $request['group'] ?? 'web';
32
33
        $data = $config->get($group);
34
        ksort($data);
35
36
        foreach ($data as &$value) {
37
            if ($value instanceof Closure) {
38
                $value = VarDumper::create($value)->asString();
39
            }
40
        }
41
42
        return $this->responseFactory->createResponse($data);
43
    }
44
45
    public function params(): ResponseInterface
46
    {
47
        $params = ApplicationState::$params;
48
        ksort($params);
49
50
        return $this->responseFactory->createResponse($params);
51
    }
52
53
    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

53
    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...
54
    {
55
        // TODO: how to get params for console or other param groups?
56
        $classes = [];
57
58
        $inspected = array_merge(get_declared_classes(), get_declared_interfaces());
59
        // TODO: think how to ignore heavy objects
60
        $patterns = [
61
            fn (string $class) => !str_starts_with($class, 'ComposerAutoloaderInit'),
62
            fn (string $class) => !str_starts_with($class, 'Composer\\'),
63
            fn (string $class) => !str_starts_with($class, 'Yiisoft\\Yii\\Debug\\'),
64
            fn (string $class) => !str_starts_with($class, 'Yiisoft\\ErrorHandler\\ErrorHandler'),
65
            fn (string $class) => !str_contains($class, '@anonymous'),
66
        ];
67
        foreach ($patterns as $patternFunction) {
68
            $inspected = array_filter($inspected, $patternFunction);
69
        }
70
71
        foreach ($inspected as $className) {
72
            $class = new ReflectionClass($className);
73
74
            if ($class->isInternal()) {
75
                continue;
76
            }
77
78
            $classes[] = $className;
79
        }
80
        sort($classes);
81
82
        return $this->responseFactory->createResponse($classes);
83
    }
84
85
    public function object(ContainerInterface $container, ServerRequestInterface $request): ResponseInterface
86
    {
87
        $request = $request->getQueryParams();
88
        $className = $request['classname'];
89
90
        $class = new ReflectionClass($className);
91
92
        if ($class->isInternal()) {
93
            throw new InvalidArgumentException('error');
94
        }
95
96
        $result = VarDumper::create($container->get($className))->asString();
97
98
        return $this->responseFactory->createResponse($result);
99
    }
100
101
    public function command(ContainerInterface $container, PhpUnitCommand $command): ResponseInterface
102
    {
103
        // TODO: pass different commands
104
//        $request = $request->getQueryParams();
105
//        $className = $request['command'];
106
107
        $result = $command->run();
108
109
        return $this->responseFactory->createResponse($result);
110
    }
111
}
112