Completed
Push — prado-3.3 ( e90646...0b76d5 )
by Fabio
23:37 queued 03:01
created

PHP_Shell_Extensions_InlineHelp::cmdHelp()   F

Complexity

Conditions 27
Paths 44

Size

Total Lines 106

Duplication

Lines 21
Ratio 19.81 %

Importance

Changes 0
Metric Value
cc 27
nc 44
nop 1
dl 21
loc 106
rs 3.3333
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
require_once(dirname(__FILE__)."/../Extensions.php");
4
require_once(dirname(__FILE__)."/Prototypes.php");
5
6
class PHP_Shell_Extensions_InlineHelp implements PHP_Shell_Extension {
7
    public function register() {
8
        $cmd = PHP_Shell_Commands::getInstance();
9
10
        $cmd->registerCommand('#^\? #', $this, 'cmdHelp', '? <var>', 
11
            'show the DocComment a Class, Method or Function'.PHP_EOL.
12
            '    e.g.: ? fopen(), ? PHP_Shell, ? $__shell');
13
    }
14
15
    /**
16
    * handle the '?' commands
17
    *
18
    * With the help of the Reflection Class we extract the DocComments and display them
19
    * For internal Functions we extract the prototype from the php source.
20
    *
21
    * ? Class::method()
22
    * ? $obj->method()
23
    * ? Class::property
24
    * ? $obj::property
25
    * ? Class
26
    * ? $obj
27
    * ? function()
28
    *
29
    * The license of the PHP_Shell class
30
    * ? license
31
    *
32
    * @return string the help text
33
    */
34
    public function cmdHelp($l) {
0 ignored issues
show
Coding Style introduced by
cmdHelp uses the super-global variable $GLOBALS which is generally not recommended.

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:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
35
        if ("? " == substr($l, 0, strlen("? "))) {
36
            $str = substr($l, 2);
37
38
            $cmd = '';
39
            
40
            if (preg_match('#^([A-Za-z0-9_]+)::([a-zA-Z0-9_]+)\(\s*\)\s*#', $str, $a)) {
41
                /* ? Class::method() */
42
43
                $class = $a[1];
44
                $method = $a[2];
45
46
                if (false !== ($proto = PHP_ShellPrototypes::getInstance()->get($class.'::'.$method))) {
47
48
                    $cmd = sprintf("/**\n* %s\n\n* @params %s\n* @return %s\n*/\n",
49
                        $proto['description'],
50
                        $proto['params'],
51
                        $proto['return']
52
                    );
53
                } else if (class_exists($class, false)) {
54
                    $c = new ReflectionClass($class);
55
56
                    if ($c->hasMethod($method)) {
57
                        $cmd = $c->getMethod($method)->getDocComment();
58
                    }
59
                }
60
            } else if (preg_match('#^\$([A-Za-z0-9_]+)->([a-zA-Z0-9_]+)\(\s*\)\s*#', $str, $a)) {
61
                /* ? $obj->method() */
62 View Code Duplication
                if (isset($GLOBALS[$a[1]]) && is_object($GLOBALS[$a[1]])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
63
                    $class = get_class($GLOBALS[$a[1]]);
64
                    $method = $a[2];
65
                    
66
                    $c = new ReflectionClass($class);
67
    
68
                    if ($c->hasMethod($method)) {
69
                        $cmd = $c->getMethod($method)->getDocComment();
70
                    }
71
                }
72
            } else if (preg_match('#^([A-Za-z0-9_]+)::([a-zA-Z0-9_]+)\s*$#', $str, $a)) { 
73
                /* ? Class::property */
74
                $class = $a[1];
75
                $property = $a[2];
76
                if (class_exists($class, false)) {
77
                    $c = new ReflectionClass($class);
78
79
                    if ($c->hasProperty($property)) {
80
                        $cmd = $c->getProperty($property)->getDocComment();
81
                    }
82
                }
83
            } else if (preg_match('#^\$([A-Za-z0-9_]+)->([a-zA-Z0-9_]+)\s*$#', $str, $a)) { 
84
                /* ? $obj->property */
85 View Code Duplication
                if (isset($GLOBALS[$a[1]]) && is_object($GLOBALS[$a[1]])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
86
                    $class = get_class($GLOBALS[$a[1]]);
87
                    $method = $a[2];
0 ignored issues
show
Unused Code introduced by
$method is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
88
                    
89
                    $c = new ReflectionClass($class);
90
91
                    if ($c->hasProperty($property)) {
0 ignored issues
show
Bug introduced by
The variable $property seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
92
                        $cmd = $c->getProperty($property)->getDocComment();
0 ignored issues
show
Bug introduced by
The variable $property seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
93
                    }
94
95
                }
96
            } else if (preg_match('#^([A-Za-z0-9_]+)$#', $str, $a)) {
97
                /* ? Class */
98
                if (class_exists($a[1], false)) {
99
                    $c = new ReflectionClass($a[1]);
100
                    $cmd = $c->getDocComment();
101
                }
102
            } else if (preg_match('#^\$([A-Za-z0-9_]+)$#', $str, $a)) {
103
                /* ? $object */
104
                $obj = $a[1];
105
                if (isset($GLOBALS[$obj]) && is_object($GLOBALS[$obj])) {
106
                    $class = get_class($GLOBALS[$obj]);
107
108
                    $c = new ReflectionClass($class);
109
                    $cmd = $c->getDocComment();
110
                }
111
112
            } else if (preg_match('#^([A-Za-z0-9_]+)\(\s*\)$#', $str, $a)) {
113
                /* ? function() */
114
                $func = $a[1];
115
116
                if (false !== ($proto = PHP_ShellPrototypes::getInstance()->get($func))) {
117
                    $cmd = sprintf("/**\n* %s\n*\n* @params %s\n* @return %s\n*/\n",
118
                        $proto['description'],
119
                        $proto['params'],
120
                        $proto['return']
121
                    );
122
                } else if (function_exists($func)) {
123
                    $c = new ReflectionFunction($func);
124
                    $cmd = $c->getDocComment();
125
                }
126
            }
127
128
            if ($cmd == '') {
129
                $cmd = var_export(sprintf('no help found for \'%s\'', $str), 1);
130
            } else {
131
                $cmd = var_export($cmd, 1);
132
            }
133
        } else if ("?" == $l) {
134
            $cmd = $this->getHelp();
0 ignored issues
show
Bug introduced by
The method getHelp() does not seem to exist on object<PHP_Shell_Extensions_InlineHelp>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
135
            $cmd = var_export($cmd, 1);
136
        }
137
138
        return $cmd;
0 ignored issues
show
Bug introduced by
The variable $cmd does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
139
    }
140
}
141