Completed
Push — develop ( 722f70...af048b )
by Jaap
15:12 queued 05:04
created

createArgumentDescriptorForMagicMethod()   C

Complexity

Conditions 15
Paths 56

Size

Total Lines 44
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 15
eloc 28
nc 56
nop 1
dl 0
loc 44
rs 5.0504
c 1
b 1
f 0

How to fix   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
 * phpDocumentor
4
 *
5
 * PHP Version 5.3
6
 *
7
 * @copyright 2010-2014 Mike van Riel / Naenius (http://www.naenius.com)
8
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
9
 * @link      http://phpdoc.org
10
 */
11
12
namespace phpDocumentor\Descriptor\Builder\Reflector\Tags;
13
14
use phpDocumentor\Descriptor\ArgumentDescriptor;
15
use phpDocumentor\Descriptor\Builder\Reflector\AssemblerAbstract;
16
use phpDocumentor\Descriptor\Tag\MethodDescriptor;
17
use phpDocumentor\Descriptor\Tag\ReturnDescriptor;
18
use phpDocumentor\Reflection\DocBlock\Tag\MethodTag;
19
use phpDocumentor\Reflection\DocBlock\Type\Collection;
20
21
/**
22
 * Constructs a new descriptor from the Reflector for an `@method` tag.
23
 *
24
 * This object will read the reflected information for the `@method` tag and create a {@see MethodDescriptor} object
25
 * that can be used in the rest of the application and templates.
26
 */
27
class MethodAssembler extends AssemblerAbstract
28
{
29
    /**
30
     * Creates a new Descriptor from the given Reflector.
31
     *
32
     * @param MethodTag $data
33
     *
34
     * @return MethodDescriptor
35
     */
36
    public function create($data)
37
    {
38
        $descriptor = new MethodDescriptor($data->getName());
39
        $descriptor->setDescription($data->getDescription());
40
        $descriptor->setMethodName($data->getMethodName());
41
42
        $response = new ReturnDescriptor('return');
43
        $response->setTypes($this->builder->buildDescriptor(new Collection($data->getTypes())));
44
        $descriptor->setResponse($response);
45
46
        foreach ($data->getArguments() as $argument) {
47
            $argumentDescriptor = $this->createArgumentDescriptorForMagicMethod($argument);
0 ignored issues
show
Documentation introduced by
$argument is of type string, but the function expects a array<integer,string>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
48
            $descriptor->getArguments()->set($argumentDescriptor->getName(), $argumentDescriptor);
49
        }
50
51
        return $descriptor;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $descriptor; (phpDocumentor\Descriptor\Tag\MethodDescriptor) is incompatible with the return type declared by the interface phpDocumentor\Descriptor...emblerInterface::create of type phpDocumentor\Descriptor...r\Descriptor\Collection.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
52
    }
53
54
    /**
55
     * Construct an argument descriptor given the array representing an argument with a Method Tag in the Reflection
56
     * component.
57
     *
58
     * @param string[] $argument
59
     *
60
     * @return ArgumentDescriptor
61
     */
62
    private function createArgumentDescriptorForMagicMethod($argument)
0 ignored issues
show
Complexity introduced by
This operation has 1020 execution paths which exceeds the configured maximum of 200.

A high number of execution paths generally suggests many nested conditional statements and make the code less readible. This can usually be fixed by splitting the method into several smaller methods.

You can also find more information in the “Code” section of your repository.

Loading history...
63
    {
64
        $argumentType = null;
65
        $argumentName = null;
66
        $argumentDefault = false; // false means we have not encountered the '=' yet.
67
        foreach ($argument as $part) {
68
            $part = trim($part);
69
            if (!$part) {
70
                continue;
71
            }
72
73
            // Type should not be assigned after name
74
            if (!$argumentName && !$argumentType && $part{0} != '$') {
0 ignored issues
show
Bug Best Practice introduced by
The expression $argumentName of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null 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...
Bug Best Practice introduced by
The expression $argumentType of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null 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...
75
                $argumentType = $part;
76
            } elseif (!$argumentName && $part{0} == '$') {
0 ignored issues
show
Bug Best Practice introduced by
The expression $argumentName of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null 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...
77
                $argumentName = $part;
78
            } elseif ($part == '=') {
79
                $argumentDefault = null;
80
            } elseif ($argumentDefault === null) {
81
                $argumentDefault = $part;
82
            }
83
        }
84
        if ($argumentDefault === false) {
85
            $argumentDefault = null;
86
        }
87
88
        // if no name is set but a type is then the input is malformed and we correct for it
89
        if ($argumentType && !$argumentName) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $argumentType of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null 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...
Bug Best Practice introduced by
The expression $argumentName of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null 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...
90
            $argumentName = $argumentType;
91
            $argumentType = null;
92
        }
93
94
        // if there is no type then we assume it is 'mixed'
95
        if (!$argumentType) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $argumentType of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null 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...
96
            $argumentType = 'mixed';
97
        }
98
99
        $argumentDescriptor = new ArgumentDescriptor();
100
        $argumentDescriptor->setTypes($this->builder->buildDescriptor(new Collection(array($argumentType))));
101
        $argumentDescriptor->setName($argumentName[0] == '$' ? $argumentName : '$' . $argumentName);
102
        $argumentDescriptor->setDefault($argumentDefault);
103
104
        return $argumentDescriptor;
105
    }
106
}
107