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 ( 3684b5...a74bc8 )
by Sergey
03:58
created

LazyMetadataFactory   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 17
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 92
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
C getMetadataFor() 0 39 11
B hasMetadataFor() 0 14 5
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\Factory\MetadataFactoryInterface;
16
use Reva2\JsonApi\Contracts\Decoders\Mapping\GenericMetadataInterface;
17
use Reva2\JsonApi\Contracts\Decoders\Mapping\Loader\LoaderInterface;
18
use Reva2\JsonApi\Contracts\Decoders\Mapping\ResourceMetadataInterface;
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
    public function __construct(LoaderInterface $loader, CacheInterface $cache = null)
52
    {
53
        $this->loader = $loader;
54
        $this->cache = $cache;
55
    }
56
57
    /**
58
     * @inheritdoc
59
     */
60
    public function getMetadataFor($value)
61
    {
62
        if (!is_object($value) && !is_string($value)) {
63
            throw new \InvalidArgumentException(sprintf(
64
                "Cannot create metadata for non-objects. Got: %s",
65
                gettype($value)
66
            ));
67
        }
68
        
69
        $class = ltrim(((is_object($value)) ? get_class($value) : $value), '\\');
70
        
71
        if (array_key_exists($value, $this->loadedClasses)) {
72
            return $this->loadedClasses[$class];
73
        }
74
        
75
        if ((null !== $this->cache) && (false !== ($this->loadedClasses[$class] = $this->cache->read($class)))) {
76
            return $this->loadedClasses[$class];
77
        }
78
        
79
        if (class_exists($class)) {
80
            throw new \RuntimeException(sprintf(
81
                "The class or interface '%s' doesn't exist",
82
                $class
83
            ));
84
        }
85
        
86
        $reflection = new \ReflectionClass($class);
87
        
88
        $metadata = $this->loader->loadClassMetadata($reflection);
89
        if (($metadata instanceof ResourceMetadataInterface) && (null !== ($parent = $reflection->getParentClass()))) {
90
            $metadata->mergeMetadata($this->getMetadataFor($parent->getName()));
91
        }
92
93
        if (null !== $this->cache) {
94
            $this->cache->write($metadata);
0 ignored issues
show
Documentation introduced by
$metadata is of type boolean, but the function expects a object<Reva2\JsonApi\Con...nericMetadataInterface>.

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...
95
        }
96
97
        return $this->loadedClasses[$class] = $metadata;
98
    }
99
100
    /**
101
     * @inheritdoc
102
     */
103
    public function hasMetadataFor($value)
104
    {
105
        if (!is_object($value) && !is_string($value)) {
106
            return false;
107
        }
108
109
        $class = ltrim(((is_object($value)) ? get_class($value) : $value), '\\');
110
111
        if (class_exists($class)) {
112
            return true;
113
        }
114
115
        return false;
116
    }
117
}