GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( b5d278...796de0 )
by Sergey
17s queued 11s
created

LazyMetadataFactory::getMetadataFor()   B

Complexity

Conditions 11
Paths 8

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 11.013

Importance

Changes 0
Metric Value
dl 0
loc 39
c 0
b 0
f 0
ccs 20
cts 21
cp 0.9524
rs 7.3166
cc 11
nc 8
nop 1
crap 11.013

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/*
3
 * This file is part of the reva2/jsonapi.
4
 *
5
 * (c) Sergey Revenko <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
12
namespace Reva2\JsonApi\Decoders\Mapping\Factory;
13
14
use Reva2\JsonApi\Contracts\Decoders\Mapping\Cache\CacheInterface;
15
use Reva2\JsonApi\Contracts\Decoders\Mapping\ClassMetadataInterface;
16
use Reva2\JsonApi\Contracts\Decoders\Mapping\Factory\MetadataFactoryInterface;
17
use Reva2\JsonApi\Contracts\Decoders\Mapping\GenericMetadataInterface;
18
use Reva2\JsonApi\Contracts\Decoders\Mapping\Loader\LoaderInterface;
19
20
/**
21
 * JSON API metadata factory
22
 *
23
 * @package Reva2\JsonApi\Decoders\Mapping\Factory
24
 * @author Sergey Revenko <[email protected]>
25
 */
26
class LazyMetadataFactory implements MetadataFactoryInterface
27
{
28
    /**
29
     * @var LoaderInterface
30
     */
31
    protected $loader;
32
33
    /**
34
     * @var CacheInterface
35
     */
36
    protected $cache;
37
38
    /**
39
     * Loaded metadata indexed by class name
40
     *
41
     * @var GenericMetadataInterface[]
42
     */
43
    protected $loadedClasses = [];
44
45
    /**
46
     * Constructor
47
     *
48
     * @param LoaderInterface $loader
49
     * @param CacheInterface|null $cache
50
     */
51 22
    public function __construct(LoaderInterface $loader, CacheInterface $cache = null)
52
    {
53 22
        $this->loader = $loader;
54 22
        $this->cache = $cache;
55 22
    }
56
57
    /**
58
     * @inheritdoc
59
     */
60 11
    public function getMetadataFor($value)
61
    {
62 11
        if (!is_object($value) && !is_string($value)) {
63 1
            throw new \InvalidArgumentException(sprintf(
64 1
                "Cannot create metadata for non-objects. Got: %s",
65 1
                gettype($value)
66
            ));
67
        }
68
        
69 11
        $class = ltrim(((is_object($value)) ? get_class($value) : $value), '\\');
70
        
71 11
        if (array_key_exists($value, $this->loadedClasses)) {
72 6
            return $this->loadedClasses[$class];
73
        }
74
        
75 11
        if ((null !== $this->cache) && (false !== ($this->loadedClasses[$class] = $this->cache->read($class)))) {
76
            return $this->loadedClasses[$class];
77
        }
78
        
79 11
        if (!class_exists($class)) {
80 1
            throw new \RuntimeException(sprintf(
81 1
                "The class '%s' doesn't exist",
82 1
                $class
83
            ));
84
        }
85
        
86 10
        $reflection = new \ReflectionClass($class);
87
        
88 10
        $metadata = $this->loader->loadClassMetadata($reflection);
89 10
        if (($metadata instanceof ClassMetadataInterface) && (false !== ($parent = $reflection->getParentClass()))) {
90 8
            $metadata->mergeMetadata($this->getMetadataFor($parent->getName()));
91
        }
92
93 10
        if (null !== $this->cache) {
94 1
            $this->cache->write($metadata);
95
        }
96
97 10
        return $this->loadedClasses[$class] = $metadata;
98
    }
99
100
    /**
101
     * @inheritdoc
102
     */
103 2
    public function hasMetadataFor($value)
104
    {
105 2
        if (!is_object($value) && !is_string($value)) {
106 1
            return false;
107
        }
108
109 1
        $class = ltrim(((is_object($value)) ? get_class($value) : $value), '\\');
110
111 1
        if (class_exists($class)) {
112 1
            return true;
113
        }
114
115 1
        return false;
116
    }
117
}
118