| Conditions | 19 |
| Paths | > 20000 |
| Total Lines | 116 |
| Code Lines | 68 |
| Lines | 17 |
| Ratio | 14.66 % |
| Changes | 5 | ||
| Bugs | 2 | Features | 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 |
||
| 38 | protected function execute(InputInterface $input, OutputInterface $output) |
||
|
|
|||
| 39 | { |
||
| 40 | $env = []; |
||
| 41 | |||
| 42 | // here to work around issues with pcntl and cli_set_process_title in PHP > 5.5 |
||
| 43 | if (version_compare(PHP_VERSION, '5.5.0') >= 0) { |
||
| 44 | $env = $_SERVER; |
||
| 45 | unset( |
||
| 46 | $env['_'], |
||
| 47 | $env['PHP_SELF'], |
||
| 48 | $env['SCRIPT_NAME'], |
||
| 49 | $env['SCRIPT_FILENAME'], |
||
| 50 | $env['PATH_TRANSLATED'], |
||
| 51 | $env['argv'] |
||
| 52 | ); |
||
| 53 | } |
||
| 54 | |||
| 55 | $env['APP_INCLUDE'] = $this->getContainer()->getParameter('resque.app_include'); |
||
| 56 | $env['COUNT'] = $input->getOption('count'); |
||
| 57 | $env['INTERVAL'] = $input->getOption('interval'); |
||
| 58 | $env['QUEUE'] = $input->getArgument('queues'); |
||
| 59 | $env['VERBOSE'] = 1; |
||
| 60 | |||
| 61 | if (FALSE !== getenv('APP_INCLUDE')) { |
||
| 62 | $env['APP_INCLUDE'] = getenv('APP_INCLUDE'); |
||
| 63 | } |
||
| 64 | |||
| 65 | $prefix = $this->getContainer()->getParameter('resque.prefix'); |
||
| 66 | if (!empty($prefix)) { |
||
| 67 | $env['PREFIX'] = $this->getContainer()->getParameter('resque.prefix'); |
||
| 68 | } |
||
| 69 | |||
| 70 | if ($input->getOption('verbose')) { |
||
| 71 | $env['VVERBOSE'] = 1; |
||
| 72 | } |
||
| 73 | |||
| 74 | if ($input->getOption('quiet')) { |
||
| 75 | unset($env['VERBOSE']); |
||
| 76 | } |
||
| 77 | |||
| 78 | $redisHost = $this->getContainer()->getParameter('resque.redis.host'); |
||
| 79 | $redisPort = $this->getContainer()->getParameter('resque.redis.port'); |
||
| 80 | $redisDatabase = $this->getContainer()->getParameter('resque.redis.database'); |
||
| 81 | |||
| 82 | View Code Duplication | if ($redisHost != NULL && $redisPort != NULL) { |
|
| 83 | $env['REDIS_BACKEND'] = $redisHost . ':' . $redisPort; |
||
| 84 | } |
||
| 85 | |||
| 86 | if (isset($redisDatabase)) { |
||
| 87 | $env['REDIS_BACKEND_DB'] = $redisDatabase; |
||
| 88 | } |
||
| 89 | |||
| 90 | $opt = ''; |
||
| 91 | if (0 !== $m = (int)$input->getOption('memory-limit')) { |
||
| 92 | $opt = sprintf('-d memory_limit=%dM', $m); |
||
| 93 | } |
||
| 94 | |||
| 95 | View Code Duplication | if (version_compare(PHP_VERSION, '5.4.0') >= 0) { |
|
| 96 | $phpExecutable = PHP_BINARY; |
||
| 97 | } else { |
||
| 98 | $phpExecutable = PHP_BINDIR . '/php'; |
||
| 99 | if (defined('PHP_WINDOWS_VERSION_BUILD')) { |
||
| 100 | $phpExecutable = 'php'; |
||
| 101 | } |
||
| 102 | } |
||
| 103 | |||
| 104 | $workerCommand = strtr('%php% %opt% %dir%/resque', [ |
||
| 105 | '%php%' => $phpExecutable, |
||
| 106 | '%opt%' => $opt, |
||
| 107 | '%dir%' => __DIR__ . '/../bin', |
||
| 108 | ]); |
||
| 109 | |||
| 110 | if (!$input->getOption('foreground')) { |
||
| 111 | $workerCommand = strtr('nohup %cmd% > %logs_dir%/resque.log 2>&1 & echo $!', [ |
||
| 112 | '%cmd%' => $workerCommand, |
||
| 113 | '%logs_dir%' => $this->getContainer()->getParameter('kernel.logs_dir'), |
||
| 114 | ]); |
||
| 115 | } |
||
| 116 | |||
| 117 | // In windows: When you pass an environment to CMD it replaces the old environment |
||
| 118 | // That means we create a lot of problems with respect to user accounts and missing vars |
||
| 119 | // this is a workaround where we add the vars to the existing environment. |
||
| 120 | View Code Duplication | if (defined('PHP_WINDOWS_VERSION_BUILD')) { |
|
| 121 | foreach ($env as $key => $value) { |
||
| 122 | putenv($key . "=" . $value); |
||
| 123 | } |
||
| 124 | $env = NULL; |
||
| 125 | } |
||
| 126 | |||
| 127 | $process = new Process($workerCommand, NULL, $env, NULL, NULL); |
||
| 128 | |||
| 129 | if (!$input->getOption('quiet')) { |
||
| 130 | $output->writeln(\sprintf('Starting worker <info>%s</info>', $process->getCommandLine())); |
||
| 131 | } |
||
| 132 | |||
| 133 | // if foreground, we redirect output |
||
| 134 | if ($input->getOption('foreground')) { |
||
| 135 | $process->run(function ($type, $buffer) use ($output) { |
||
| 136 | $output->write($buffer); |
||
| 137 | }); |
||
| 138 | } // else we recompose and display the worker id |
||
| 139 | else { |
||
| 140 | $process->run(); |
||
| 141 | $pid = \trim($process->getOutput()); |
||
| 142 | |||
| 143 | if (function_exists('gethostname')) { |
||
| 144 | $hostname = gethostname(); |
||
| 145 | } else { |
||
| 146 | $hostname = php_uname('n'); |
||
| 147 | } |
||
| 148 | |||
| 149 | if (!$input->getOption('quiet')) { |
||
| 150 | $output->writeln(\sprintf('<info>Worker started</info> %s:%s:%s', $hostname, $pid, $input->getArgument('queues'))); |
||
| 151 | } |
||
| 152 | } |
||
| 153 | } |
||
| 154 | } |
||
| 155 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: