Completed
Pull Request — master (#14)
by Robbie
01:54
created

ExtensionsCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
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
        $headers = ['Class name', 'Added DB fields'];
45
        if ($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...
46
            $headers[] = 'Updates CMS fields';
47
        }
48
49
        $tableRows = [];
50
        foreach ($extensions as $extensionClass) {
51
            $row = [
52
                $extensionClass,
53
                // Add the number of DB fields that the class adds
54
                $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...
55
            ];
56
57
            if ($isCmsClass) {
58
                // Add whether or not the extension updates CMS fields
59
                $row[] = method_exists(singleton($extensionClass), 'updateCMSFields') ? 'Yes' : 'No';
60
            }
61
62
            $tableRows[] = $row;
63
        }
64
65
        $output->writeln('<info>Extensions for ' . $object . ':</info>');
66
67
        $table = new Table($output);
68
        $table
69
            ->setHeaders($headers)
70
            ->setRows($tableRows)
71
            ->render();
72
73
        // $output->writeln('<comment>' . $object . ' resolves to <info>' . $resolvedTo . '</info></comment>');
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
74
    }
75
}
76