Completed
Push — master ( e48266...c6a956 )
by Steven
8s
created

ViewCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 66
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 7
c 1
b 0
f 1
lcom 1
cbo 5
dl 66
loc 66
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 7 7 1
A execute() 13 13 2
A getCommand() 10 10 1
A getLogLocation() 9 9 3

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 namespace Magestead\Command\Log;
2
3
use Magestead\Command\ProcessCommand;
4
use Magestead\Helper\Config;
5
use Symfony\Component\Console\Command\Command;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
/**
11
 * Class ViewCommand
12
 * @package Magestead\Command\Redis
13
 */
14 View Code Duplication
class ViewCommand 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...
15
{
16
    protected $_config;
17
    protected $_projectPath;
18
19
    /**
20
     * Configure the view command
21
     */
22
    protected function configure()
23
    {
24
        $this->_projectPath = getcwd();
25
        $this->setName("log:view");
26
        $this->setDescription("View a specific server log");
27
        $this->addArgument('log', InputArgument::REQUIRED, 'access or error');
28
    }
29
30
    /**
31
     * @param InputInterface $input
32
     * @param OutputInterface $output
33
     * @return ProcessCommand
34
     */
35
    protected function execute(InputInterface $input, OutputInterface $output)
36
    {
37
        $log = $input->getArgument('log');
38
39
        $output->writeln('<info>Viewing '. ucwords($log) . ' Log</info>');
40
        $command = $this->getCommand(new Config($output), $log);
41
        if (!$command) {
42
            return $output->writeln('<error>Command not available for this application</error>');
43
        }
44
45
        $pCommand = "vagrant ssh -c '". $command ."'";
46
        return new ProcessCommand($pCommand, $this->_projectPath, $output);
47
    }
48
49
    /**
50
     * @param Config $config
51
     * @param $log
52
     * @return string
53
     */
54
    private function getCommand(Config $config, $log)
55
    {
56
        $server = $config->_config['magestead']['server'];
0 ignored issues
show
Documentation introduced by
The property _config does not exist on object<Magestead\Helper\Config>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
57
        $os = $config->_config['magestead']['os'];
0 ignored issues
show
Documentation introduced by
The property _config does not exist on object<Magestead\Helper\Config>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Comprehensibility introduced by
Avoid variables with short names like $os. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
58
59
        $location = $this->getLogLocation($server, $os);
60
        $command = 'cat /var/log/' . $location . '/' . $config->base_url . '-' . $log . '.log';
0 ignored issues
show
Documentation introduced by
The property base_url does not exist on object<Magestead\Helper\Config>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
61
62
        return $command;
63
    }
64
65
    /**
66
     * @param $server
67
     * @param $os
68
     * @return string
69
     */
70
    private function getLogLocation($server, $os)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $os. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
71
    {
72
        $location = 'nginx';
73
        if ($server != 'nginx') {
74
            $location = ($os == 'ubuntu14') ? 'apache2' : 'httpd';
75
        }
76
77
        return $location;
78
    }
79
}
80