Completed
Push — master ( ea15e4...6b748c )
by Michał
04:07
created

Run   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 59
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 4
dl 59
loc 59
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 14 14 1
A execute() 19 19 3
A getNamespace() 10 10 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Phppm\Console;
4
5
use Phppm\ProcessManager;
6
use Symfony\Component\Console\Command\Command;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Style\SymfonyStyle;
11
12 View Code Duplication
class Run extends Command
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

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.

Loading history...
13
{
14
    const DEFAULT_TIME = 100;
15
16
    protected function configure()
17
    {
18
        $this->setName('run')
19
            ->setDescription('Run given script as demon process.')
20
            ->setHelp('');
21
22
        $this->addArgument(
23
            'script',
24
            InputArgument::REQUIRED,
25
            'Script that will be demonized'
26
        );
27
28
        $this->addOption('interval', 'i', InputArgument::OPTIONAL, 'Worker interval time.');
29
    }
30
31
    /**
32
     * @param InputInterface $input
33
     * @param OutputInterface $output
34
     * @throws \InvalidArgumentException
35
     */
36
    protected function execute(InputInterface $input, OutputInterface $output)
37
    {
38
        $style = new SymfonyStyle($input, $output);
39
        $script = $input->getArgument('script');
40
41
        $style->title('Script demonizer.');
42
43
        $time = $input->getOption('interval') ?: self::DEFAULT_TIME;
44
45
        $style->comment('Execute with interval: ' . $time);
46
47
        if (file_exists($script)) {
48
            require_once $script;
49
            $namespace = $this->getNamespace($script);
50
51
            $processManager = new ProcessManager();
52
            $processManager->addWorker('\\' . $namespace, $output, $time);
53
        }
54
    }
55
56
    /**
57
     * @param string $file
58
     * @return string
59
     */
60
    protected function getNamespace(string $file) : string
61
    {
62
        $source = file_get_contents($file);
63
        
64
        if (preg_match('#^namespace\s+(.+?);.*class\s+(\w+).+;$#sm', $source, $matches)) {
65
            return $matches[1].'\\'.$matches[2];
66
        }
67
68
        return '';
69
    }
70
}
71