Completed
Pull Request — develop (#87)
by Jaap
03:38
created

Argument::create()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 18
ccs 10
cts 10
cp 1
rs 9.2
cc 4
eloc 10
nc 3
nop 3
crap 4
1
<?php
2
/**
3
 * This file is part of phpDocumentor.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @copyright 2010-2015 Mike van Riel<[email protected]>
9
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
10
 * @link      http://phpdoc.org
11
 */
12
13
namespace phpDocumentor\Reflection\Php\Factory;
14
15
use InvalidArgumentException;
16
use phpDocumentor\Reflection\Php\Argument as ArgumentDescriptor;
17
use phpDocumentor\Reflection\Php\Factory;
18
use phpDocumentor\Reflection\Php\ProjectFactoryStrategy;
19
use phpDocumentor\Reflection\Php\StrategyContainer;
20
use phpDocumentor\Reflection\PrettyPrinter;
21
use phpDocumentor\Reflection\Types\Context;
22
use PhpParser\Node\Param;
23
24
/**
25
 * Strategy to convert Param to Argument
26
 *
27
 * @see \phpDocumentor\Descriptor\Argument
28
 * @see \PhpParser\Node\Arg
29
 */
30
final class Argument implements ProjectFactoryStrategy
31
{
32
    /**
33
     * @var PrettyPrinter
34
     */
35
    private $valueConverter;
36
37
    /**
38
     * Initializes the object.
39
     *
40
     * @param PrettyPrinter $prettyPrinter
41
     */
42
    public function __construct(PrettyPrinter $prettyPrinter)
43
    {
44
        $this->valueConverter = $prettyPrinter;
45
    }
46
47
    /**
48
     * Returns true when the strategy is able to handle the object.
49
     *
50
     * @param Param $object object to check.
51
     * @return boolean
52
     */
53 1
    public function matches($object)
54
    {
55 1
        return $object instanceof Param;
56
    }
57
58
    /**
59
     * Creates an ArgumentDescriptor out of the given object.
60
     * Since an object might contain other objects that need to be converted the $factory is passed so it can be
61
     * used to create nested Elements.
62
     *
63
     * @param Param $object object to convert to an Element
64
     * @param StrategyContainer $strategies used to convert nested objects.
65
     * @param Context $context of the created object
66
     * @return ArgumentDescriptor
67
     */
68 2
    public function create($object, StrategyContainer $strategies, Context $context = null)
69
    {
70 2
        if (!$this->matches($object)) {
71 1
            throw new InvalidArgumentException(
72 1
                sprintf('%s cannot handle objects with the type %s',
73 1
                    __CLASS__,
74 1
                    is_object($object) ? get_class($object) : gettype($object)
75
                )
76
            );
77
        }
78
79 1
        $default = null;
80 1
        if ($object->default !== null) {
81 1
            $default = $this->valueConverter->prettyPrintExpr($object->default);
82
        }
83
84 1
        return new ArgumentDescriptor($object->name, $default, $object->byRef, $object->variadic);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \phpDocumento...ef, $object->variadic); (phpDocumentor\Reflection\Php\Argument) is incompatible with the return type declared by the interface phpDocumentor\Reflection...FactoryStrategy::create of type phpDocumentor\Reflection\Element.

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...
85
    }
86
}
87