ChangeGroupsCommand   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 46
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 7 1
A execute() 0 29 4
1
<?php
2
3
namespace SilverLeague\Console\Command\Member;
4
5
use SilverStripe\Security\Group;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use Symfony\Component\Console\Question\ChoiceQuestion;
10
11
/**
12
 * Change a member's assigned groups
13
 *
14
 * @package silverstripe-console
15
 * @author  Robbie Averill <[email protected]>
16
 */
17
class ChangeGroupsCommand extends AbstractMemberCommand
18
{
19
    /**
20
     * {@inheritDoc}
21
     */
22
    protected function configure()
23
    {
24 2
        $this
25
            ->setName('member:change-groups')
26
            ->setDescription("Change a member's groups")
27 2
            ->addArgument('email', InputArgument::OPTIONAL, 'Email address');
28 2
    }
29 2
30 2
    /**
31
     * {@inheritDoc}
32
     */
33
    protected function execute(InputInterface $input, OutputInterface $output)
34
    {
35 2
        $member = $this->getMember($input, $output);
36
        if (!$member) {
37 2
            return;
38 2
        }
39 2
40 1
        if ($member->Groups()->count()) {
41 1
            $output->writeln(
42
                'Member <info>' . $member->Email . '</info> is already in the following groups (will be overwritten):'
43
            );
44 1
            $output->writeln('   ' . implode(', ', $member->Groups()->column('Code')));
45
            $output->writeln('');
46
        }
47
48
        $allGroups = Group::get()->column('Code');
49
        $question = new ChoiceQuestion('Select the groups to add this Member to', $allGroups);
50
        $question->setMultiselect(true);
51
52 1
        $newGroups = $this->getHelper('question')->ask($input, $output, $question);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Symfony\Component\Console\Helper\HelperInterface as the method ask() does only exist in the following implementations of said interface: Symfony\Component\Console\Helper\QuestionHelper, Symfony\Component\Consol...r\SymfonyQuestionHelper.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
53 1
54 1
        $output->writeln('Adding <info>' . $member->Email . '</info> to groups: ' . implode(', ', $newGroups));
55
        // $member->Groups()->removeAll();
56 1
        foreach ($newGroups as $group) {
57
            $member->addToGroupByCode($group);
58 1
        }
59
60 1
        $output->writeln('<info>Groups updated.</info>');
61 1
    }
62
}
63