Completed
Pull Request — master (#4)
by Mariano
04:20
created

ParserGenerator::getPropertyParserBuilder()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
ccs 8
cts 8
cp 1
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * This file is part of php-simple-request.
4
 *
5
 * php-simple-request is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU Lesser General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * php-simple-request is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with php-simple-request.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
namespace Mcustiel\SimpleRequest;
19
20
use Doctrine\Common\Annotations\AnnotationReader;
21
use Mcustiel\SimpleRequest\Strategies\AnnotationParserFactory;
22
use Mcustiel\SimpleRequest\Strategies\PropertyParserBuilder;
23
use Mcustiel\SimpleRequest\Interfaces\ReflectionService;
24
use Mcustiel\SimpleRequest\Interfaces\AnnotationService;
25
26
class ParserGenerator
27
{
28
    /**
29
     * @var \Mcustiel\SimpleRequest\Interfaces\AnnotationService
30
     */
31
    private $annotationReader;
32
    /**
33
     * @var \Mcustiel\SimpleRequest\Strategies\AnnotationParserFactory
34
     */
35
    private $annotationParserFactory;
36
37
    /**
38
     * @var \Mcustiel\SimpleRequest\Interfaces\ReflectionService
39
     */
40
    private $reflectionService;
41
42
    /**
43
     * @param \Mcustiel\SimpleRequest\Interfaces\AnnotationService       $annotationReader
44
     * @param \Mcustiel\SimpleRequest\Strategies\AnnotationParserFactory $annotationParserFactory
45
     * @param \Mcustiel\SimpleRequest\Interfaces\ReflectionService       $reflectionService
46
     */
47 97
    public function __construct(
48
        AnnotationService $annotationReader,
49
        AnnotationParserFactory $annotationParserFactory,
50
        ReflectionService $reflectionService
51
    ) {
52 97
        $this->annotationReader = $annotationReader;
53 97
        $this->annotationParserFactory = $annotationParserFactory;
54 97
        $this->reflectionService = $reflectionService;
55 97
    }
56
57
    /**
58
     * Populates the parser object with the properties parser and the class object.
59
     *
60
     * @param string         $className
61
     * @param RequestParser  $parserObject
62
     * @param RequestBuilder $requestBuilder
63
     *
64
     * @return RequestParser
65
     */
66 93
    public function populateRequestParser(
67
        $className,
68
        RequestParser $parserObject,
69
        RequestBuilder $requestBuilder
70
    ) {
71 93
        $parserObject->setRequestObject(new $className);
72 93
        foreach ($this->reflectionService->getClassProperties($className) as $property) {
73 93
            $parserObject->addPropertyParser(
74 93
                $this->getPropertyParserBuilder($property)->build($requestBuilder)
75 93
            );
76 93
        }
77 93
        return $parserObject;
78
    }
79
80 93
    private function getPropertyParserBuilder(\ReflectionProperty $property)
81
    {
82 93
        $propertyParserBuilder = new PropertyParserBuilder($property->getName());
83 93
        foreach ($this->annotationReader->getAnnotationsFromProperty($property) as $propertyAnnotation) {
84 93
            $this->annotationParserFactory
85 93
                ->getAnnotationParserFor($propertyAnnotation)
0 ignored issues
show
Documentation introduced by
$propertyAnnotation is of type object<Doctrine\Common\Annotations\Annotation>, but the function expects a object<Mcustiel\SimpleRe...tion\RequestAnnotation>.

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...
86 93
                ->execute($propertyAnnotation, $propertyParserBuilder);
0 ignored issues
show
Documentation introduced by
$propertyAnnotation is of type object<Doctrine\Common\Annotations\Annotation>, but the function expects a object<Mcustiel\SimpleRe...tion\RequestAnnotation>.

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...
87 93
        }
88 93
        return $propertyParserBuilder;
89
    }
90
}
91