Skip to content

Commit c79b73c

Browse files
committed
Fixes thanks to Stof!
1 parent 6c914fa commit c79b73c

File tree

7 files changed

+68
-32
lines changed

7 files changed

+68
-32
lines changed

src/Doctrine/DoctrineMetadataFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* Simpler version of DoctrineBundle's DisconnectedMetadataFactory, to
2020
* avoid PSR-4 issues.
2121
*
22+
* @internal
2223
* @author Fabien Potencier <fabien@symfony.com>
2324
* @author Ryan Weaver <ryan@knpuniversity.com>
2425
*/

src/Maker/MakeEntity.php

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
1515
use Doctrine\Common\Persistence\ManagerRegistry;
1616
use Doctrine\DBAL\Types\Type;
17+
use Doctrine\ORM\Mapping\Column;
1718
use Psr\Container\ContainerInterface;
1819
use Symfony\Bundle\MakerBundle\ConsoleStyle;
1920
use Symfony\Bundle\MakerBundle\DependencyBuilder;
@@ -262,10 +263,17 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
262263

263264
public function configureDependencies(DependencyBuilder $dependencies)
264265
{
266+
// guarantee DoctrineBundle
265267
$dependencies->addClassDependency(
266268
DoctrineBundle::class,
267269
'orm'
268270
);
271+
272+
// guarantee ORM
273+
$dependencies->addClassDependency(
274+
Column::class,
275+
'orm'
276+
);
269277
}
270278

271279
private function askForNextField(ConsoleStyle $io, array $fields, string $entityClass)
@@ -290,15 +298,15 @@ private function askForNextField(ConsoleStyle $io, array $fields, string $entity
290298

291299
$defaultType = 'string';
292300
// try to guess the type by the field name prefix/suffix
293-
// convert to camel case for simplicity
294-
$camelCasedField = Str::snakeCase($fieldName);
295-
if ('_at' == substr($camelCasedField, -3)) {
301+
// convert to snake case for simplicity
302+
$snakeCasedField = Str::asSnakeCase($fieldName);
303+
if ('_at' == substr($snakeCasedField, -3)) {
296304
$defaultType = 'datetime';
297-
} elseif ('_id' == substr($camelCasedField, -3)) {
305+
} elseif ('_id' == substr($snakeCasedField, -3)) {
298306
$defaultType = 'integer';
299-
} elseif ('is_' == substr($camelCasedField, 0, 3)) {
307+
} elseif ('is_' == substr($snakeCasedField, 0, 3)) {
300308
$defaultType = 'boolean';
301-
} elseif ('has_' == substr($camelCasedField, 0, 4)) {
309+
} elseif ('has_' == substr($snakeCasedField, 0, 4)) {
302310
$defaultType = 'boolean';
303311
}
304312

@@ -514,20 +522,21 @@ function ($name) use ($targetClass) {
514522

515523
$askOrphanRemoval = function (string $owningClass, string $inverseClass) use ($io) {
516524
$io->text([
525+
'Do you want to activate <comment>orphanRemoval</comment> on your relationship?',
517526
sprintf(
518-
'A <comment>%s</comment> becomes "orphaned" if it is removed from its related <comment>%s</comment>.',
527+
'A <comment>%s</comment> is "orphaned" when it is removed from its related <comment>%s</comment>.',
519528
Str::getShortClassName($owningClass),
520529
Str::getShortClassName($inverseClass)
521530
),
522531
sprintf(
523-
'For example: <comment>$%s->remove%s($%s)</comment>',
532+
'e.g. <comment>$%s->remove%s($%s)</comment>',
524533
Str::asLowerCamelCase(Str::getShortClassName($inverseClass)),
525534
Str::asCamelCase(Str::getShortClassName($owningClass)),
526535
Str::asLowerCamelCase(Str::getShortClassName($owningClass))
527536
),
528537
'',
529538
sprintf(
530-
'NOTE: If a <comment>%s</comment> should be allowed to *change* from one <comment>%s</comment> to another, answer "no".',
539+
'NOTE: If a <comment>%s</comment> may *change* from one <comment>%s</comment> to another, answer "no".',
531540
Str::getShortClassName($owningClass),
532541
Str::getShortClassName($inverseClass)
533542
),

src/Resources/skeleton/doctrine/Repository.tpl.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ public function findByExampleField($value)
3131
->orderBy('<?= $entity_alias; ?>.id', 'ASC')
3232
->setMaxResults(10)
3333
->getQuery()
34-
->execute()
34+
->getResult()
3535
;
3636
}
3737
*/
3838

3939
/*
40-
public function findOneBySomeField($value): <?= $entity_class_name."\n" ?>
40+
public function findOneBySomeField($value): ?<?= $entity_class_name."\n" ?>
4141
{
4242
return $this->createQueryBuilder('<?= $entity_alias ?>')
4343
->andWhere('<?= $entity_alias ?>.exampleField = :val')

src/Str.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public static function asLowerCamelCase(string $str): string
8787

8888
public static function asCamelCase(string $str): string
8989
{
90-
return strtr(ucwords(strtr($str, ['_' => ' ', '.' => '_ ', '\\' => '_ '])), [' ' => '']);
90+
return strtr(ucwords(strtr($str, ['_' => ' ', '.' => ' ', '\\' => ' '])), [' ' => '']);
9191
}
9292

9393
public static function asRoutePath(string $value): string
@@ -100,7 +100,7 @@ public static function asRouteName(string $value): string
100100
return self::asTwigVariable($value);
101101
}
102102

103-
public static function snakeCase(string $value): string
103+
public static function asSnakeCase(string $value): string
104104
{
105105
return self::asTwigVariable($value);
106106
}
@@ -145,7 +145,7 @@ public static function getNamespace(string $fullClassName): string
145145

146146
public static function singularCamelCaseToPluralCamelCase(string $camelCase): string
147147
{
148-
$snake = self::snakeCase($camelCase);
148+
$snake = self::asSnakeCase($camelCase);
149149
$words = explode('_', $snake);
150150
$words[count($words) - 1] = Inflector::pluralize($words[count($words) - 1]);
151151
$reSnaked = implode('_', $words);
@@ -155,7 +155,7 @@ public static function singularCamelCaseToPluralCamelCase(string $camelCase): st
155155

156156
public static function pluralCamelCaseToSingular(string $camelCase): string
157157
{
158-
$snake = self::snakeCase($camelCase);
158+
$snake = self::asSnakeCase($camelCase);
159159
$words = explode('_', $snake);
160160
$words[count($words) - 1] = Inflector::singularize($words[count($words) - 1]);
161161
$reSnaked = implode('_', $words);

src/Util/ClassSourceManipulator.php

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -199,33 +199,44 @@ private function createSetterNodeBuilder(string $propertyName, ?string $type, bo
199199
private function buildAnnotationLine(string $annotationClass, array $options)
200200
{
201201
$formattedOptions = array_map(function ($option, $value) {
202-
$quoteValue = !(is_bool($value) || is_int($value) || is_array($value));
203-
204-
if (is_bool($value)) {
205-
$value = $value ? 'true' : 'false';
206-
}
207-
208202
if (is_array($value)) {
209203
if (!isset($value[0])) {
210204
// associative array: we'll add this if/when we need it
211205
throw new \Exception('Not currently supported');
212206
}
213207

214-
$value = sprintf('{%s}', implode(', ', array_map(function ($val) {
215-
return sprintf('"%s"', $val);
208+
return sprintf('%s={%s}', $option, implode(', ', array_map(function ($val) {
209+
return $this->quoteAnnotationValue($val);
216210
}, $value)));
217211
}
218212

219-
if ($quoteValue) {
220-
return sprintf('%s="%s"', $option, $value);
221-
}
222-
223-
return sprintf('%s=%s', $option, $value);
213+
return sprintf('%s=%s', $option, $this->quoteAnnotationValue($value));
224214
}, array_keys($options), array_values($options));
225215

226216
return sprintf('%s(%s)', $annotationClass, implode(', ', $formattedOptions));
227217
}
228218

219+
private function quoteAnnotationValue($value)
220+
{
221+
if (is_bool($value)) {
222+
return $value ? 'true' : 'false';
223+
}
224+
225+
if (null === $value) {
226+
return 'null';
227+
}
228+
229+
if (is_int($value)) {
230+
return $value;
231+
}
232+
233+
if (is_array($value)) {
234+
throw new \Exception('Invalid value: loop before quoting.');
235+
}
236+
237+
return sprintf('"%s"', $value);
238+
}
239+
229240
private function addSingularRelation(BaseRelation $relation)
230241
{
231242
$typeHint = $this->addUseStatementIfNecessary($relation->getTargetClassName());
@@ -774,8 +785,6 @@ private function getEntityTypeHint($doctrineType)
774785

775786
case 'array':
776787
case 'simple_array':
777-
case 'json_array':
778-
case 'json':
779788
return 'array';
780789

781790
case 'boolean':
@@ -804,6 +813,8 @@ private function getEntityTypeHint($doctrineType)
804813
case 'dateinterval':
805814
return '\DateInterval';
806815

816+
case 'json_array':
817+
case 'json':
807818
case 'object':
808819
case 'decimal':
809820
case 'binary':

tests/Doctrine/fixtures/expected_xml/src/Repository/UserRepository.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ public function findByExampleField($value)
2525
->orderBy('u.id', 'ASC')
2626
->setMaxResults(10)
2727
->getQuery()
28-
->execute()
28+
->getResult()
2929
;
3030
}
3131
*/
3232

3333
/*
34-
public function findOneBySomeField($value): User
34+
public function findOneBySomeField($value): ?User
3535
{
3636
return $this->createQueryBuilder('u')
3737
->andWhere('u.exampleField = :val')

tests/StrTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,19 @@ public function getNamespaceTests()
146146
yield ['App\\Entity\\Foo', 'App\\Entity'];
147147
yield ['DateTime', ''];
148148
}
149+
150+
/**
151+
* @dataProvider getAsCamelCaseTests
152+
*/
153+
public function testAsCamelCase(string $original, string $expected)
154+
{
155+
$this->assertSame($expected, Str::asCamelCase($original));
156+
}
157+
158+
public function getAsCamelCaseTests()
159+
{
160+
yield ['foo', 'Foo'];
161+
162+
yield ['foo_bar.baz\\pizza', 'FooBarBazPizza'];
163+
}
149164
}

0 commit comments

Comments
 (0)