Completed
Pull Request — master (#14)
by Robbie
02:05
created

ExtensionsCommand::getRows()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 19
rs 9.2
cc 4
eloc 10
nc 4
nop 2
1
<?php
2
3
namespace SilverLeague\Console\Command\Object;
4
5
use SilverLeague\Console\Command\SilverStripeCommand;
6
use SilverStripe\Core\Config\Config;
7
use Symfony\Component\Console\Helper\Table;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
/**
13
 * List all extensions of a given Object, e.g. "Page"
14
 *
15
 * @package silverstripe-console
16
 * @author  Robbie Averill <[email protected]>
17
 */
18
class ExtensionsCommand extends SilverStripeCommand
19
{
20
    /**
21
     * {@inheritDoc}
22
     */
23
    protected function configure()
24
    {
25
        $this
26
            ->setName('object:extensions')
27
            ->setDescription('List all Extensions of a given Object, e.g. "Page"')
28
            ->addArgument('object', InputArgument::REQUIRED, 'The Object to look up');
29
    }
30
31
    /**
32
     * {@inheritDoc}
33
     */
34
    protected function execute(InputInterface $input, OutputInterface $output)
35
    {
36
        $object = $input->getArgument('object');
37
        $extensions = \SilverStripe\Core\Object::get_extensions($object);
38
        if (!$extensions) {
39
            $output->writeln('There are no Extensions registered for ' . $object);
40
            return;
41
        }
42
        sort($extensions);
43
44
        $isCmsClass = singleton($object) instanceof \SilverStripe\CMS\Model\SiteTree;
0 ignored issues
show
Bug introduced by
The class SilverStripe\CMS\Model\SiteTree does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
45
46
        $output->writeln('<info>Extensions for ' . $object . ':</info>');
47
        $table = new Table($output);
48
        $table
49
            ->setHeaders($this->getHeaders($isCmsClass))
50
            ->setRows($this->getRows($isCmsClass, $extensions))
51
            ->render();
52
    }
53
54
    /**
55
     * Return the header cells for the output table. CMS classes have an extra column.
56
     *
57
     * @param  bool $isCmsClass
58
     * @return array
59
     */
60
    protected function getHeaders($isCmsClass)
61
    {
62
        $headers = ['Class name', 'Added DB fields'];
63
        if ($isCmsClass) {
64
            $headers[] = 'Updates CMS fields';
65
        }
66
67
        return $headers;
68
    }
69
70
    /**
71
     * Return the rows for the output table containing extension statistics. CMS classes have an extra column.
72
     *
73
     * @param  bool $isCmsClass
74
     * @return array
75
     */
76
    protected function getRows($isCmsClass, $extensions)
77
    {
78
        $tableRows = [];
79
        foreach ($extensions as $extensionClass) {
80
            $row = [
81
                $extensionClass,
82
                // Add the number of DB fields that the class adds
83
                $row[] = count((array) Config::inst()->get($extensionClass, 'db', Config::UNINHERITED))
0 ignored issues
show
Coding Style Comprehensibility introduced by
$row was never initialized. Although not strictly required by PHP, it is generally a good practice to add $row = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
84
            ];
85
86
            if ($isCmsClass) {
87
                // Add whether or not the extension updates CMS fields
88
                $row[] = method_exists(singleton($extensionClass), 'updateCMSFields') ? 'Yes' : 'No';
89
            }
90
91
            $tableRows[] = $row;
92
        }
93
        return $tableRows;
94
    }
95
}
96