Completed
Push — develop ( 71fd61...bbac44 )
by Jaap
06:03 queued 02:27
created

Functions/IsParamTypeNotAnIdeDefaultValidator.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
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)) {
54
                continue;
55
            }
56
57
            $this->context->addViolationAt(
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