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\GeneratorBundle\Command; |
||
4 | |||
5 | use Kunstmaan\GeneratorBundle\Generator\DefaultEntityGenerator; |
||
6 | use Symfony\Component\Console\Input\InputOption; |
||
7 | use Symfony\Component\HttpKernel\Bundle\BundleInterface; |
||
8 | |||
9 | /** |
||
10 | * GenerateEntityCommand |
||
11 | */ |
||
12 | class GenerateEntityCommand extends KunstmaanGenerateCommand |
||
13 | { |
||
14 | /** |
||
15 | * @var BundleInterface |
||
16 | */ |
||
17 | private $bundle; |
||
18 | |||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | private $prefix; |
||
23 | |||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | private $entityName; |
||
28 | |||
29 | /** |
||
30 | * @var array |
||
31 | */ |
||
32 | private $fields; |
||
33 | |||
34 | /** |
||
35 | * @var bool |
||
36 | */ |
||
37 | private $withRepository; |
||
38 | |||
39 | /** |
||
40 | * {@inheritdoc} |
||
41 | */ |
||
42 | protected function configure() |
||
43 | { |
||
44 | $this |
||
45 | ->setName('kuma:generate:entity') |
||
46 | ->setDescription('Generates a new Doctrine entity inside a bundle') |
||
47 | ->addOption('prefix', '', InputOption::VALUE_OPTIONAL, 'The prefix to be used in the table names of the generated entities') |
||
48 | ->addOption('with-repository', null, InputOption::VALUE_NONE, 'Whether to generate the entity repository or not (y/n)') |
||
49 | ->setHelp(<<<'EOT' |
||
50 | The <info>kuma:generate:entity</info> task generates a new entity inside a bundle: |
||
51 | |||
52 | <info>php bin/console kuma:generate:entity</info> |
||
53 | |||
54 | The command can also generate the corresponding entity repository class with the |
||
55 | <comment>--with-repository</comment> option: |
||
56 | |||
57 | <info>php bin/console kuma:generate:entity --with-repository</info> |
||
58 | |||
59 | Use the <info>--prefix</info> option to add a prefix to the table names of the generated entities |
||
60 | |||
61 | <info>php bin/console kuma:generate:entity --prefix=demo_</info> |
||
62 | EOT |
||
63 | ); |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * @return DefaultEntityGenerator |
||
68 | */ |
||
69 | View Code Duplication | protected function createGenerator() |
|
0 ignored issues
–
show
|
|||
70 | { |
||
71 | $filesystem = $this->getContainer()->get('filesystem'); |
||
72 | $registry = $this->getContainer()->get('doctrine'); |
||
73 | |||
74 | return new DefaultEntityGenerator($filesystem, $registry, '/entity', $this->assistant, $this->getContainer()); |
||
0 ignored issues
–
show
$filesystem is of type object|null , but the function expects a object<Symfony\Component\Filesystem\Filesystem> .
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...
$registry is of type object|null , but the function expects a object<Symfony\Bridge\Doctrine\RegistryInterface> .
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...
|
|||
75 | } |
||
76 | |||
77 | /** |
||
78 | * The text to be displayed on top of the generator. |
||
79 | * |
||
80 | * @return string|array |
||
81 | */ |
||
82 | protected function getWelcomeText() |
||
83 | { |
||
84 | return 'Welcome to the Kunstmaan entity generator'; |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * {@inheritdoc} |
||
89 | */ |
||
90 | protected function doExecute() |
||
91 | { |
||
92 | $this->assistant->writeSection('Entity generation'); |
||
93 | |||
94 | $this->createGenerator()->generate($this->bundle, $this->entityName, $this->prefix, $this->fields, $this->withRepository); |
||
95 | |||
96 | $this->assistant->writeSection('Entity successfully created', 'bg=green;fg=black'); |
||
97 | $this->assistant->writeLine(array( |
||
98 | 'Make sure you update your database first before you test the entity:', |
||
99 | ' Directly update your database: <comment>bin/console doctrine:schema:update --force</comment>', |
||
100 | ' Create a Doctrine migration and run it: <comment>bin/console doctrine:migrations:diff && app/console doctrine:migrations:migrate</comment>', |
||
101 | )); |
||
102 | |||
103 | return 0; |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * Do the interaction with the end user. |
||
108 | */ |
||
109 | protected function doInteract() |
||
110 | { |
||
111 | $this->assistant->writeLine(array("This command helps you to generate a new entity.\n")); |
||
112 | |||
113 | /* |
||
114 | * Ask for which bundle we need to create the pagepart |
||
115 | */ |
||
116 | $this->bundle = $this->askForBundleName('entity'); |
||
117 | |||
118 | /* |
||
119 | * Ask the database table prefix |
||
120 | */ |
||
121 | $this->prefix = $this->askForPrefix(null, $this->bundle->getNamespace()); |
||
122 | |||
123 | /* |
||
124 | * Ask the name of the pagepart |
||
125 | */ |
||
126 | $this->assistant->writeLine(array( |
||
127 | '', |
||
128 | 'The name of your Entity: For example: <comment>Address</comment>', |
||
129 | '', |
||
130 | )); |
||
131 | $generator = $this->getGenerator(); |
||
132 | $bundlePath = $this->bundle->getPath(); |
||
133 | $name = $this->assistant->askAndValidate( |
||
134 | 'Entity name', |
||
135 | function ($name) use ($generator, $bundlePath) { |
||
136 | // Check reserved words |
||
137 | if ($generator->isReservedKeyword($name)) { |
||
138 | throw new \InvalidArgumentException(sprintf('"%s" is a reserved word', $name)); |
||
139 | } |
||
140 | |||
141 | if (!preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $name)) { |
||
142 | throw new \InvalidArgumentException(sprintf('Invalid entity name: %s', $name)); |
||
143 | } |
||
144 | |||
145 | // Check that entity does not already exist |
||
146 | if (file_exists($bundlePath.'/Entity/'.$name.'.php')) { |
||
147 | throw new \InvalidArgumentException(sprintf('Entity "%s" already exists', $name)); |
||
148 | } |
||
149 | |||
150 | return $name; |
||
151 | } |
||
152 | ); |
||
153 | $this->entityName = $name; |
||
154 | |||
155 | /* |
||
156 | * Ask which fields need to be present |
||
157 | */ |
||
158 | $this->assistant->writeLine(array("\nInstead of starting with a blank entity, you can add some fields now.\n")); |
||
159 | $fields = $this->askEntityFields($this->bundle); |
||
160 | $this->fields = array_map(function ($fieldInfo) { |
||
161 | switch ($fieldInfo['type']) { |
||
162 | case 'image': |
||
163 | return $this->getEntityFields($this->bundle, $this->entityName, $this->prefix, $fieldInfo['name'], $fieldInfo['type'], |
||
164 | $fieldInfo['extra'], true, $fieldInfo['minHeight'], $fieldInfo['maxHeight'], $fieldInfo['minWidth'], $fieldInfo['maxWidth'], $fieldInfo['mimeTypes']); |
||
165 | |||
166 | break; |
||
0 ignored issues
–
show
break is not strictly necessary here and could be removed.
The break statement is not necessary if it is preceded for example by a return statement: switch ($x) {
case 1:
return 'foo';
break; // This break is not necessary and can be left off.
}
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.
Loading history...
|
|||
167 | |||
168 | case 'media': |
||
169 | return $this->getEntityFields($this->bundle, $this->entityName, $this->prefix, $fieldInfo['name'], $fieldInfo['type'], |
||
170 | $fieldInfo['extra'], true, null, null, null, null, $fieldInfo['mimeTypes']); |
||
171 | |||
172 | break; |
||
0 ignored issues
–
show
break is not strictly necessary here and could be removed.
The break statement is not necessary if it is preceded for example by a return statement: switch ($x) {
case 1:
return 'foo';
break; // This break is not necessary and can be left off.
}
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.
Loading history...
|
|||
173 | |||
174 | default: |
||
175 | return $this->getEntityFields($this->bundle, $this->entityName, $this->prefix, $fieldInfo['name'], $fieldInfo['type'], $fieldInfo['extra'], true); |
||
176 | |||
177 | break; |
||
0 ignored issues
–
show
break is not strictly necessary here and could be removed.
The break statement is not necessary if it is preceded for example by a return statement: switch ($x) {
case 1:
return 'foo';
break; // This break is not necessary and can be left off.
}
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.
Loading history...
|
|||
178 | } |
||
179 | }, $fields); |
||
180 | |||
181 | /* |
||
182 | * Ask if a repository class needs to be generated |
||
183 | */ |
||
184 | $this->withRepository = $this->askForWithRepository(); |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * @return bool |
||
189 | */ |
||
190 | View Code Duplication | protected function askForWithRepository() |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
191 | { |
||
192 | $withRepositoryOption = $this->assistant->getOption('with-repository'); |
||
193 | if ($withRepositoryOption != 'y' && $withRepositoryOption != 'n') { |
||
194 | /** @var $question */ |
||
195 | $withRepositoryOption = $this->assistant->askConfirmation("\nDo you want to generate a repository class for the entity ? (y/n)\n", '', '?', false); |
||
196 | } |
||
197 | |||
198 | return $withRepositoryOption == 'y'; |
||
199 | } |
||
200 | } |
||
201 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.