Conditions | 18 |
Paths | 450 |
Total Lines | 86 |
Code Lines | 63 |
Lines | 0 |
Ratio | 0 % |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
56 | protected function execute(InputInterface $input, OutputInterface $output) |
||
57 | { |
||
58 | $verbose = OutputInterface::VERBOSITY_VERBOSE === $output->getVerbosity(); |
||
|
|||
59 | $invoke = $input->getOption('invoke'); |
||
60 | |||
61 | $sm = $this->getServiceManager()->get('ServiceManager'); |
||
62 | $registeredServices = $sm->getRegisteredServicesReal(); |
||
63 | |||
64 | $lines = array(); |
||
65 | $pad = array( |
||
66 | 'id' => 0, |
||
67 | 'type' => strlen('Instance '), |
||
68 | 'class' => strlen('Class name|type|alias'), |
||
69 | ); |
||
70 | $serviceTypeToColumnName = array( |
||
71 | 'invokableClasses' => 'Invokable', |
||
72 | 'factories' => 'Factory', |
||
73 | 'aliases' => 'Alias', |
||
74 | 'instances' => 'Instance', |
||
75 | ); |
||
76 | |||
77 | foreach ($registeredServices as $type => $services) { |
||
78 | foreach ($services as $key => $service) { |
||
79 | $lines[$key]['type'] = $serviceTypeToColumnName[$type]; |
||
80 | if (strlen($key) > $pad['id']) { |
||
81 | $pad['id'] = strlen($key); |
||
82 | } |
||
83 | |||
84 | if (is_object($service)) { |
||
85 | // As of PHP 5.4 you can rely on Closure being a Closure: php.net/manual/en/class.closure.php |
||
86 | if ($service instanceof \Closure) { |
||
87 | $r = new \ReflectionFunction($service); |
||
88 | if ($ns = $r->getNamespaceName()) { |
||
89 | $filename = basename($r->getFileName(), '.php'); |
||
90 | $lines[$key]['class'] = $ns . '\\' . $filename . '\{closure}'; |
||
91 | } else { |
||
92 | $lines[$key]['class'] = 'Closure in ' . $r->getFileName(); |
||
93 | } |
||
94 | } else { |
||
95 | $r = new \ReflectionObject($service); |
||
96 | $lines[$key]['class'] = $r->getName(); |
||
97 | } |
||
98 | } elseif (is_array($service)) { |
||
99 | $lines[$key]['class'] = 'Array'; |
||
100 | } elseif (is_string($service) && ($type != 'aliases')) { |
||
101 | $r = new \ReflectionClass($service); |
||
102 | $lines[$key]['class'] = $r->getName(); |
||
103 | } else { // Alias |
||
104 | $lines[$key]['class'] = $service; |
||
105 | } |
||
106 | |||
107 | $len = strlen($lines[$key]['class']); |
||
108 | if ('aliases' == $type) { |
||
109 | $len += 10; // add the "alias for " prefix |
||
110 | } |
||
111 | if ($len > $pad['class']) { |
||
112 | $pad['class'] = $len; |
||
113 | } |
||
114 | } |
||
115 | } |
||
116 | |||
117 | ksort($lines); |
||
118 | $output->write(sprintf('<comment>%s</comment> <comment>%s</comment> <comment>%s</comment>', |
||
119 | str_pad('Service Id', $pad['id']), |
||
120 | str_pad('Type', $pad['type']), |
||
121 | str_pad('Class Name|Type|Alias', $pad['class']))); |
||
122 | $output->writeln($invoke ? ' <comment>Invokation Status [result]</comment>' : ''); |
||
123 | foreach ($lines as $id => $line) { |
||
124 | $output->write(sprintf('<info>%s</info> ', str_pad($id, $pad['id']))); |
||
125 | $output->write(sprintf('%s ', str_pad($line['type'], $pad['type']))); |
||
126 | if ('Alias' == $line['type']) { |
||
127 | $output->write(sprintf('<comment>alias for</comment> <info>%s </info>', str_pad($line['class'], $pad['class'] - 10))); |
||
128 | } else { |
||
129 | $output->write(sprintf('%s ', str_pad($line['class'], $pad['class']))); |
||
130 | } |
||
131 | if ($invoke) { |
||
132 | try { |
||
133 | $service = $sm->get($id); |
||
134 | $output->write(sprintf(' <info>OK</info> [%s]', is_object($service) ? get_class($service) : gettype($service))); |
||
135 | } catch (\Exception $e) { |
||
136 | $output->write(' <error>FAIL</error> [' . $e->getMessage() . ']'); |
||
137 | } |
||
138 | } |
||
139 | $output->writeln(''); |
||
140 | } |
||
141 | } |
||
142 | } |
||
143 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.