Completed
Push — master ( 5d31f9...7b1630 )
by Sander
21s queued 10s
created

AdminBundle/Command/CreateGroupCommand.php (1 issue)

call_checks.maybe_mismatching_type_passed_with_def

Bug Minor

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\AdminBundle\Command;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Kunstmaan\AdminBundle\Entity\Group;
7
use Kunstmaan\AdminBundle\Entity\Role;
8
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Input\InputOption;
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
/**
15
 * Symfony CLI command to create a group using bin/console kuma:group:create <name_of_the_group>
16
 *
17
 * @final since 5.1
18
 * NEXT_MAJOR extend from `Command` and remove `$this->getContainer` usages
19
 */
20
class CreateGroupCommand extends ContainerAwareCommand
21
{
22
    /**
23
     * @var EntityManagerInterface
24
     */
25
    private $em;
26
27
    /**
28
     * @param EntityManagerInterface|null $em
29
     */
30
    public function __construct(/* EntityManagerInterface */ $em = null)
31
    {
32
        parent::__construct();
33
34
        if (!$em instanceof EntityManagerInterface) {
35
            @trigger_error(sprintf('Passing a command name as the first argument of "%s" is deprecated since version symfony 3.4 and will be removed in symfony 4.0. If the command was registered by convention, make it a service instead. ', __METHOD__), E_USER_DEPRECATED);
36
37
            $this->setName(null === $em ? 'kuma:group:create' : $em);
38
39
            return;
40
        }
41
42
        $this->em = $em;
43
    }
44
45
    /**
46
     * Configures the current command
47
     */
48
    protected function configure()
49
    {
50
        parent::configure();
51
52
        $this->setName('kuma:group:create')
53
            ->setDescription('Create a user group.')
54
            ->setDefinition(array(
55
                new InputArgument('group', InputArgument::REQUIRED, 'The group'),
56
                new InputOption('role', null, InputOption::VALUE_OPTIONAL, 'Role(s) (comma separated list if you want to specifiy multiple roles)'),
57
            ))
58
            ->setHelp(<<<'EOT'
59
The <info>kuma:group:create</info> command creates a group:
60
61
  <info>php bin/console kuma:group:create Administrators</info>
62
63
You can specify a list of roles to attach to this group by specifying the
64
optional --roles parameter, providing a comma separated list of roles :
65
66
  <info>php bin/console kuma:group:create --role=admin,guest Administrators</info>
67
68
<comment>Note:</comment> The ROLE_ prefix will be added if you don't provide it AND you must make
69
sure the roles already exist!
70
71
EOT
72
            );
73
    }
74
75
    /**
76
     * Executes the current command
77
     *
78
     * @param InputInterface  $input  The input
79
     * @param OutputInterface $output The output
80
     *
81
     * @return int
82
     */
83
    protected function execute(InputInterface $input, OutputInterface $output)
84
    {
85
        if (null === $this->em) {
86
            $this->em = $this->getContainer()->get('doctrine.orm.entity_manager');
87
        }
88
89
        $groupName = $input->getArgument('group');
90
        $roleNames = $input->getOption('role');
91
        $group = new Group($groupName);
0 ignored issues
show
It seems like $groupName defined by $input->getArgument('group') on line 89 can also be of type array<integer,string> or null; however, Kunstmaan\AdminBundle\Entity\Group::__construct() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
92
93
        if (!empty($roleNames)) {
94
            // Roles were provided, so attach them to the group
95
            $roleNames = explode(',', strtoupper($roleNames));
96
            foreach ($roleNames as $roleName) {
97
                if ('ROLE_' != substr($roleName, 0, 5)) {
98
                    $roleName = 'ROLE_' . $roleName;
99
                }
100
                /* @var Role $role */
101
                $role = $this->em->getRepository('KunstmaanAdminBundle:Role')->findOneBy(array('role' => $roleName));
102
                $group->addRole($role);
103
            }
104
        }
105
        $this->em->persist($group);
106
        $this->em->flush();
107
108
        $output->writeln(sprintf('Created group <comment>%s</comment>', $groupName));
109
    }
110
}
111