Completed
Push — master ( 6bfcaa...57efb7 )
by Vitaly
02:48
created

AnnotationResolver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 1 Features 2
Metric Value
c 2
b 1
f 2
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: root
5
 * Date: 29.07.2016
6
 * Time: 21:38.
7
 */
8
namespace samsonframework\container\resolver;
9
10
use Doctrine\Common\Annotations\Annotation;
11
use Doctrine\Common\Annotations\AnnotationReader;
12
use Doctrine\Common\Annotations\CachedReader;
13
use Doctrine\Common\Cache\FilesystemCache;
14
use samsonframework\container\annotation\Alias;
15
use samsonframework\container\annotation\AutoWire;
16
use samsonframework\container\annotation\Controller;
17
use samsonframework\container\annotation\Inject;
18
use samsonframework\container\annotation\MetadataInterface;
19
use samsonframework\container\annotation\MethodAnnotation;
20
use samsonframework\container\annotation\Scope;
21
use samsonframework\container\annotation\Service;
22
use samsonframework\container\metadata\ClassMetadata;
23
use samsonframework\container\metadata\MethodMetadata;
24
use samsonframework\container\scope\ControllerScope;
25
26
class AnnotationResolver extends Resolver
27
{
28
    /**
29
     * @var CachedReader
30
     */
31
    protected $reader;
32
33
    /**
34
     * AnnotationResolver constructor.
35
     *
36
     * @param string $cachePath Path for storing annotation cache
37
     *
38
     * @throws \InvalidArgumentException
39
     */
40 1
    public function __construct($cachePath)
41
    {
42 1
        $this->reader = new CachedReader(new AnnotationReader(), new FilesystemCache($cachePath));
43 1
    }
44
45
    /**
46
     * {@inheritDoc}
47
     */
48 1
    public function resolve($classData, $identifier = null)
49
    {
50
        /** @var \ReflectionClass $classData */
51
52
        /** @var MetadataInterface[] $classAnnotations Read class annotations */
53 1
        $classAnnotations = $this->reader->getClassAnnotations($classData);
54
55 1
        if ($classAnnotations) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $classAnnotations of type samsonframework\containe...ion\MetadataInterface[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
56 1
            $metadata = new ClassMetadata();
57 1
            $metadata->className = $classData->getName();
58 1
            $metadata->internalId = $identifier ?: uniqid('container', true);
59
60 1
            foreach ($classAnnotations as $annotation) {
61 1
                if (class_implements($annotation, MetadataInterface::class)) {
62 1
                    $annotation->toMetadata($metadata, $classData->getName());
0 ignored issues
show
Unused Code introduced by
The call to MetadataInterface::toMetadata() has too many arguments starting with $classData->getName().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
63
                }
64
65 1
                if ($annotation instanceof Inject) {
66
                    $argumentList = $annotation->list['value'];
67
                    foreach ($argumentList as $name => $serviceName) {
68
                        $arg = ['service' => $serviceName];
69
                        if (is_string($name)) {
70
                            $metadata->args[$name] = $arg;
71
                        } else {
72 1
                            $metadata->args[] = $arg;
73
                        }
74
                    }
75
                }
76
            }
77
78 1
            if (!$metadata->name) {
79 1
                $metadata->name = $metadata->internalId;
80
            }
81
        }
82
83
        /** @var \ReflectionMethod $method */
84 1
        foreach ($classData->getMethods() as $method) {
85
            $methodAnnotations = $this->reader->getMethodAnnotations($method);
86
            $methodMetadata = new MethodMetadata();
87
            $methodMetadata->name = $method->getName();
88
            $methodMetadata->modifiers = $method->getModifiers();
89
            $methodMetadata->parameters = $method->getParameters();
90
91
            /** @var MethodAnnotation $methodAnnotation */
92
            foreach ($methodAnnotations as $methodAnnotation) {
93
                $methodMetadata->options[$methodAnnotation->getMethodAlias()] = $methodAnnotation->convertToMetadata();
94
            }
95
            $metadata->methodsMetadata[$method->getName()] = $methodMetadata;
0 ignored issues
show
Bug introduced by
The variable $metadata does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
96
        }
97
98 1
        return $metadata;
99
    }
100
}
101