Skip to content

Commit

Permalink
Fixes thanks to Stof!
Browse files Browse the repository at this point in the history
  • Loading branch information
weaverryan committed Feb 27, 2018
1 parent 6c914fa commit c79b73c
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 32 deletions.
1 change: 1 addition & 0 deletions src/Doctrine/DoctrineMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* Simpler version of DoctrineBundle's DisconnectedMetadataFactory, to
* avoid PSR-4 issues.
*
* @internal
* @author Fabien Potencier <fabien@symfony.com>
* @author Ryan Weaver <ryan@knpuniversity.com>
*/
Expand Down
27 changes: 18 additions & 9 deletions src/Maker/MakeEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\DBAL\Types\Type;
use Doctrine\ORM\Mapping\Column;
use Psr\Container\ContainerInterface;
use Symfony\Bundle\MakerBundle\ConsoleStyle;
use Symfony\Bundle\MakerBundle\DependencyBuilder;
Expand Down Expand Up @@ -262,10 +263,17 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen

public function configureDependencies(DependencyBuilder $dependencies)
{
// guarantee DoctrineBundle
$dependencies->addClassDependency(
DoctrineBundle::class,
'orm'
);

// guarantee ORM
$dependencies->addClassDependency(
Column::class,
'orm'
);
}

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

$defaultType = 'string';
// try to guess the type by the field name prefix/suffix
// convert to camel case for simplicity
$camelCasedField = Str::snakeCase($fieldName);
if ('_at' == substr($camelCasedField, -3)) {
// convert to snake case for simplicity
$snakeCasedField = Str::asSnakeCase($fieldName);
if ('_at' == substr($snakeCasedField, -3)) {
$defaultType = 'datetime';
} elseif ('_id' == substr($camelCasedField, -3)) {
} elseif ('_id' == substr($snakeCasedField, -3)) {
$defaultType = 'integer';
} elseif ('is_' == substr($camelCasedField, 0, 3)) {
} elseif ('is_' == substr($snakeCasedField, 0, 3)) {
$defaultType = 'boolean';
} elseif ('has_' == substr($camelCasedField, 0, 4)) {
} elseif ('has_' == substr($snakeCasedField, 0, 4)) {
$defaultType = 'boolean';
}

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

$askOrphanRemoval = function (string $owningClass, string $inverseClass) use ($io) {
$io->text([
'Do you want to activate <comment>orphanRemoval</comment> on your relationship?',
sprintf(
'A <comment>%s</comment> becomes "orphaned" if it is removed from its related <comment>%s</comment>.',
'A <comment>%s</comment> is "orphaned" when it is removed from its related <comment>%s</comment>.',
Str::getShortClassName($owningClass),
Str::getShortClassName($inverseClass)
),
sprintf(
'For example: <comment>$%s->remove%s($%s)</comment>',
'e.g. <comment>$%s->remove%s($%s)</comment>',
Str::asLowerCamelCase(Str::getShortClassName($inverseClass)),
Str::asCamelCase(Str::getShortClassName($owningClass)),
Str::asLowerCamelCase(Str::getShortClassName($owningClass))
),
'',
sprintf(
'NOTE: If a <comment>%s</comment> should be allowed to *change* from one <comment>%s</comment> to another, answer "no".',
'NOTE: If a <comment>%s</comment> may *change* from one <comment>%s</comment> to another, answer "no".',
Str::getShortClassName($owningClass),
Str::getShortClassName($inverseClass)
),
Expand Down
4 changes: 2 additions & 2 deletions src/Resources/skeleton/doctrine/Repository.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ public function findByExampleField($value)
->orderBy('<?= $entity_alias; ?>.id', 'ASC')
->setMaxResults(10)
->getQuery()
->execute()
->getResult()
;
}
*/

/*
public function findOneBySomeField($value): <?= $entity_class_name."\n" ?>
public function findOneBySomeField($value): ?<?= $entity_class_name."\n" ?>
{
return $this->createQueryBuilder('<?= $entity_alias ?>')
->andWhere('<?= $entity_alias ?>.exampleField = :val')
Expand Down
8 changes: 4 additions & 4 deletions src/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public static function asLowerCamelCase(string $str): string

public static function asCamelCase(string $str): string
{
return strtr(ucwords(strtr($str, ['_' => ' ', '.' => '_ ', '\\' => '_ '])), [' ' => '']);
return strtr(ucwords(strtr($str, ['_' => ' ', '.' => ' ', '\\' => ' '])), [' ' => '']);
}

public static function asRoutePath(string $value): string
Expand All @@ -100,7 +100,7 @@ public static function asRouteName(string $value): string
return self::asTwigVariable($value);
}

public static function snakeCase(string $value): string
public static function asSnakeCase(string $value): string
{
return self::asTwigVariable($value);
}
Expand Down Expand Up @@ -145,7 +145,7 @@ public static function getNamespace(string $fullClassName): string

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

public static function pluralCamelCaseToSingular(string $camelCase): string
{
$snake = self::snakeCase($camelCase);
$snake = self::asSnakeCase($camelCase);
$words = explode('_', $snake);
$words[count($words) - 1] = Inflector::singularize($words[count($words) - 1]);
$reSnaked = implode('_', $words);
Expand Down
41 changes: 26 additions & 15 deletions src/Util/ClassSourceManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,33 +199,44 @@ private function createSetterNodeBuilder(string $propertyName, ?string $type, bo
private function buildAnnotationLine(string $annotationClass, array $options)
{
$formattedOptions = array_map(function ($option, $value) {
$quoteValue = !(is_bool($value) || is_int($value) || is_array($value));

if (is_bool($value)) {
$value = $value ? 'true' : 'false';
}

if (is_array($value)) {
if (!isset($value[0])) {
// associative array: we'll add this if/when we need it
throw new \Exception('Not currently supported');
}

$value = sprintf('{%s}', implode(', ', array_map(function ($val) {
return sprintf('"%s"', $val);
return sprintf('%s={%s}', $option, implode(', ', array_map(function ($val) {
return $this->quoteAnnotationValue($val);
}, $value)));
}

if ($quoteValue) {
return sprintf('%s="%s"', $option, $value);
}

return sprintf('%s=%s', $option, $value);
return sprintf('%s=%s', $option, $this->quoteAnnotationValue($value));
}, array_keys($options), array_values($options));

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

private function quoteAnnotationValue($value)
{
if (is_bool($value)) {
return $value ? 'true' : 'false';
}

if (null === $value) {
return 'null';
}

if (is_int($value)) {
return $value;
}

if (is_array($value)) {
throw new \Exception('Invalid value: loop before quoting.');
}

return sprintf('"%s"', $value);
}

private function addSingularRelation(BaseRelation $relation)
{
$typeHint = $this->addUseStatementIfNecessary($relation->getTargetClassName());
Expand Down Expand Up @@ -774,8 +785,6 @@ private function getEntityTypeHint($doctrineType)

case 'array':
case 'simple_array':
case 'json_array':
case 'json':
return 'array';

case 'boolean':
Expand Down Expand Up @@ -804,6 +813,8 @@ private function getEntityTypeHint($doctrineType)
case 'dateinterval':
return '\DateInterval';

case 'json_array':
case 'json':
case 'object':
case 'decimal':
case 'binary':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ public function findByExampleField($value)
->orderBy('u.id', 'ASC')
->setMaxResults(10)
->getQuery()
->execute()
->getResult()
;
}
*/

/*
public function findOneBySomeField($value): User
public function findOneBySomeField($value): ?User
{
return $this->createQueryBuilder('u')
->andWhere('u.exampleField = :val')
Expand Down
15 changes: 15 additions & 0 deletions tests/StrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,19 @@ public function getNamespaceTests()
yield ['App\\Entity\\Foo', 'App\\Entity'];
yield ['DateTime', ''];
}

/**
* @dataProvider getAsCamelCaseTests
*/
public function testAsCamelCase(string $original, string $expected)
{
$this->assertSame($expected, Str::asCamelCase($original));
}

public function getAsCamelCaseTests()
{
yield ['foo', 'Foo'];

yield ['foo_bar.baz\\pizza', 'FooBarBazPizza'];
}
}

0 comments on commit c79b73c

Please sign in to comment.