ChildrenCommand::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace SilverLeague\Console\Command\Object;
4
5
use SilverLeague\Console\Command\SilverStripeCommand;
6
use SilverLeague\Console\Framework\Utility\ObjectUtilities;
7
use SilverStripe\Core\ClassInfo;
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 child classes of a given class name
15
 *
16
 * @package silverstripe-console
17
 * @author  Robbie Averill <[email protected]>
18
 */
19
class ChildrenCommand extends SilverStripeCommand
20
{
21
    use ObjectUtilities;
22
23 3
    /**
24
     * {@inheritDoc}
25
     */
26 3
    protected function configure()
27 3
    {
28 3
        $this
29 3
            ->setName('object:children')
30
            ->setDescription('List all child classes of a given class, e.g. "Page"')
31
            ->addArgument('object', InputArgument::REQUIRED, 'The class to find children for');
32
    }
33
34 1
    /**
35
     * {@inheritDoc}
36 1
     */
37 1
    protected function execute(InputInterface $input, OutputInterface $output)
38
    {
39 1
        $object = $input->getArgument('object');
40 1
        $classes = (array) ClassInfo::subclassesFor($object);
41
        // Remove the class itself
42
        array_shift($classes);
43
        if (!$classes) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $classes of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
44 1
            $output->writeln('There are no child classes for ' . $object);
45 1
            return;
46 1
        }
47 1
        sort($classes);
48
        $rows = array_map(function ($class) {
49 1
            return [$class, $this->getModuleName($class)];
50 1
        }, $classes);
51
52 1
        $output->writeln('<info>Child classes for ' . $object . ':</info>');
53 1
        $table = new Table($output);
54 1
        $table
55 1
            ->setHeaders(['Class name', 'Module'])
56
            ->setRows($rows)
57
            ->render();
58
    }
59
}
60