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.

Entity   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 8
dl 0
loc 99
c 0
b 0
f 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 10 2
A format() 0 10 2
A find() 0 18 4
A fromModule() 0 12 4
A getEntityClass() 0 4 1
1
<?php
2
3
namespace Saltwater\RedBean\Provider;
4
5
use Saltwater\Server as S;
6
use Saltwater\Utils as U;
7
use Saltwater\Salt\Provider;
8
use Saltwater\Salt\Module;
9
10
class Entity extends Provider
11
{
12
    /**
13
     * @param string $name
14
     *
15
     * @return string
16
     */
17
    public function get($name)
18
    {
19
        $entity = $this->format($name);
20
21
        if (!empty($entity)) {
22
            return $entity;
23
        }
24
25
        return $name;
26
    }
27
28
    /**
29
     * Format a full class name from an entity name
30
     *
31
     * @param string $name
32
     *
33
     * @return string|null
34
     */
35
    private function format($name)
36
    {
37
        $bit = S::$n->registry->bit('entity.' . $name);
38
39
        if (!$bit) {
40
            return null;
41
        }
42
43
        return $this->find($name, $bit);
44
    }
45
46
    /**
47
     * Search for an entity implementation in the module stack
48
     *
49
     * @param string $name
50
     * @param int    $bit
51
     *
52
     * @return bool|null|string
53
     */
54
    private function find($name, $bit)
55
    {
56
        // Try to find the entity within the module that is calling us
57
        if ($class = $this->fromModule(self::$caller, $name, $bit)) {
58
            return $class;
59
        }
60
61
        // Otherwise try to find it by going up the module stack
62
        $modules = S::$n->modules->finder->modulesBySalt('entity.' . $name);
63
64
        foreach ($modules as $module) {
0 ignored issues
show
Bug introduced by
The expression $modules of type array<integer,object<Sal...y<integer,string>|false is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
65
            if ($class = $this->fromModule($module, $name, $bit)) {
66
                return $class;
67
            }
68
        }
69
70
        return null;
71
    }
72
73
    /**
74
     * Check whether a module has an entity declared, if so, either load the
75
     * entity class, or fall back to the basic salt
76
     *
77
     * @param Module|string $module
78
     * @param string        $name
79
     * @param int           $bit
80
     *
81
     * @return bool|string
82
     */
83
    private function fromModule($module, $name, $bit)
84
    {
85
        $module = S::$n->modules->get($module);
0 ignored issues
show
Bug introduced by
It seems like $module defined by \Saltwater\Server::$n->modules->get($module) on line 85 can also be of type object<Saltwater\Salt\Module>; however, Saltwater\Water\ModuleStack::get() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
86
87
        if (!is_object($module) || !$module->has($bit)) {
88
            return false;
89
        }
90
91
        $class = $this->getEntityClass($name, $module);
92
93
        return class_exists($class) ? $class : 'Saltwater\RedBean\Salt\Entity';
94
    }
95
96
    /**
97
     * Get Entity class name from a name plus a module
98
     *
99
     * @param string $name
100
     * @param Module $module
101
     *
102
     * @return string
103
     */
104
    private function getEntityClass($name, $module)
105
    {
106
        return U::className($module::getNamespace(), 'entity', $name);
107
    }
108
}
109