Completed
Push — master ( e6c9d7...cc6bd7 )
by Grégoire
12:23
created

ExplainAdminCommand::execute()   D

Complexity

Conditions 13
Paths 256

Size

Total Lines 107

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 107
rs 4.0666
c 0
b 0
f 0
cc 13
nc 256
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\AdminBundle\Command;
15
16
use Sonata\AdminBundle\Admin\Pool;
17
use Symfony\Component\Console\Command\Command;
18
use Symfony\Component\Console\Input\InputArgument;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Output\OutputInterface;
21
use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface;
22
23
/**
24
 * @author Thomas Rabaix <[email protected]>
25
 */
26
class ExplainAdminCommand extends Command
27
{
28
    /**
29
     * {@inheritdoc}
30
     */
31
    protected static $defaultName = 'sonata:admin:explain';
32
33
    /**
34
     * @var Pool
35
     */
36
    private $pool;
37
38
    /**
39
     * @var MetadataFactoryInterface
40
     */
41
    private $validator;
42
43
    public function __construct(Pool $pool, MetadataFactoryInterface $validator)
44
    {
45
        $this->pool = $pool;
46
        $this->validator = $validator;
47
48
        parent::__construct();
49
    }
50
51
    public function configure(): void
52
    {
53
        $this->setDescription('Explain an admin service');
54
55
        $this->addArgument('admin', InputArgument::REQUIRED, 'The admin service id');
56
    }
57
58
    public function execute(InputInterface $input, OutputInterface $output): void
59
    {
60
        $admin = $this->pool->getInstance($input->getArgument('admin'));
0 ignored issues
show
Bug introduced by
It seems like $input->getArgument('admin') targeting Symfony\Component\Consol...nterface::getArgument() can also be of type array<integer,string> or null; however, Sonata\AdminBundle\Admin\Pool::getInstance() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
61
62
        $output->writeln('<comment>AdminBundle Information</comment>');
63
        $output->writeln(sprintf('<info>% -20s</info> : %s', 'id', $admin->getCode()));
64
        $output->writeln(sprintf('<info>% -20s</info> : %s', 'Admin', \get_class($admin)));
65
        $output->writeln(sprintf('<info>% -20s</info> : %s', 'Model', $admin->getClass()));
66
        $output->writeln(sprintf('<info>% -20s</info> : %s', 'Controller', $admin->getBaseControllerName()));
67
        $output->writeln(sprintf('<info>% -20s</info> : %s', 'Model Manager', \get_class($admin->getModelManager())));
68
        $output->writeln(sprintf('<info>% -20s</info> : %s', 'Form Builder', \get_class($admin->getFormBuilder())));
69
        $output->writeln(sprintf('<info>% -20s</info> : %s', 'Datagrid Builder', \get_class($admin->getDatagridBuilder())));
70
        $output->writeln(sprintf('<info>% -20s</info> : %s', 'List Builder', \get_class($admin->getListBuilder())));
71
72
        if ($admin->isChild()) {
73
            $output->writeln(sprintf('<info>% -15s</info> : %s', 'Parent', $admin->getParent()->getCode()));
74
        }
75
76
        $output->writeln('');
77
        $output->writeln('<info>Routes</info>');
78
        foreach ($admin->getRoutes()->getElements() as $route) {
79
            $output->writeln(sprintf('  - % -25s %s', $route->getDefault('_sonata_name'), $route->getPath()));
80
        }
81
82
        $output->writeln('');
83
        $output->writeln('<info>Datagrid Columns</info>');
84
        foreach ($admin->getListFieldDescriptions() as $name => $fieldDescription) {
85
            $output->writeln(sprintf(
86
                '  - % -25s  % -15s % -15s',
87
                $name,
88
                $fieldDescription->getType(),
89
                $fieldDescription->getTemplate()
90
            ));
91
        }
92
93
        $output->writeln('');
94
        $output->writeln('<info>Datagrid Filters</info>');
95
        foreach ($admin->getFilterFieldDescriptions() as $name => $fieldDescription) {
96
            $output->writeln(sprintf(
97
                '  - % -25s  % -15s % -15s',
98
                $name,
99
                $fieldDescription->getType(),
100
                $fieldDescription->getTemplate()
101
            ));
102
        }
103
104
        $output->writeln('');
105
        $output->writeln('<info>Form theme(s)</info>');
106
        foreach ($admin->getFormTheme() as $template) {
107
            $output->writeln(sprintf('  - %s', $template));
108
        }
109
110
        $output->writeln('');
111
        $output->writeln('<info>Form Fields</info>');
112
        foreach ($admin->getFormFieldDescriptions() as $name => $fieldDescription) {
113
            $output->writeln(sprintf(
114
                '  - % -25s  % -15s % -15s',
115
                $name,
116
                $fieldDescription->getType(),
117
                $fieldDescription->getTemplate()
118
            ));
119
        }
120
121
        $metadata = $this->validator->getMetadataFor($admin->getClass());
122
123
        $output->writeln('');
124
        $output->writeln('<comment>Validation Framework</comment> - http://symfony.com/doc/3.0/book/validation.html');
125
        $output->writeln('<info>Properties constraints</info>');
126
127
        if (0 === \count($metadata->properties)) {
0 ignored issues
show
Bug introduced by
Accessing properties on the interface Symfony\Component\Valida...pping\MetadataInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
128
            $output->writeln('    <error>no property constraints defined !!</error>');
129
        } else {
130
            foreach ($metadata->properties as $name => $property) {
0 ignored issues
show
Bug introduced by
Accessing properties on the interface Symfony\Component\Valida...pping\MetadataInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
131
                $output->writeln(sprintf('  - %s', $name));
132
133
                foreach ($property->getConstraints() as $constraint) {
134
                    $output->writeln(sprintf(
135
                        '    % -70s %s',
136
                        \get_class($constraint),
137
                        implode('|', $constraint->groups)
138
                    ));
139
                }
140
            }
141
        }
142
143
        $output->writeln('');
144
        $output->writeln('<info>Getters constraints</info>');
145
146
        if (0 === \count($metadata->getters)) {
0 ignored issues
show
Bug introduced by
Accessing getters on the interface Symfony\Component\Valida...pping\MetadataInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
147
            $output->writeln('    <error>no getter constraints defined !!</error>');
148
        } else {
149
            foreach ($metadata->getters as $name => $property) {
0 ignored issues
show
Bug introduced by
Accessing getters on the interface Symfony\Component\Valida...pping\MetadataInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
150
                $output->writeln(sprintf('  - %s', $name));
151
152
                foreach ($property->getConstraints() as $constraint) {
153
                    $output->writeln(sprintf(
154
                        '    % -70s %s',
155
                        \get_class($constraint),
156
                        implode('|', $constraint->groups)
157
                    ));
158
                }
159
            }
160
        }
161
162
        $output->writeln('');
163
        $output->writeln('<info>done!</info>');
164
    }
165
}
166