AbstractApiCommand   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Test Coverage

Coverage 78.13%

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 91
ccs 25
cts 32
cp 0.7813
rs 10
c 0
b 0
f 0
wmc 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getWriter() 0 6 2
A configure() 0 4 1
A setConfigProvider() 0 3 1
A getConfigProvider() 0 3 1
A execute() 0 5 1
A initialize() 0 17 3
1
<?php
2
declare(strict_types=1);
3
4
namespace SlayerBirden\DFCodeGeneration\Command;
5
6
use Doctrine\Common\Annotations\AnnotationRegistry;
7
use SlayerBirden\DFCodeGeneration\Generator\Config\CurrentConfigProviderInterface;
8
use SlayerBirden\DFCodeGeneration\Writer\OutputWriter;
9
use SlayerBirden\DFCodeGeneration\Writer\WriteInterface;
10
use Symfony\Component\Console\Command\Command;
11
use Symfony\Component\Console\Exception\InvalidArgumentException;
12
use Symfony\Component\Console\Exception\LogicException;
13
use Symfony\Component\Console\Input\InputArgument;
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Input\InputOption;
16
use Symfony\Component\Console\Output\OutputInterface;
17
18
abstract class AbstractApiCommand extends Command
19
{
20
    /**
21
     * @var OutputInterface
22
     */
23
    protected $output;
24
    /**
25
     * @var string
26
     */
27
    protected $entityClassName;
28
    /**
29
     * @var bool
30
     */
31
    protected $force;
32
    /**
33
     * @var null|WriteInterface
34
     */
35
    protected $writer;
36
    /**
37
     * @var null|CurrentConfigProviderInterface
38
     */
39
    private $configProvider;
40
41 20
    public function __construct(?string $name = null, ?WriteInterface $writer = null, ?CurrentConfigProviderInterface $configProvider = null)
42
    {
43 20
        parent::__construct($name);
44 20
        $this->writer = $writer;
45 20
        $this->configProvider = $configProvider;
46 20
    }
47
48 20
    protected function configure()
49
    {
50 20
        $this->addArgument('entity', InputArgument::REQUIRED, 'Entity class')
51 20
            ->addOption('force', 'f', InputOption::VALUE_NONE, 'Without force flag no writes happen (only output).');
52 20
    }
53
54 20
    protected function initialize(InputInterface $input, OutputInterface $output)
55
    {
56
        // we need to validate before assigning values
57 20
        $input->validate();
58 20
        $this->output = $output;
59 20
        $this->entityClassName = $input->getArgument('entity');
0 ignored issues
show
Documentation Bug introduced by
It seems like $input->getArgument('entity') can also be of type string[]. However, the property $entityClassName is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
60 20
        if (!class_exists($this->entityClassName)) {
0 ignored issues
show
Bug introduced by
It seems like $this->entityClassName can also be of type string[]; however, parameter $class_name of class_exists() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

60
        if (!class_exists(/** @scrutinizer ignore-type */ $this->entityClassName)) {
Loading history...
61
            throw new InvalidArgumentException('Entity Class does not exist.');
62
        }
63 20
        $this->force = $input->getOption('force');
64
        // If it's not force mode we're using output writer
65 20
        if (!$this->force) {
66 10
            $this->writer = new OutputWriter($this->output);
67
        }
68
69
        // we need to register the default Annotation loader
70 20
        AnnotationRegistry::registerLoader('class_exists');
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\Common\Annotati...istry::registerLoader() has been deprecated: this method is deprecated and will be removed in doctrine/annotations 2.0 autoloading should be deferred to the globally registered autoloader by then. For now, use @example AnnotationRegistry::registerLoader('class_exists') ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

70
        /** @scrutinizer ignore-deprecated */ AnnotationRegistry::registerLoader('class_exists');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

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

Loading history...
71 20
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 20
    protected function execute(InputInterface $input, OutputInterface $output)
77
    {
78 20
        $output->writeln([
79 20
            '<error>IMPORTANT: please check all generated files before committing.</error>',
80
            '<error># You might want to run something like "php-cs-fixer" to make sure formatting is correct.</error>',
81
        ]);
82 20
    }
83
84
    /**
85
     * @return WriteInterface
86
     */
87
    protected function getWriter(): WriteInterface
88
    {
89
        if ($this->writer === null) {
90
            throw new LogicException('Writer is not defined!');
91
        }
92
        return $this->writer;
93
    }
94
95
    /**
96
     * @return CurrentConfigProviderInterface|null
97
     */
98 10
    public function getConfigProvider(): ?CurrentConfigProviderInterface
99
    {
100 10
        return $this->configProvider;
101
    }
102
103
    /**
104
     * @param CurrentConfigProviderInterface $configProvider
105
     */
106
    public function setConfigProvider(CurrentConfigProviderInterface $configProvider): void
107
    {
108
        $this->configProvider = $configProvider;
109
    }
110
}
111