| Conditions | 15 |
| Paths | 1 |
| Total Lines | 72 |
| Code Lines | 40 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 17 | public function register(Container $container) |
||
| 18 | { |
||
| 19 | $container['console.cache'] = null; |
||
| 20 | |||
| 21 | $container['console.command.paths'] = function () { |
||
| 22 | $paths = array(); |
||
| 23 | |||
| 24 | return $paths; |
||
| 25 | }; |
||
| 26 | |||
| 27 | $container['console.commands'] = function () use ($container) { |
||
| 28 | $commands = array(); |
||
| 29 | |||
| 30 | return $commands; |
||
| 31 | }; |
||
| 32 | |||
| 33 | $container['console'] = function () use ($container) { |
||
| 34 | $console = new ConsoleApplication($container); |
||
| 35 | |||
| 36 | // automatically detect dispatcher (e.g. in Silex applications) |
||
| 37 | if (isset($container['dispatcher']) && !is_null($container['dispatcher'])) { |
||
| 38 | $console->setDispatcher($container['dispatcher']); |
||
| 39 | } |
||
| 40 | |||
| 41 | foreach ($container['console.commands'] as $command) { |
||
| 42 | $console->add($command); |
||
| 43 | } |
||
| 44 | |||
| 45 | if (!is_null($container['console.cache'])) { |
||
| 46 | if (!is_null($container['console.cache']) && !is_dir($container['console.cache'])) { |
||
| 47 | mkdir($container['console.cache'], 0777, true); |
||
| 48 | } |
||
| 49 | |||
| 50 | $cacheFile = $container['console.cache'] . '/saxulum-console.php'; |
||
| 51 | if ($container['debug'] || !file_exists($cacheFile)) { |
||
| 52 | file_put_contents( |
||
| 53 | $cacheFile, |
||
| 54 | '<?php return ' . var_export($container['console.command.search'](), true) . ';' |
||
| 55 | ); |
||
| 56 | } |
||
| 57 | $commandsMap = require($cacheFile); |
||
| 58 | } else { |
||
| 59 | $commandsMap = $container['console.command.search'](); |
||
| 60 | } |
||
| 61 | |||
| 62 | foreach ($commandsMap as $commandClass) { |
||
| 63 | $command = new $commandClass; |
||
| 64 | $console->add($command); |
||
| 65 | } |
||
| 66 | |||
| 67 | return $console; |
||
| 68 | }; |
||
| 69 | |||
| 70 | $container['console.command.search'] = $container->protect(function () use ($container) { |
||
| 71 | $commandsMap = array(); |
||
| 72 | foreach ($container['console.command.paths'] as $path) { |
||
| 73 | foreach (Finder::create()->files()->name('*.php')->in($path) as $file) { |
||
| 74 | /** @var SplFileInfo $file */ |
||
| 75 | $classes = ClassFinder::findClasses($file->getContents()); |
||
| 76 | foreach ($classes as $class) { |
||
| 77 | $reflectionClass = new \ReflectionClass($class); |
||
| 78 | if($reflectionClass->isSubclassOf('Saxulum\Console\Command\AbstractPimpleCommand') && |
||
| 79 | $reflectionClass->isInstantiable()) { |
||
| 80 | $commandsMap[] = $class; |
||
| 81 | } |
||
| 82 | } |
||
| 83 | } |
||
| 84 | } |
||
| 85 | |||
| 86 | return $commandsMap; |
||
| 87 | }); |
||
| 88 | } |
||
| 89 | } |
||
| 90 |