Passed
Pull Request — master (#49)
by Alexander
05:06 queued 02:25
created

InspectController   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 12
eloc 37
c 3
b 0
f 0
dl 0
loc 89
ccs 0
cts 42
cp 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A command() 0 9 1
A classes() 0 30 4
A config() 0 15 3
A params() 0 6 1
A object() 0 14 2
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): ResponseInterface
27
    {
28
        $config = $container->get(ConfigInterface::class);
29
30
        // TODO: pass different envs
31
        $data = $config->get('web');
32
        ksort($data);
33
        foreach ($data as &$value) {
34
//            $value = get_debug_type($value);
35
            if ($value instanceof Closure) {
36
                $value = VarDumper::create($value)->asString();
37
            }
38
        }
39
40
        return $this->responseFactory->createResponse($data);
41
    }
42
43
    public function params(): ResponseInterface
44
    {
45
        $params = ApplicationState::$params;
46
        ksort($params);
47
48
        return $this->responseFactory->createResponse($params);
49
    }
50
51
    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

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