Skip to content

Commit 9c7932e

Browse files
committed
Quality fixes (PHP Inspections)
1 parent 967d791 commit 9c7932e

16 files changed

+60
-70
lines changed

src/Command/MakerCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ protected function configure()
5656
protected function initialize(InputInterface $input, OutputInterface $output)
5757
{
5858
$this->io = new ConsoleStyle($input, $output);
59-
$this->fileManager->setIo($this->io);
59+
$this->fileManager->setIO($this->io);
6060

6161
if ($this->checkDependencies) {
6262
$dependencies = new DependencyBuilder();
@@ -75,7 +75,7 @@ protected function interact(InputInterface $input, OutputInterface $output)
7575
continue;
7676
}
7777

78-
if (in_array($argument->getName(), $this->inputConfig->getNonInteractiveArguments(), true)) {
78+
if (\in_array($argument->getName(), $this->inputConfig->getNonInteractiveArguments(), true)) {
7979
continue;
8080
}
8181

src/DependencyBuilder.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function getMissingPackagesMessage(string $commandName): string
8484
return '';
8585
}
8686

87-
$packagesCount = count($packages) + count($packagesDev);
87+
$packagesCount = \count($packages) + \count($packagesDev);
8888

8989
$message = sprintf(
9090
"Missing package%s: to use the %s command, run:\n",
@@ -103,7 +103,7 @@ public function getMissingPackagesMessage(string $commandName): string
103103
return $message;
104104
}
105105

106-
private function getRequiredDependencyNames(array $dependencies)
106+
private function getRequiredDependencyNames(array $dependencies): array
107107
{
108108
$packages = [];
109109
foreach ($dependencies as $package) {
@@ -116,7 +116,7 @@ private function getRequiredDependencyNames(array $dependencies)
116116
return array_unique($packages);
117117
}
118118

119-
private function calculateMissingDependencies(array $dependencies)
119+
private function calculateMissingDependencies(array $dependencies): array
120120
{
121121
$missingPackages = [];
122122
$missingOptionalPackages = [];

src/Doctrine/DoctrineHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public function getMetadata(string $classOrNamespace = null, bool $disconnected
125125
if (null === $classOrNamespace) {
126126
$metadata[$m->getName()] = $m;
127127
} else {
128-
if ($m->getName() == $classOrNamespace) {
128+
if ($m->getName() === $classOrNamespace) {
129129
return $m;
130130
}
131131

src/Doctrine/EntityRegenerator.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,13 @@ final class EntityRegenerator
2828
private $doctrineHelper;
2929
private $fileManager;
3030
private $generator;
31-
private $projectDirectory;
3231
private $overwrite;
3332

3433
public function __construct(DoctrineHelper $doctrineHelper, FileManager $fileManager, Generator $generator, string $projectDirectory, bool $overwrite)
3534
{
3635
$this->doctrineHelper = $doctrineHelper;
3736
$this->fileManager = $fileManager;
3837
$this->generator = $generator;
39-
$this->projectDirectory = $projectDirectory;
4038
$this->overwrite = $overwrite;
4139
}
4240

@@ -99,7 +97,7 @@ public function regenerateEntities(string $classOrNamespace)
9997
}
10098

10199
$getIsNullable = function (array $mapping) {
102-
if (!isset($mapping['joinColumns'][0]) || !isset($mapping['joinColumns'][0]['nullable'])) {
100+
if (!isset($mapping['joinColumns'][0]['nullable'])) {
103101
// the default for relationships IS nullable
104102
return true;
105103
}

src/Doctrine/EntityRelation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ final class EntityRelation
3939

4040
public function __construct(string $type, string $owningClass, string $inverseClass)
4141
{
42-
if (!in_array($type, self::getValidRelationTypes())) {
42+
if (!\in_array($type, self::getValidRelationTypes())) {
4343
throw new \Exception(sprintf('Invalid relation type "%s"', $type));
4444
}
4545

src/EventRegistry.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public function getEventClassName(string $event)
9696
}
9797

9898
foreach ($listeners as $listener) {
99-
if (!is_array($listener) || 2 !== count($listener)) {
99+
if (!\is_array($listener) || 2 !== \count($listener)) {
100100
continue;
101101
}
102102

src/FileManager.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function __construct(Filesystem $fs, AutoloaderUtil $autoloaderUtil, stri
3636
// update EntityRegeneratorTest to mock the autoloader
3737
$this->fs = $fs;
3838
$this->autoloaderUtil = $autoloaderUtil;
39-
$this->rootDirectory = rtrim($this->realpath($this->normalizeSlashes($rootDirectory)), '/');
39+
$this->rootDirectory = rtrim($this->realPath($this->normalizeSlashes($rootDirectory)), '/');
4040
}
4141

4242
public function setIO(SymfonyStyle $io)
@@ -196,8 +196,7 @@ private function realPath($absolutePath): string
196196
$finalPath = implode('/', $finalParts);
197197
// Normalize: // => /
198198
// Normalize: /./ => /
199-
$finalPath = str_replace('//', '/', $finalPath);
200-
$finalPath = str_replace('/./', '/', $finalPath);
199+
$finalPath = str_replace(['//', '/./'], '/', $finalPath);
201200

202201
return $finalPath;
203202
}

src/Maker/MakeEntity.php

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
9898
continue;
9999
}
100100

101-
$classes[] = str_replace('/', '\\', str_replace('.php', '', $item->getRelativePathname()));
101+
$classes[] = str_replace(['.php', '/'], ['', '\\'], $item->getRelativePathname());
102102
}
103103

104104
$argument = $command->getDefinition()->getArgument('name');
@@ -191,7 +191,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
191191
$fileManagerOperations = [];
192192
$fileManagerOperations[$entityPath] = $manipulator;
193193

194-
if (is_array($newField)) {
194+
if (\is_array($newField)) {
195195
$annotationOptions = $newField;
196196
unset($annotationOptions['fieldName']);
197197
$manipulator->addEntityField($newField['fieldName'], $annotationOptions);
@@ -254,7 +254,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
254254
}
255255

256256
foreach ($fileManagerOperations as $path => $manipulatorOrMessage) {
257-
if (is_string($manipulatorOrMessage)) {
257+
if (\is_string($manipulatorOrMessage)) {
258258
$io->comment($manipulatorOrMessage);
259259
} else {
260260
$this->fileManager->dumpFile($path, $manipulatorOrMessage->getSourceCode());
@@ -300,28 +300,29 @@ private function askForNextField(ConsoleStyle $io, array $fields, string $entity
300300
return $name;
301301
}
302302

303-
if (in_array($name, $fields)) {
303+
if (\in_array($name, $fields)) {
304304
throw new \InvalidArgumentException(sprintf('The "%s" property already exists.', $name));
305305
}
306306

307307
return Validator::validateDoctrineFieldName($name, $this->doctrineHelper->getRegistry());
308308
});
309309

310310
if (!$fieldName) {
311-
return;
311+
return null;
312312
}
313313

314314
$defaultType = 'string';
315315
// try to guess the type by the field name prefix/suffix
316316
// convert to snake case for simplicity
317317
$snakeCasedField = Str::asSnakeCase($fieldName);
318-
if ('_at' == substr($snakeCasedField, -3)) {
318+
319+
if ('_at' === $prefix = substr($snakeCasedField, -3)) {
319320
$defaultType = 'datetime';
320-
} elseif ('_id' == substr($snakeCasedField, -3)) {
321+
} elseif ('_id' === $prefix) {
321322
$defaultType = 'integer';
322-
} elseif ('is_' == substr($snakeCasedField, 0, 3)) {
323+
} elseif (0 === strpos($snakeCasedField, 'is_')) {
323324
$defaultType = 'boolean';
324-
} elseif ('has_' == substr($snakeCasedField, 0, 4)) {
325+
} elseif (0 === strpos($snakeCasedField, 'has_')) {
325326
$defaultType = 'boolean';
326327
}
327328

@@ -341,7 +342,7 @@ private function askForNextField(ConsoleStyle $io, array $fields, string $entity
341342
$io->writeln('');
342343

343344
$type = null;
344-
} elseif (!in_array($type, $allValidTypes)) {
345+
} elseif (!\in_array($type, $allValidTypes)) {
345346
$this->printAvailableTypes($io);
346347
$io->error(sprintf('Invalid type "%s".', $type));
347348
$io->writeln('');
@@ -350,7 +351,7 @@ private function askForNextField(ConsoleStyle $io, array $fields, string $entity
350351
}
351352
}
352353

353-
if ('relation' === $type || in_array($type, EntityRelation::getValidRelationTypes())) {
354+
if ('relation' === $type || \in_array($type, EntityRelation::getValidRelationTypes())) {
354355
return $this->askRelationDetails($io, $entityClass, $type, $fieldName);
355356
}
356357

@@ -414,9 +415,9 @@ private function printAvailableTypes(ConsoleStyle $io)
414415
unset($allTypes[$mainType]);
415416
$line = sprintf(' * <comment>%s</comment>', $mainType);
416417

417-
if (is_string($subTypes) && $subTypes) {
418+
if (\is_string($subTypes) && $subTypes) {
418419
$line .= sprintf(' (%s)', $subTypes);
419-
} elseif (is_array($subTypes) && !empty($subTypes)) {
420+
} elseif (\is_array($subTypes) && !empty($subTypes)) {
420421
$line .= sprintf(' (or %s)', implode(', ', array_map(function ($subType) {
421422
return sprintf('<comment>%s</comment>', $subType);
422423
}, $subTypes)));
@@ -560,7 +561,7 @@ function ($name) use ($targetClass) {
560561
}
561562

562563
// recommend an inverse side, except for OneToOne, where it's inefficient
563-
$recommendMappingInverse = EntityRelation::ONE_TO_ONE === $relation->getType() ? false : true;
564+
$recommendMappingInverse = EntityRelation::ONE_TO_ONE !== $relation->getType();
564565

565566
$getterMethodName = 'get'.Str::asCamelCase(Str::getShortClassName($relation->getOwningClass()));
566567
if (EntityRelation::ONE_TO_ONE !== $relation->getType()) {
@@ -742,7 +743,7 @@ private function askRelationType(ConsoleStyle $io, string $entityClass, string $
742743
));
743744
$question->setAutocompleterValues(EntityRelation::getValidRelationTypes());
744745
$question->setValidator(function ($type) {
745-
if (!in_array($type, EntityRelation::getValidRelationTypes())) {
746+
if (!\in_array($type, EntityRelation::getValidRelationTypes())) {
746747
throw new \InvalidArgumentException(sprintf('Invalid type: use one of: %s', implode(', ', EntityRelation::getValidRelationTypes())));
747748
}
748749

@@ -801,7 +802,7 @@ private function doesEntityUseAnnotationMapping(string $className): bool
801802
return false;
802803
}
803804

804-
$className = (reset($otherClassMetadatas))->getName();
805+
$className = reset($otherClassMetadatas)->getName();
805806
}
806807

807808
$driver = $this->doctrineHelper->getMappingDriverForClass($className);

src/Maker/MakeValidator.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use Symfony\Bundle\MakerBundle\Generator;
1717
use Symfony\Bundle\MakerBundle\InputConfiguration;
1818
use Symfony\Bundle\MakerBundle\Str;
19-
use Symfony\Bundle\MakerBundle\Validator;
2019
use Symfony\Component\Console\Command\Command;
2120
use Symfony\Component\Console\Input\InputArgument;
2221
use Symfony\Component\Console\Input\InputInterface;

src/Str.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ final class Str
2525
*/
2626
public static function hasSuffix(string $value, string $suffix): bool
2727
{
28-
return 0 === strcasecmp($suffix, substr($value, -strlen($suffix)));
28+
return 0 === strcasecmp($suffix, substr($value, -\strlen($suffix)));
2929
}
3030

3131
/**
@@ -45,7 +45,7 @@ public static function addSuffix(string $value, string $suffix): string
4545
*/
4646
public static function removeSuffix(string $value, string $suffix): string
4747
{
48-
return self::hasSuffix($value, $suffix) ? substr($value, 0, -strlen($suffix)) : $value;
48+
return self::hasSuffix($value, $suffix) ? substr($value, 0, -\strlen($suffix)) : $value;
4949
}
5050

5151
/**
@@ -117,11 +117,11 @@ public static function asEventMethod(string $eventName): string
117117

118118
public static function getShortClassName(string $fullClassName): string
119119
{
120-
if (!empty(self::getNamespace($fullClassName))) {
121-
return substr($fullClassName, strrpos($fullClassName, '\\') + 1);
122-
} else {
120+
if (empty(self::getNamespace($fullClassName))) {
123121
return $fullClassName;
124122
}
123+
124+
return substr($fullClassName, strrpos($fullClassName, '\\') + 1);
125125
}
126126

127127
public static function getNamespace(string $fullClassName): string
@@ -141,7 +141,7 @@ public static function singularCamelCaseToPluralCamelCase(string $camelCase): st
141141
{
142142
$snake = self::asSnakeCase($camelCase);
143143
$words = explode('_', $snake);
144-
$words[count($words) - 1] = Inflector::pluralize($words[count($words) - 1]);
144+
$words[\count($words) - 1] = Inflector::pluralize($words[\count($words) - 1]);
145145
$reSnaked = implode('_', $words);
146146

147147
return self::asLowerCamelCase($reSnaked);
@@ -151,7 +151,7 @@ public static function pluralCamelCaseToSingular(string $camelCase): string
151151
{
152152
$snake = self::asSnakeCase($camelCase);
153153
$words = explode('_', $snake);
154-
$words[count($words) - 1] = Inflector::singularize($words[count($words) - 1]);
154+
$words[\count($words) - 1] = Inflector::singularize($words[\count($words) - 1]);
155155
$reSnaked = implode('_', $words);
156156

157157
return self::asLowerCamelCase($reSnaked);

src/Test/MakerTestCase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ protected function executeMakerCommand(MakerTestDetails $testDetails)
3434
foreach ($files as $file) {
3535
$this->assertTrue($testEnv->fileExists($file));
3636

37-
if ('.php' == substr($file, -4)) {
37+
if ('.php' === substr($file, -4)) {
3838
$csProcess = $testEnv->runPhpCSFixer($file);
3939

4040
$this->assertTrue($csProcess->isSuccessful(), sprintf('File "%s" has a php-cs problem: %s', $file, $csProcess->getOutput()));
4141
}
4242

43-
if ('.twig' == substr($file, -5)) {
43+
if ('.twig' === substr($file, -5)) {
4444
$csProcess = $testEnv->runTwigCSLint($file);
4545

4646
$this->assertTrue($csProcess->isSuccessful(), sprintf('File "%s" has a twig-cs problem: %s', $file, $csProcess->getOutput()));

src/Test/MakerTestEnvironment.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public function runMaker()
142142

143143
// We don't need ansi coloring in tests!
144144
$testProcess = MakerTestProcess::create(
145-
sprintf('php bin/console %s %s --no-ansi', ($this->testDetails->getMaker())::getCommandName(), $this->testDetails->getArgumentsString()),
145+
sprintf('php bin/console %s %s --no-ansi', $this->testDetails->getMaker()::getCommandName(), $this->testDetails->getArgumentsString()),
146146
$this->path,
147147
10
148148
);
@@ -184,9 +184,7 @@ public function getGeneratedFilesFromOutputText()
184184

185185
preg_match_all('#(created|updated): (.*)\n#iu', $output, $matches, PREG_PATTERN_ORDER);
186186

187-
$files = array_map('trim', $matches[2]);
188-
189-
return $files;
187+
return array_map('trim', $matches[2]);
190188
}
191189

192190
public function fileExists(string $file)
@@ -237,7 +235,7 @@ public function reset()
237235

238236
$diff = array_diff($currentSnapshot, $cleanSnapshot);
239237

240-
if (count($diff)) {
238+
if (\count($diff)) {
241239
$this->fs->remove($diff);
242240
}
243241

src/Test/MakerTestProcess.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function run($allowToFail = false): self
5151
{
5252
$this->process->run();
5353

54-
if (!$this->process->isSuccessful() && !$allowToFail) {
54+
if (!$allowToFail && !$this->process->isSuccessful()) {
5555
throw new \Exception(sprintf(
5656
'Error running command: "%s". Output: "%s". Error: "%s"',
5757
$this->process->getCommandLine(),

src/Util/AutoloaderUtil.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,13 @@ private function getClassLoader(): ClassLoader
7777
if (null === $this->classLoader) {
7878
$autoloadFunctions = spl_autoload_functions();
7979
foreach ($autoloadFunctions as $autoloader) {
80-
if (is_array($autoloader) && isset($autoloader[0]) && is_object($autoloader[0])) {
80+
if (\is_array($autoloader) && isset($autoloader[0]) && \is_object($autoloader[0])) {
8181
if ($autoloader[0] instanceof ClassLoader) {
8282
$this->classLoader = $autoloader[0];
8383
break;
8484
}
8585
if ($autoloader[0] instanceof DebugClassLoader
86-
&& is_array($autoloader[0]->getClassLoader())
86+
&& \is_array($autoloader[0]->getClassLoader())
8787
&& $autoloader[0]->getClassLoader()[0] instanceof ClassLoader) {
8888
$this->classLoader = $autoloader[0]->getClassLoader()[0];
8989
break;

0 commit comments

Comments
 (0)