Completed
Push — master ( 453cff...c1eed9 )
by Martijn van
02:33
created

AbstractListCommand::getTableData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 19
rs 9.4285
cc 2
eloc 11
nc 2
nop 0
1
<?php
2
3
4
abstract class AbstractListCommand extends SilverstripeCommand
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
5
{
6
    public function fire()
7
    {
8
        $this->table($this->getTableHeaders(), $this->getTableData());
9
    }
10
11
    abstract function getClassName();
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
12
13
    /**
14
     * @return array
15
     */
16
    protected function getTableData()
17
    {
18
        $classes = ClassInfo::subclassesFor($this->getClassName());
19
20
        unset($classes[$this->getClassName()]);
21
22
        foreach ($classes as $key => $class) {
0 ignored issues
show
Bug introduced by
The expression $classes of type null|array 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...
23
            $classes[$class] = [
24
                $class,
25
                implode("\n", (array) $this->getExtensions($class)),
26
                implode(" => ", (array) $this->getParentClasses($class)),
27
                $this->getModule($class),
28
            ];
29
        }
30
31
        ksort($classes);
32
33
        return $classes;
34
    }
35
36
37
    /**
38
     * @return array
39
     */
40
    protected function getTableHeaders()
41
    {
42
        return ['ClassName', 'Extensions', 'Extends', 'Module'];
43
    }
44
45
    /**
46
     * @param $className
47
     * @return array
48
     */
49
    protected function getExtensions($className)
50
    {
51
        return (array)Config::inst()->get($className, 'extensions', Config::UNINHERITED);
52
    }
53
54
    /**
55
     * @param string $className
56
     * @return string
57
     */
58
    protected function getModule($className)
59
    {
60
        $reflection = new \ReflectionClass($className);
61
        $file = $reflection->getFileName();
62
63
        $path  = str_replace([BASE_PATH.'/'], '', $file);
64
        $parts = explode(DIRECTORY_SEPARATOR, $path);
65
66
        return (string)array_shift($parts);
67
    }
68
69
    /**
70
     * @param string $className
71
     * @return array
72
     */
73
    protected function getParentClasses($className)
74
    {
75
        return ClassInfo::ancestry($className);
76
    }
77
}
78