Conditions | 16 |
Paths | 1153 |
Total Lines | 89 |
Code Lines | 53 |
Lines | 19 |
Ratio | 21.35 % |
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 |
||
36 | protected function execute(InputInterface $input, OutputInterface $output) |
||
37 | { |
||
38 | $pidFile = $this->getContainer()->get('kernel')->getCacheDir() . '/resque_scheduledworker.pid'; |
||
39 | if (file_exists($pidFile) && !$input->getOption('force')) { |
||
40 | throw new \Exception('PID file exists - use --force to override'); |
||
41 | } |
||
42 | |||
43 | if (file_exists($pidFile)) { |
||
44 | unlink($pidFile); |
||
45 | } |
||
46 | |||
47 | $env = [ |
||
48 | 'APP_INCLUDE' => $this->getContainer()->getParameter('resque.app_include'), |
||
49 | 'VVERBOSE' => 1, |
||
50 | 'RESQUE_PHP' => $this->getContainer()->getParameter('resque.vendor_dir') . '/chrisboulton/php-resque/lib/Resque.php', |
||
51 | 'INTERVAL' => $input->getOption('interval'), |
||
52 | ]; |
||
53 | |||
54 | if (FALSE !== getenv('APP_INCLUDE')) { |
||
55 | $env['APP_INCLUDE'] = getenv('APP_INCLUDE'); |
||
56 | } |
||
57 | |||
58 | $prefix = $this->getContainer()->getParameter('resque.prefix'); |
||
59 | |||
60 | if (!empty($prefix)) { |
||
61 | $env['PREFIX'] = $this->getContainer()->getParameter('resque.prefix'); |
||
62 | } |
||
63 | |||
64 | $redisHost = $this->getContainer()->getParameter('resque.redis.host'); |
||
65 | $redisPort = $this->getContainer()->getParameter('resque.redis.port'); |
||
66 | $redisDatabase = $this->getContainer()->getParameter('resque.redis.database'); |
||
67 | |||
68 | View Code Duplication | if ($redisHost != NULL && $redisPort != NULL) { |
|
|
|||
69 | $env['REDIS_BACKEND'] = $redisHost . ':' . $redisPort; |
||
70 | } |
||
71 | |||
72 | if (isset($redisDatabase)) { |
||
73 | $env['REDIS_BACKEND_DB'] = $redisDatabase; |
||
74 | } |
||
75 | |||
76 | View Code Duplication | if (version_compare(PHP_VERSION, '5.4.0') >= 0) { |
|
77 | $phpExecutable = PHP_BINARY; |
||
78 | } else { |
||
79 | $phpExecutable = PHP_BINDIR . '/php'; |
||
80 | if (defined('PHP_WINDOWS_VERSION_BUILD')) { |
||
81 | $phpExecutable = 'php'; |
||
82 | } |
||
83 | } |
||
84 | |||
85 | $workerCommand = $phpExecutable . ' ' . __DIR__ . '/../bin/resque-scheduler'; |
||
86 | |||
87 | if (!$input->getOption('foreground')) { |
||
88 | $logFile = $this->getContainer()->getParameter( |
||
89 | 'kernel.logs_dir' |
||
90 | ) . '/resque-scheduler_' . $this->getContainer()->getParameter('kernel.environment') . '.log'; |
||
91 | $workerCommand = 'nohup ' . $workerCommand . ' > ' . $logFile . ' 2>&1 & echo $!'; |
||
92 | } |
||
93 | |||
94 | // In windows: When you pass an environment to CMD it replaces the old environment |
||
95 | // That means we create a lot of problems with respect to user accounts and missing vars |
||
96 | // this is a workaround where we add the vars to the existing environment. |
||
97 | View Code Duplication | if (defined('PHP_WINDOWS_VERSION_BUILD')) { |
|
98 | foreach ($env as $key => $value) { |
||
99 | putenv($key . "=" . $value); |
||
100 | } |
||
101 | $env = NULL; |
||
102 | } |
||
103 | |||
104 | $process = new Process($workerCommand, NULL, $env, NULL, NULL); |
||
105 | |||
106 | $output->writeln(\sprintf('Starting worker <info>%s</info>', $process->getCommandLine())); |
||
107 | |||
108 | if ($input->getOption('foreground')) { |
||
109 | $process->run(function ($type, $buffer) use ($output) { |
||
110 | $output->write($buffer); |
||
111 | }); |
||
112 | } // else we recompose and display the worker id |
||
113 | else { |
||
114 | $process->run(); |
||
115 | $pid = \trim($process->getOutput()); |
||
116 | if (function_exists('gethostname')) { |
||
117 | $hostname = gethostname(); |
||
118 | } else { |
||
119 | $hostname = php_uname('n'); |
||
120 | } |
||
121 | $output->writeln(\sprintf('<info>Worker started</info> %s:%s', $hostname, $pid)); |
||
122 | file_put_contents($pidFile, $pid); |
||
123 | } |
||
124 | } |
||
125 | } |
||
126 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.