Passed
Pull Request — master (#49)
by Dmitriy
12:07
created

InspectController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 30
c 1
b 0
f 0
dl 0
loc 71
ccs 0
cts 27
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A classes() 0 29 4
A index() 0 11 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 Psr\Container\ContainerInterface;
9
use Psr\Http\Message\ResponseInterface;
10
use Psr\Http\Message\ServerRequestInterface;
11
use ReflectionClass;
12
use Yiisoft\DataResponse\DataResponseFactoryInterface;
13
use Yiisoft\VarDumper\VarDumper;
14
use Yiisoft\Yii\Debug\Api\ApplicationState;
15
use Yiisoft\Yii\Debug\Api\Repository\CollectorRepositoryInterface;
16
17
class InspectController
18
{
19
    private DataResponseFactoryInterface $responseFactory;
20
    private CollectorRepositoryInterface $collectorRepository;
21
22
    public function __construct(
23
        DataResponseFactoryInterface $responseFactory,
24
        CollectorRepositoryInterface $collectorRepository
25
    ) {
26
        $this->responseFactory = $responseFactory;
27
        $this->collectorRepository = $collectorRepository;
28
    }
29
30
    public function index(): ResponseInterface
31
    {
32
        // TODO: how to get params for console or other param groups?
33
        $params = ApplicationState::$params;
34
35
        // TODO: also would be nice to inspect application config to access to di config at least
36
37
//        $config = ApplicationState::$config;
38
//        return $this->responseFactory->createResponse([$config->get('web'), $params]);
39
40
        return $this->responseFactory->createResponse($params);
41
    }
42
43
    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

43
    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...
44
    {
45
        // TODO: how to get params for console or other param groups?
46
        $classes = [];
47
48
        $inspected = array_merge(get_declared_classes(), get_declared_interfaces());
49
        // TODO: think how to ignore heavy objects
50
        $patterns = [
51
            'ComposerAutoloaderInit',
52
            'Composer\\',
53
            'Yiisoft\\Yii\\Debug\\',
54
            'Yiisoft\\ErrorHandler\\ErrorHandler',
55
        ];
56
        foreach ($patterns as $pattern) {
57
            $inspected = array_filter($inspected, fn (string $class) => !str_starts_with($class, $pattern));
58
        }
59
60
        foreach ($inspected as $className) {
61
            $class = new ReflectionClass($className);
62
63
            if ($class->isInternal()) {
64
                continue;
65
            }
66
67
            $classes[] = $className;
68
        }
69
        sort($classes);
70
71
        return $this->responseFactory->createResponse($classes);
72
    }
73
74
    public function object(ContainerInterface $container, ServerRequestInterface $request): ResponseInterface
75
    {
76
        $request = $request->getQueryParams();
77
        $className = $request['classname'];
78
79
        $class = new ReflectionClass($className);
80
81
        if ($class->isInternal()) {
82
            throw new InvalidArgumentException('error');
83
        }
84
85
        $result = VarDumper::create($container->get($className))->asString();
86
87
        return $this->responseFactory->createResponse($result);
88
    }
89
}
90