Completed
Pull Request — develop (#91)
by Jaap
02:59
created

AbstractFactory::createMember()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4286
nc 1
cc 1
eloc 3
nop 3
crap 1
1
<?php
2
3
namespace phpDocumentor\Reflection\Php\Factory;
4
5
use phpDocumentor\Reflection\Element;
6
use phpDocumentor\Reflection\Php\ProjectFactoryStrategy;
7
use phpDocumentor\Reflection\Php\StrategyContainer;
8
use phpDocumentor\Reflection\Types\Context;
9
use PhpParser\Comment\Doc;
10
use PhpParser\Node;
11
12
abstract class AbstractFactory implements ProjectFactoryStrategy
13
{
14
    abstract public function matches($object);
15
16 42
    public final function create($object, StrategyContainer $strategies, Context $context = null)
0 ignored issues
show
Coding Style introduced by
As per PSR2, final should precede the visibility keyword.
Loading history...
17
    {
18 42
        if (! $this->matches($object)) {
19 8
            throw new \InvalidArgumentException(
20 8
                sprintf('%s cannot handle objects with the type %s',
21 8
                    __CLASS__,
22 8
                    is_object($object) ? get_class($object) : gettype($object)
23 8
                )
24 8
            );
25
        }
26
27 34
        return $this->doCreate($object, $strategies, $context);
28
    }
29
30
    abstract protected function doCreate($object, StrategyContainer $strategies, Context $context = null);
31
32
    /**
33
     * @param Node|PropertyIterator|ClassConstantIterator $stmt
34
     * @param StrategyContainer $strategies
35
     * @param Context $context
36
     * @return Element
37
     */
38 14
    protected function createMember($stmt, StrategyContainer $strategies, Context $context = null)
39
    {
40 14
        $strategy = $strategies->findMatching($stmt);
41 14
        return $strategy->create($stmt, $strategies, $context);
42
    }
43
44
    /**
45
     * @param Doc $docBlock
46
     * @param StrategyContainer $strategies
47
     * @param Context $context
48
     * @return null|\phpDocumentor\Reflection\DocBlock
49
     */
50 28
    protected function createDocBlock(Doc $docBlock = null, StrategyContainer $strategies, Context $context = null)
0 ignored issues
show
Coding Style introduced by
Parameters which have default values should be placed at the end.

If you place a parameter with a default value before a parameter with a default value, the default value of the first parameter will never be used as it will always need to be passed anyway:

// $a must always be passed; it's default value is never used.
function someFunction($a = 5, $b) { }
Loading history...
51
    {
52 28
        if ($docBlock === null) {
53 22
            return null;
54
        }
55
56 6
        return $this->createMember($docBlock, $strategies, $context);
0 ignored issues
show
Documentation introduced by
$docBlock is of type object<PhpParser\Comment\Doc>, but the function expects a object<PhpParser\Node>|o...\ClassConstantIterator>.

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...
57
    }
58
}
59