Completed
Pull Request — master (#30)
by Anton
17:34
created

StatusCommand   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 53
Duplicated Lines 26.42 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 30.43%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 6
lcom 2
cbo 4
dl 14
loc 53
ccs 7
cts 23
cp 0.3043
rs 10
c 1
b 0
f 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 14 14 1
B execute() 0 27 5

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
 * @copyright Bluz PHP Team
4
 * @link https://github.com/bluzphp/bluzman
5
 */
6
7
namespace Bluzman\Command\Server;
8
9
use Bluzman\Input\InputOption;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Symfony\Component\Process\Process;
13
14
/**
15
 * StatusCommand
16
 *
17
 * @package  Bluzman\Command\Server
18
 *
19
 * @author   Pavel Machekhin
20
 * @created  2013-06-17 14:52
21
 */
22
class StatusCommand extends AbstractServerCommand
23
{
24
    /**
25
     * Command configuration
26
     */
27 14 View Code Duplication
    protected function configure()
0 ignored issues
show
Duplication introduced by
This method 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...
28
    {
29
        $this
30
            // the name of the command (the part after "bin/bluzman")
31 14
            ->setName('server:status')
32
            // the short description shown while running "php bin/bluzman list"
33 14
            ->setDescription('Get the status of built-in PHP server')
34
            // the full command description shown when running the command with
35
            // the "--help" option
36 14
            ->setHelp('This command allows you to check built-in PHP server')
37
        ;
38 14
        $this->addOption('host', null, InputOption::VALUE_OPTIONAL, 'IP address of the server', '0.0.0.0');
39 14
        $this->addOption('port', null, InputOption::VALUE_OPTIONAL, 'Port of the server', '8000');
40 14
    }
41
42
    /**
43
     * @param InputInterface $input
44
     * @param OutputInterface $output
45
     * @return int|null|void
46
     */
47
    protected function execute(InputInterface $input, OutputInterface $output)
48
    {
49
        $this->info('Running "server:status" command');
50
        try {
51
            $host = $input->getOption('host');
52
            $port = $input->getOption('port');
53
54
            $pid = $this->getProcessId($host, $port) ?: false;
55
56
            if (!$pid) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $pid of type string|false is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
57
                throw new NotRunningException;
58
            }
59
60
            $process = new Process("ps -p $pid -o comm=");
61
            $process->run();
62
63
            $processOutput = $process->getOutput();
64
65
            if (empty($processOutput)) {
66
                throw new NotRunningException;
67
            }
68
69
            $this->write("Server <info>$host:$port</info> is running. PID is <info>$pid</info>");
70
        } catch (NotRunningException $e) {
71
            $this->comment('Server is not running');
72
        }
73
    }
74
}
75