Passed
Pull Request — master (#247)
by
unknown
04:35
created

MakeFactory::generateEntity()   B

Complexity

Conditions 7
Paths 18

Size

Total Lines 51
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
c 1
b 0
f 0
dl 0
loc 51
ccs 0
cts 0
cp 0
rs 8.5706
cc 7
nc 18
nop 4
crap 56

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\ORM\EntityManagerInterface;
6
use Doctrine\Persistence\ManagerRegistry;
7
use Symfony\Bundle\MakerBundle\ConsoleStyle;
8
use Symfony\Bundle\MakerBundle\DependencyBuilder;
9
use Symfony\Bundle\MakerBundle\Exception\RuntimeCommandException;
10
use Symfony\Bundle\MakerBundle\Generator;
11
use Symfony\Bundle\MakerBundle\InputConfiguration;
12
use Symfony\Bundle\MakerBundle\Maker\AbstractMaker;
13
use Symfony\Component\Console\Command\Command;
14
use Symfony\Component\Console\Input\InputArgument;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Input\InputOption;
17
use Zenstruck\Foundry\ModelFactory;
18
19
/**
20
 * @author Kevin Bond <[email protected]>
21
 */
22
final class MakeFactory extends AbstractMaker
23
{
24
    private const ORM_DEFAULTS = [
25 20
        'ARRAY' => '[],',
26
        'ASCII_STRING' => 'self::faker()->text(),',
27 20
        'BIGINT' => 'self::faker()->randomNumber(),',
28 20
        'BLOB' => 'self::faker()->text(),',
29
        'BOOLEAN' => 'self::faker()->boolean(),',
30 4
        'DATE' => 'self::faker()->datetime(),',
31
        'DATE_MUTABLE' => 'self::faker()->datetime(),',
32 4
        'DATE_IMMUTABLE' => 'self::faker()->datetime(),',
33
        'DATETIME_MUTABLE' => 'self::faker()->datetime(),',
34
        'DATETIME_IMMUTABLE' => 'self::faker()->datetime(),',
35 20
        'DATETIMETZ_MUTABLE' => 'self::faker()->datetime(),',
36
        'DATETIMETZ_IMMUTABLE' => 'self::faker()->datetime(),',
37
        'DECIMAL' => 'self::faker()->randomFloat(),',
38 20
        'FLOAT' => 'self::faker()->randomFloat(),',
39 20
        'INTEGER' => 'self::faker()->randomNumber(),',
40 20
        'JSON' => '[],',
41
        'JSON_ARRAY' => '[],',
42
        'SIMPLE_ARRAY' => '[],',
43 20
        'SMALLINT' => 'self::faker()->numberBetween(1, 32767),',
44 20
        'STRING' => 'self::faker()->text(),',
45
        'TEXT' => 'self::faker()->text(),',
46 20
        'TIME_MUTABLE' => 'self::faker()->datetime(),',
47
        'TIME_IMMUTABLE' => 'self::faker()->datetime(),',
48 20
    ];
49 12
50
    /** @var ManagerRegistry */
51
    private $managerRegistry;
52 8
53 4
    /** @var string[] */
54 4
    private $entitiesWithFactories;
55
56
    public function __construct(ManagerRegistry $managerRegistry, \Traversable $factories)
57 8
    {
58 8
        $this->managerRegistry = $managerRegistry;
59
        $this->entitiesWithFactories = \array_map(
60 8
            static function(ModelFactory $factory) {
61 8
                return $factory::getEntityClass();
62
            },
63 20
            \iterator_to_array($factories)
64
        );
65 20
    }
66
67 20
    public static function getCommandName(): string
68 4
    {
69
        return 'make:factory';
70
    }
71 20
72 4
    public static function getCommandDescription(): string
73
    {
74
        return 'Creates a Foundry model factory for a Doctrine entity class';
75 16
    }
76 16
77 16
    public function configureCommand(Command $command, InputConfiguration $inputConfig): void
78 16
    {
79 16
        $command
80
            ->setDescription(self::getCommandDescription())
81
            ->addArgument('entity', InputArgument::OPTIONAL, 'Entity class to create a factory for')
82 16
            ->addOption('namespace', null, InputOption::VALUE_REQUIRED, 'Customize the namespace for generated factories', 'Factory')
83
            ->addOption('test', null, InputOption::VALUE_NONE, 'Create in <fg=yellow>tests/</> instead of <fg=yellow>src/</>')
84 16
            ->addOption('all-fields', null, InputOption::VALUE_NONE, 'Create defaults for all entity fields, not only required fields')
85
        ;
86 16
87
        $inputConfig->setArgumentAsNonInteractive('entity');
88
    }
89 16
90 16
    public function interact(InputInterface $input, ConsoleStyle $io, Command $command): void
91 16
    {
92
        if ($input->getArgument('entity')) {
93 16
            return;
94 16
        }
95
96
        if (!$input->getOption('test')) {
97
            $io->text('// Note: pass <fg=yellow>--test</> if you want to generate factories in your <fg=yellow>tests/</> directory');
98 16
            $io->newLine();
99
        }
100 16
101
        if (!$input->getOption('all-fields')) {
102 16
            $io->text('// Note: pass <fg=yellow>--all-fields</> if you want to generate default values for all fields, not only required fields');
103 16
            $io->newLine();
104
        }
105
106 16
        $entity_argument = $command->getDefinition()->getArgument('entity');
107
        $choices = \array_merge($this->entityChoices(), ['All']);
108 20
        $entity = $io->choice($entity_argument->getDescription(), $choices);
109
110
        $input->setArgument('entity', $entity);
111 20
    }
112
113 8
    public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void
114
    {
115 8
        $class_or_all = $input->getArgument('entity');
116
        switch ($class_or_all) {
117 8
            case 'All':
118 8
                foreach ($this->entityChoices() as $class) {
119 8
                    $this->generateEntity($class, $input, $io, $generator);
120
                }
121
                break;
122
            default:
123 8
                $this->generateEntity($class_or_all, $input, $io, $generator);
124
                break;
125 8
        }
126
    }
127
128
    public function configureDependencies(DependencyBuilder $dependencies): void
129
    {
130
        // noop
131
    }
132
133
    /**
134
     * Generates a single entity factory.
135
     */
136
    private function generateEntity(string $class, InputInterface $input, ConsoleStyle $io, Generator $generator)
137
    {
138
        if (!\class_exists($class)) {
139
            $class = $generator->createClassNameDetails($class, 'Entity\\')->getFullName();
140
        }
141
142
        if (!\class_exists($class)) {
143
            throw new RuntimeCommandException(\sprintf('Entity "%s" not found.', $input->getArgument('entity')));
144
        }
145
146
        $namespace = $input->getOption('namespace');
147
148
        // strip maker's root namespace if set
149
        if (0 === \mb_strpos($namespace, $generator->getRootNamespace())) {
150
            $namespace = \mb_substr($namespace, \mb_strlen($generator->getRootNamespace()));
151
        }
152
153
        $namespace = \trim($namespace, '\\');
154
155
        // if creating in tests dir, ensure namespace prefixed with Tests\
156
        if ($input->getOption('test') && 0 !== \mb_strpos($namespace, 'Tests\\')) {
157
            $namespace = 'Tests\\'.$namespace;
158
        }
159
160
        $entity = new \ReflectionClass($class);
161
        $factory = $generator->createClassNameDetails($entity->getShortName(), $namespace, 'Factory');
162
163
        $repository = new \ReflectionClass($this->managerRegistry->getRepository($entity->getName()));
164
165
        if (0 !== \mb_strpos($repository->getName(), $generator->getRootNamespace())) {
166
            // not using a custom repository
167
            $repository = null;
168
        }
169
170
        $generator->generateClass(
171
            $factory->getFullName(),
172
            __DIR__.'/../Resources/skeleton/Factory.tpl.php',
173
            [
174
                'entity' => $entity,
175
                'defaultProperties' => $this->defaultPropertiesFor($entity->getName(), $input->getOption('all-fields')),
176
                'repository' => $repository,
177
            ]
178
        );
179
180
        $generator->writeChanges();
181
182
        $this->writeSuccessMessage($io);
183
184
        $io->text([
185
            'Next: Open your new factory and set default values/states.',
186
            'Find the documentation at https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#model-factories',
187
        ]);
188
    }
189
190
    private function entityChoices(): array
191
    {
192
        $choices = [];
193
194
        foreach ($this->managerRegistry->getManagers() as $manager) {
195
            foreach ($manager->getMetadataFactory()->getAllMetadata() as $metadata) {
196
                if (!\in_array($metadata->getName(), $this->entitiesWithFactories, true)) {
197
                    $choices[] = $metadata->getName();
198
                }
199
            }
200
        }
201
202
        \sort($choices);
203
204
        if (empty($choices)) {
205
            throw new RuntimeCommandException('No entities or documents found, or none left to make factories for.');
206
        }
207
208
        return $choices;
209
    }
210
211
    private function defaultPropertiesFor(string $class, bool $allFields): iterable
212
    {
213
        $em = $this->managerRegistry->getManagerForClass($class);
214
215
        if (!$em instanceof EntityManagerInterface) {
216
            return [];
217
        }
218
219
        $metadata = $em->getClassMetadata($class);
220
        $ids = $metadata->getIdentifierFieldNames();
221
222
        foreach ($metadata->fieldMappings as $property) {
223
            // ignore identifiers and nullable fields
224
            if ((!$allFields && ($property['nullable'] ?? false)) || \in_array($property['fieldName'], $ids, true)) {
225
                continue;
226
            }
227
228
            $type = \mb_strtoupper($property['type']);
229
            $value = "null, // TODO add {$type} ORM type manually";
230
231
            if (\array_key_exists($type, self::ORM_DEFAULTS)) {
232
                $value = self::ORM_DEFAULTS[$type];
233
            }
234
235
            yield $property['fieldName'] => $value;
236
        }
237
    }
238
}
239