MigrationsDiffCommand::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 4
cp 0
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Kunstmaan\TranslatorBundle\Command;
4
5
use Doctrine\Bundle\DoctrineBundle\Command\Proxy\DoctrineCommandHelper;
6
use Doctrine\Bundle\MigrationsBundle\Command\DoctrineCommand;
7
use Kunstmaan\TranslatorBundle\Service\Command\DiffCommand;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12 1
if (class_exists(\Doctrine\Migrations\Tools\Console\Command\GenerateCommand::class)) {
13
    /**
14
     * Command for generate migration classes by checking the translation flag value
15
     *
16
     * @final since 5.1
17
     *
18
     * @deprecated This class is deprecated since KunstmaanTranslatorBundle 5.2 and will be removed in 6.0.
19
     */
20
    class MigrationsDiffCommand extends DiffCommand
0 ignored issues
show
Deprecated Code introduced by
The class Kunstmaan\TranslatorBund...ice\Command\DiffCommand has been deprecated with message: This class is deprecated since KunstmaanTranslatorBundle 5.2 and will be removed in 6.0.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
21
    {
22
        protected function configure(): void
23
        {
24
            parent::configure();
25
26
            $this
27
                ->setName('kuma:translator:migrations:diff')
28
                ->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command.')
29
            ;
30
        }
31
32
        public function execute(InputInterface $input, OutputInterface $output): ?int
33
        {
34
            DoctrineCommandHelper::setApplicationEntityManager($this->getApplication(), $input->getOption('em'));
0 ignored issues
show
Documentation introduced by
$this->getApplication() is of type null|object<Symfony\Comp...nt\Console\Application>, but the function expects a object<Symfony\Bundle\Fr...le\Console\Application>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
35
36
            $configuration = $this->getMigrationConfiguration($input, $output);
37
            DoctrineCommand::configureMigrations($this->getApplication()->getKernel()->getContainer(), $configuration);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Console\Application as the method getKernel() does only exist in the following sub-classes of Symfony\Component\Console\Application: Symfony\Bundle\FrameworkBundle\Console\Application. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
38
39
            $exitCode = parent::execute($input, $output);
40
41
            return is_numeric($exitCode) ? (int) $exitCode : 0;
42
        }
43
    }
44
} else {
45
    /**
46
     * Command for generate migration classes by checking the translation flag value
47
     *
48
     * @final since 5.1
49
     *
50
     * @deprecated This class is deprecated since KunstmaanTranslatorBundle 5.2 and will be removed in 6.0.
51
     */
52
    class MigrationsDiffCommand extends DiffCommand
0 ignored issues
show
Comprehensibility Best Practice introduced by
The type Kunstmaan\TranslatorBund...d\MigrationsDiffCommand has been defined more than once; this definition is ignored, only the first definition in this file (L20-43) is considered.

This check looks for classes that have been defined more than once in the same file.

If you can, we would recommend to use standard object-oriented programming techniques. For example, to avoid multiple types, it might make sense to create a common interface, and then multiple, different implementations for that interface.

This also has the side-effect of providing you with better IDE auto-completion, static analysis and also better OPCode caching from PHP.

Loading history...
Deprecated Code introduced by
The class Kunstmaan\TranslatorBund...ice\Command\DiffCommand has been deprecated with message: This class is deprecated since KunstmaanTranslatorBundle 5.2 and will be removed in 6.0.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
53
    {
54
        protected function configure()
55
        {
56
            parent::configure();
57
58
            $this
59
                ->setName('kuma:translator:migrations:diff')
60
                ->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command.')
61
            ;
62
        }
63
64
        public function execute(InputInterface $input, OutputInterface $output)
65
        {
66
            DoctrineCommandHelper::setApplicationEntityManager($this->getApplication(), $input->getOption('em'));
0 ignored issues
show
Documentation introduced by
$this->getApplication() is of type null|object<Symfony\Comp...nt\Console\Application>, but the function expects a object<Symfony\Bundle\Fr...le\Console\Application>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
67
68
            $configuration = $this->getMigrationConfiguration($input, $output);
69
            DoctrineCommand::configureMigrations($this->getApplication()->getKernel()->getContainer(), $configuration);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Console\Application as the method getKernel() does only exist in the following sub-classes of Symfony\Component\Console\Application: Symfony\Bundle\FrameworkBundle\Console\Application. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
70
71
            $exitCode = parent::execute($input, $output);
72
73
            return is_numeric($exitCode) ? (int) $exitCode : 0;
74
        }
75
    }
76
}
77