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

Functions/AreAllArgumentsValidValidator.php (2 issues)

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-2013 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\MethodDescriptor;
16
use phpDocumentor\Descriptor\FunctionDescriptor;
17
use phpDocumentor\Plugin\Core\Descriptor\Validator\Constraints\Functions\DoesArgumentNameMatchParam;
18
use phpDocumentor\Plugin\Core\Descriptor\Validator\Constraints\Functions\DoesArgumentNameMatchParamValidator;
19
use phpDocumentor\Plugin\Core\Descriptor\Validator\ValidationValueObject;
20
use Symfony\Component\Validator\Constraint;
21
use Symfony\Component\Validator\ConstraintValidator;
22
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
23
24
/**
25
 *
26
 */
27
class AreAllArgumentsValidValidator extends ConstraintValidator
28
{
29
    /** @var Constraint $constraint */
30
    protected $constraint;
31
32
    /**
33
     * Value to validate against.
34
     *
35
     * @var Collection|null
36
     */
37
    protected $validationValue = null;
38
39
    /**
40
     * @see \Symfony\Component\Validator\ConstraintValidatorInterface::validate()
41
     *
42
     * @throws ConstraintDefinitionException
43
     */
44
    public function validate($value, Constraint $constraint)
45
    {
46
        if (! $value instanceof MethodDescriptor
47
            && ! $value instanceof FunctionDescriptor
48
        ) {
49
            throw new ConstraintDefinitionException(
50
                'The Functions\AreAllArgumentsValid validator may only be used on function or method objects'
51
            );
52
        }
53
54
        $this->constraint = $constraint;
55
56
        $this->initValueObject($value);
57
58
        $violation = $this->processArgumentValidation();
59
        if ($violation) {
60
            return $violation;
61
        }
62
63
        $this->checkParamsExists();
64
    }
65
66
    protected function initValueObject($value)
67
    {
68
        $this->validationValue = new ValidationValueObject();
69
70
        $this->validationValue->fqsen = $value->getFullyQualifiedStructuralElementName();
71
        $this->validationValue->arguments = $value->getArguments();
72
        $this->validationValue->parameters = $value->getParam();
73
        $this->validationValue->name = $value->getName();
74
    }
75
76
    /**
77
     * @return null
78
     */
79
    protected function processArgumentValidation()
80
    {
81
        $arguments = $this->validationValue->arguments->getAll();
82
83
        foreach (array_values($arguments) as $key => $argument) {
84
            $this->validationValue->key = $key;
85
            $this->validationValue->argument = $argument;
86
            $this->validationValue->index = $key;
87
            $this->validationValue->parameter = $this->validationValue->parameters[$key] ?? null;
88
89
            if ($this->checkArgumentInDocBlock()) {
90
                continue;
91
            }
92
93
            $violation = $this->checkArgumentNameMatchParam();
94
            if ($violation) {
95
                return $violation;
96
            }
97
98
            $violation = $this->checkArgumentTypehintMatchParam();
99
            if ($violation) {
100
                return $violation;
101
            }
102
        }
103
104
        return null;
105
    }
106
107
    /**
108
     * Check if argument is inside docblock.
109
     */
110
    protected function checkArgumentInDocBlock()
111
    {
112
        $validator = new IsArgumentInDocBlockValidator();
113
        $validator->initialize($this->context);
114
115
        return $validator->validate($this->validationValue, new IsArgumentInDocBlock);
116
    }
117
118
    /**
119
     * Check if argument matches parameter.
120
     */
121
    protected function checkArgumentNameMatchParam()
122
    {
123
        $validator  = new DoesArgumentNameMatchParamValidator;
124
        $validator->initialize($this->context);
125
126
        return $validator->validate($this->validationValue, new DoesArgumentNameMatchParam);
127
    }
128
129
    /**
130
     * Check if argument typehint matches parameter.
131
     */
132
    protected function checkArgumentTypehintMatchParam()
133
    {
134
        $validator  = new DoesArgumentTypehintMatchParamValidator;
135
        $validator->initialize($this->context);
136
137
        return $validator->validate($this->validationValue, new DoesArgumentTypehintMatchParam);
138
    }
139
140
    /**
141
     * Check if parameter exists for argument.
142
     *
143
     * @param Collection $params
0 ignored issues
show
There is no parameter named $params. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
144
     * @param Collection $arguments
0 ignored issues
show
There is no parameter named $arguments. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
145
     */
146
    protected function checkParamsExists()
147
    {
148
        $validator  = new DoesParamsExistsValidator();
149
        $validator->initialize($this->context);
150
151
        return $validator->validate($this->validationValue, new DoesParamsExists);
152
    }
153
}
154