Completed
Push — develop ( 9193e7...62056c )
by Jaap
12:45 queued 02:43
created

IsParamTypeNotAnIdeDefaultValidator::validate()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 18
nc 5
nop 2
dl 0
loc 31
rs 8.439
c 0
b 0
f 0
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\Plugin\Core\Descriptor\Validator\Constraints\Functions;
13
14
use phpDocumentor\Descriptor\Collection;
15
use phpDocumentor\Descriptor\Interfaces\TypeInterface;
16
use phpDocumentor\Descriptor\MethodDescriptor;
17
use phpDocumentor\Descriptor\FunctionDescriptor;
18
use phpDocumentor\Descriptor\Tag\ParamDescriptor;
19
use phpDocumentor\Descriptor\Type\UnknownTypeDescriptor;
20
use phpDocumentor\Reflection\Type;
21
use Symfony\Component\Validator\Constraint;
22
use Symfony\Component\Validator\ConstraintValidator;
23
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
24
25
/**
26
 * Validates whether the type for a param tag with a Method or Function is not a known default.
27
 *
28
 * Whenever this validator triggers then it means that someone forgot to replace the pseudo type generated by the IDE
29
 * with the real type for the given parameter.
30
 */
31
class IsParamTypeNotAnIdeDefaultValidator extends ConstraintValidator
32
{
33
    /**
34
     * @see \Symfony\Component\Validator\ConstraintValidatorInterface::validate()
35
     */
36
    public function validate($value, Constraint $constraint)
37
    {
38
        if (! $value instanceof MethodDescriptor && ! $value instanceof FunctionDescriptor) {
39
            throw new ConstraintDefinitionException(
40
                'The Functions\IsParamTypeNotAnIdeDefault validator may only be used on function or method objects'
41
            );
42
        }
43
44
        $params = $value->getParam();
0 ignored issues
show
Bug introduced by
The method getParam does only exist in phpDocumentor\Descriptor\MethodDescriptor, but not in phpDocumentor\Descriptor\FunctionDescriptor.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
45
46
        foreach ($params as $param) {
47
            if (! $param instanceof ParamDescriptor) {
48
                continue;
49
            }
50
51
            /** @var UnknownTypeDescriptor[]|Collection $types */
52
            $types = $param->getTypes();
53
            if (! $this->isTypeAnIdeDefault($types)) {
0 ignored issues
show
Documentation introduced by
$types is of type array<integer,object<php...\Descriptor\Collection>, but the function expects a null|object<phpDocumentor\Reflection\Type>.

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...
54
                continue;
55
            }
56
57
            $this->context->addViolationAt(
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Component\Valida...rface::addViolationAt() has been deprecated with message: since version 2.5, to be removed in 3.0. Use {@link Context\ExecutionContextInterface::buildViolation()} instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
58
                'params',
59
                $constraint->message,
60
                array($param->getVariableName(), $value->getFullyQualifiedStructuralElementName()),
61
                null,
62
                null,
63
                $constraint->code
64
            );
65
        }
66
    }
67
68
    /**
69
     * Verifies if this type is a possible IDE default.
70
     *
71
     * @param UnknownTypeDescriptor|TypeInterface $type
72
     *
73
     * @return bool
74
     */
75
    private function isTypeAnIdeDefault(Type $type = null)
76
    {
77
        return (string)$type === '\\type' || (string)$type === '\\unknown';
78
    }
79
}
80