Passed
Pull Request — master (#171)
by Kevin
04:24 queued 01:50
created

MakeFactory::generate()   B

Complexity

Conditions 7
Paths 18

Size

Total Lines 52
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 8.9755

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
c 1
b 0
f 0
dl 0
loc 52
ccs 23
cts 35
cp 0.6571
rs 8.5706
cc 7
nc 18
nop 3
crap 8.9755

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Zenstruck\Foundry\Bundle\Maker;
4
5
use Doctrine\Persistence\ManagerRegistry;
6
use Symfony\Bundle\MakerBundle\ConsoleStyle;
7
use Symfony\Bundle\MakerBundle\DependencyBuilder;
8
use Symfony\Bundle\MakerBundle\Exception\RuntimeCommandException;
9
use Symfony\Bundle\MakerBundle\Generator;
10
use Symfony\Bundle\MakerBundle\InputConfiguration;
11
use Symfony\Bundle\MakerBundle\Maker\AbstractMaker;
12
use Symfony\Component\Console\Command\Command;
13
use Symfony\Component\Console\Input\InputArgument;
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Input\InputOption;
16
use Zenstruck\Foundry\ModelFactory;
17
18
/**
19
 * @author Kevin Bond <[email protected]>
20
 */
21
final class MakeFactory extends AbstractMaker
22
{
23
    /** @var ManagerRegistry */
24
    private $managerRegistry;
25 20
26
    /** @var string[] */
27 20
    private $entitiesWithFactories;
28 20
29
    public function __construct(ManagerRegistry $managerRegistry, \Traversable $factories)
30 4
    {
31
        $this->managerRegistry = $managerRegistry;
32 4
        $this->entitiesWithFactories = \array_map(
33
            static function(ModelFactory $factory) {
34
                return $factory::getEntityClass();
35 20
            },
36
            \iterator_to_array($factories)
37
        );
38 20
    }
39 20
40 20
    public static function getCommandName(): string
41
    {
42
        return 'make:factory';
43 20
    }
44 20
45
    public static function getCommandDescription(): string
46 20
    {
47
        return 'Creates a Foundry model factory for a Doctrine entity class';
48 20
    }
49 12
50
    public function configureCommand(Command $command, InputConfiguration $inputConfig): void
51
    {
52 8
        $command
53 4
            ->setDescription(self::getCommandDescription())
54 4
            ->addArgument('entity', InputArgument::OPTIONAL, 'Entity class to create a factory for')
55
            ->addOption('namespace', null, InputOption::VALUE_REQUIRED, 'Customize the namespace for generated factories', 'Factory')
56
            ->addOption('test', null, InputOption::VALUE_NONE, 'Create in <fg=yellow>tests/</> instead of <fg=yellow>src/</>')
57 8
        ;
58 8
59
        $inputConfig->setArgumentAsNonInteractive('entity');
60 8
    }
61 8
62
    public function interact(InputInterface $input, ConsoleStyle $io, Command $command): void
63 20
    {
64
        if ($input->getArgument('entity')) {
65 20
            return;
66
        }
67 20
68 4
        if (!$input->getOption('test')) {
69
            $io->text('// Note: pass <fg=yellow>--test</> if you want to generate factories in your <fg=yellow>tests/</> directory');
70
            $io->newLine();
71 20
        }
72 4
73
        $argument = $command->getDefinition()->getArgument('entity');
74
        $entity = $io->choice($argument->getDescription(), $this->entityChoices());
75 16
76 16
        $input->setArgument('entity', $entity);
77 16
    }
78 16
79 16
    public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void
80
    {
81
        $class = $input->getArgument('entity');
82 16
83
        if (!\class_exists($class)) {
0 ignored issues
show
Bug introduced by
It seems like $class can also be of type null and string[]; however, parameter $class 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

83
        if (!\class_exists(/** @scrutinizer ignore-type */ $class)) {
Loading history...
84 16
            $class = $generator->createClassNameDetails($class, 'Entity\\')->getFullName();
0 ignored issues
show
Bug introduced by
It seems like $class can also be of type null and string[]; however, parameter $name of Symfony\Bundle\MakerBund...reateClassNameDetails() 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

84
            $class = $generator->createClassNameDetails(/** @scrutinizer ignore-type */ $class, 'Entity\\')->getFullName();
Loading history...
85
        }
86 16
87
        if (!\class_exists($class)) {
88
            throw new RuntimeCommandException(\sprintf('Entity "%s" not found.', $input->getArgument('entity')));
0 ignored issues
show
Bug introduced by
It seems like $input->getArgument('entity') can also be of type string[]; however, parameter $values of sprintf() does only seem to accept double|integer|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

88
            throw new RuntimeCommandException(\sprintf('Entity "%s" not found.', /** @scrutinizer ignore-type */ $input->getArgument('entity')));
Loading history...
89 16
        }
90 16
91 16
        $namespace = $input->getOption('namespace');
92
93 16
        // strip maker's root namespace if set
94 16
        if (0 === \mb_strpos($namespace, $generator->getRootNamespace())) {
95
            $namespace = \mb_substr($namespace, \mb_strlen($generator->getRootNamespace()));
96
        }
97
98 16
        $namespace = \trim($namespace, '\\');
99
100 16
        // if creating in tests dir, ensure namespace prefixed with Tests\
101
        if ($input->getOption('test') && 0 !== \mb_strpos($namespace, 'Tests\\')) {
102 16
            $namespace = 'Tests\\'.$namespace;
103 16
        }
104
105
        $entity = new \ReflectionClass($class);
0 ignored issues
show
Bug introduced by
It seems like $class can also be of type string[]; however, parameter $objectOrClass of ReflectionClass::__construct() does only seem to accept object|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

105
        $entity = new \ReflectionClass(/** @scrutinizer ignore-type */ $class);
Loading history...
106 16
        $factory = $generator->createClassNameDetails($entity->getShortName(), $namespace, 'Factory');
107
108 20
        $repository = new \ReflectionClass($this->managerRegistry->getRepository($entity->getName()));
109
110
        if (0 !== \mb_strpos($repository->getName(), $generator->getRootNamespace())) {
111 20
            // not using a custom repository
112
            $repository = null;
113 8
        }
114
115 8
        $generator->generateClass(
116
            $factory->getFullName(),
117 8
            __DIR__.'/../Resources/skeleton/Factory.tpl.php',
118 8
            [
119 8
                'entity' => $entity,
120
                'repository' => $repository,
121
            ]
122
        );
123 8
124
        $generator->writeChanges();
125 8
126
        $this->writeSuccessMessage($io);
127
128
        $io->text([
129
            'Next: Open your new factory and set default values/states.',
130
            'Find the documentation at https://github.com/zenstruck/foundry#model-factories',
131
        ]);
132
    }
133
134
    public function configureDependencies(DependencyBuilder $dependencies): void
135
    {
136
        // noop
137
    }
138
139
    private function entityChoices(): array
140
    {
141
        $choices = [];
142
143
        foreach ($this->managerRegistry->getManagers() as $manager) {
144
            foreach ($manager->getMetadataFactory()->getAllMetadata() as $metadata) {
145
                if (!\in_array($metadata->getName(), $this->entitiesWithFactories, true)) {
146
                    $choices[] = $metadata->getName();
147
                }
148
            }
149
        }
150
151
        \sort($choices);
152
153
        if (empty($choices)) {
154
            throw new RuntimeCommandException('No entities found.');
155
        }
156
157
        return $choices;
158
    }
159
}
160