Completed
Push — master ( 01b890...0ab6a2 )
by Robbie
01:15
created

ChildrenCommand::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 22
ccs 15
cts 15
cp 1
rs 9.2
c 1
b 0
f 0
cc 2
eloc 17
nc 2
nop 2
crap 2
1
<?php
2
3
namespace SilverLeague\Console\Command\Object;
4
5
use SilverLeague\Console\Command\SilverStripeCommand;
6
use SilverStripe\Core\ClassInfo;
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 child classes of a given class name
14
 *
15
 * @package silverstripe-console
16
 * @author  Robbie Averill <[email protected]>
17
 */
18
class ChildrenCommand extends SilverStripeCommand
19
{
20
    /**
21
     * {@inheritDoc}
22
     */
23 1
    protected function configure()
24
    {
25
        $this
26 1
            ->setName('object:children')
27 1
            ->setDescription('List all child classes of a given class, e.g. "Page"')
28 1
            ->addArgument('object', InputArgument::REQUIRED, 'The class to find children for');
29 1
    }
30
31
    /**
32
     * {@inheritDoc}
33
     */
34 1
    protected function execute(InputInterface $input, OutputInterface $output)
35
    {
36 1
        $object = $input->getArgument('object');
37 1
        $classes = (array) ClassInfo::subclassesFor($object);
38
        // Remove the class itself
39 1
        array_shift($classes);
40 1
        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...
41
            $output->writeln('There are no child classes for ' . $object);
42
            return;
43
        }
44 1
        sort($classes);
45 1
        $rows = array_map(function ($class) {
46 1
            return [$class];
47 1
        }, $classes);
48
49 1
        $output->writeln('<info>Child classes for ' . $object . ':</info>');
50 1
        $table = new Table($output);
51
        $table
52 1
            ->setHeaders(['Class name'])
53 1
            ->setRows($rows)
54 1
            ->render();
55 1
    }
56
}
57