Completed
Push — master ( 8110dc...01b890 )
by Robbie
01:10
created

ExtensionsCommand   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 10
c 2
b 0
f 0
lcom 0
cbo 5
dl 0
loc 79
ccs 32
cts 32
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 7 1
A execute() 0 20 3
A getHeaders() 0 9 2
A getRows() 0 19 4
1
<?php
2
3
namespace SilverLeague\Console\Command\Object;
4
5
use SilverLeague\Console\Command\SilverStripeCommand;
6
use SilverStripe\Core\Config\Config;
7
use SilverStripe\Core\Object;
8
use Symfony\Component\Console\Helper\Table;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
/**
14
 * List all extensions of a given Object, e.g. "Page"
15
 *
16
 * @package silverstripe-console
17
 * @author  Robbie Averill <[email protected]>
18
 */
19
class ExtensionsCommand extends SilverStripeCommand
20
{
21
    /**
22
     * {@inheritDoc}
23
     */
24 1
    protected function configure()
25
    {
26
        $this
27 1
            ->setName('object:extensions')
28 1
            ->setDescription('List all Extensions of a given Object, e.g. "Page"')
29 1
            ->addArgument('object', InputArgument::REQUIRED, 'The Object to find Extensions for');
30 1
    }
31
32
    /**
33
     * {@inheritDoc}
34
     */
35 1
    protected function execute(InputInterface $input, OutputInterface $output)
36
    {
37 1
        $object = $input->getArgument('object');
38 1
        $extensions = Object::get_extensions($object);
39 1
        if (!$extensions) {
40
            $output->writeln('There are no Extensions registered for ' . $object);
41
            return;
42
        }
43 1
        sort($extensions);
44
45 1
        $isCmsClass = (class_exists('SilverStripe\\CMS\\Model\\SiteTree')
46 1
            && 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...
47
48 1
        $output->writeln('<info>Extensions for ' . $object . ':</info>');
49 1
        $table = new Table($output);
50
        $table
51 1
            ->setHeaders($this->getHeaders($isCmsClass))
52 1
            ->setRows($this->getRows($isCmsClass, $extensions))
53 1
            ->render();
54 1
    }
55
56
    /**
57
     * Return the header cells for the output table. CMS classes have an extra column.
58
     *
59
     * @param  bool $isCmsClass
60
     * @return string[]
61
     */
62 2
    public function getHeaders($isCmsClass)
63
    {
64 2
        $headers = ['Class name', 'Added DB fields'];
65 2
        if ($isCmsClass) {
66 1
            $headers[] = 'Updates CMS fields';
67
        }
68
69 2
        return $headers;
70
    }
71
72
    /**
73
     * Return the rows for the output table containing extension statistics. CMS classes have an extra column.
74
     *
75
     * @param  bool $isCmsClass
76
     * @return array[]
77
     */
78 2
    public function getRows($isCmsClass, $extensions)
79
    {
80 2
        $tableRows = [];
81 2
        foreach ($extensions as $extensionClass) {
82
            $row = [
83 2
                $extensionClass,
84
                // Add the number of DB fields that the class adds
85 2
                count((array) Config::inst()->get($extensionClass, 'db', Config::UNINHERITED))
86
            ];
87
88 2
            if ($isCmsClass) {
89
                // Add whether or not the extension updates CMS fields
90 1
                $row[] = method_exists(singleton($extensionClass), 'updateCMSFields') ? 'Yes' : 'No';
91
            }
92
93 2
            $tableRows[] = $row;
94
        }
95 2
        return $tableRows;
96
    }
97
}
98