Completed
Pull Request — master (#37)
by Angelo
03:16 queued 01:16
created

ProxyArgumentGenerator::generateMethodParameter()   C

Complexity

Conditions 8
Paths 108

Size

Total Lines 52
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 8.0021

Importance

Changes 0
Metric Value
dl 0
loc 52
ccs 30
cts 31
cp 0.9677
rs 6.4314
c 0
b 0
f 0
cc 8
eloc 35
nc 108
nop 1
crap 8.0021

How to fix   Long Method   

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
declare(strict_types=1);
3
4
namespace Moka\Generator;
5
6
class ProxyArgumentGenerator
7
{
8 8
    public static function generateMethodParameter(\ReflectionParameter $parameter)
9
    {
10
        try {
11 8
            $allowsNull = $parameter->allowsNull();
12 8
            $defaultValue = null;
13 8
            if ($allowsNull) {
14 8
                $defaultValue = 'null';
15
            } else {
16 8
                $defaultValue = var_export($parameter->getDefaultValue(), true);
17
            }
18
19 8
            if ($defaultValue) {
20 8
                $defaultValue = "= $defaultValue";
21
            }
22 8
        } catch (\ReflectionException $exception) {
23 8
            $defaultValue = '';
24
        }
25
26
        try {
27 8
            $type = $parameter->getType();
28 8
            if ($type) {
29 8
                $type = ' ' . self::getType($type) . ' ';
30
            } else {
31 8
                $type = '';
32
            }
33
34 8
            $name = '$' . $parameter->getName();
0 ignored issues
show
Bug introduced by
Consider using $parameter->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
35
36
//            $canBePassByValue = $parameter->canBePassedByValue();
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
37 8
            $isPassedByReference = $parameter->isPassedByReference();
38 8
            $byReference = '';
39 8
            if ($isPassedByReference) {
40 2
                $byReference = ' &';
41
            }
42
43 8
            $isVariadic = $parameter->isVariadic();
44 8
            if ($isVariadic) {
45 2
                $type = '...';
46 2
                $byReference = '';
47 8
                $defaultValue = '';
48
            }
49
        } catch (\ReflectionException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
50
        }
51
52 8
        return sprintf(
53 8
            '%s%s%s%s',
54 8
            $type,
55 8
            $byReference,
0 ignored issues
show
Bug introduced by
The variable $byReference 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...
56 8
            $name,
0 ignored issues
show
Bug introduced by
The variable $name 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...
57 8
            $defaultValue
58
        );
59
    }
60
61 8
    protected static function getType(\ReflectionType $type)
62
    {
63 8
        return (string)$type;
64
    }
65
}
66