Skip to content

Commit 91bc781

Browse files
committed
better typing
1 parent 37676b0 commit 91bc781

File tree

6 files changed

+32
-35
lines changed

6 files changed

+32
-35
lines changed

src/FileManager.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use Symfony\Bundle\MakerBundle\Util\MakerFileLinkFormatter;
1616
use Symfony\Component\Console\Style\SymfonyStyle;
1717
use Symfony\Component\Filesystem\Filesystem;
18-
use Symfony\Component\Finder\Finder;
1918

2019
/**
2120
* @author Javier Eguiluz <[email protected]>

src/Generator.php

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,23 @@
2323
*/
2424
class Generator
2525
{
26-
private $fileManager;
27-
private $twigHelper;
28-
private $pendingOperations = [];
29-
private $namespacePrefix;
30-
private $phpCompatUtil;
31-
private $templateComponentGenerator;
32-
33-
public function __construct(FileManager $fileManager, string $namespacePrefix, PhpCompatUtil $phpCompatUtil = null, TemplateComponentGenerator $templateComponentGenerator = null)
34-
{
35-
$this->fileManager = $fileManager;
26+
private GeneratorTwigHelper $twigHelper;
27+
private array $pendingOperations = [];
28+
private ?TemplateComponentGenerator $templateComponentGenerator;
29+
30+
public function __construct(
31+
private FileManager $fileManager,
32+
private string $namespacePrefix,
33+
PhpCompatUtil $phpCompatUtil = null,
34+
TemplateComponentGenerator $templateComponentGenerator = null,
35+
) {
3636
$this->twigHelper = new GeneratorTwigHelper($fileManager);
3737
$this->namespacePrefix = trim($namespacePrefix, '\\');
3838

3939
if (null === $phpCompatUtil) {
40-
$phpCompatUtil = new PhpCompatUtil($fileManager);
41-
42-
trigger_deprecation('symfony/maker-bundle', '1.25', 'Initializing Generator without providing an instance of PhpCompatUtil is deprecated.');
40+
trigger_deprecation('symfony/maker-bundle', '1.25', 'Initializing Generator while providing an instance of PhpCompatUtil is deprecated.');
4341
}
4442

45-
$this->phpCompatUtil = $phpCompatUtil;
4643
$this->templateComponentGenerator = $templateComponentGenerator;
4744
}
4845

@@ -151,7 +148,7 @@ public function createClassNameDetails(string $name, string $namespacePrefix, st
151148

152149
// if this is a custom class, we may be completely different than the namespace prefix
153150
// the best way can do, is find the PSR4 prefix and use that
154-
if (0 !== strpos($className, $fullNamespacePrefix)) {
151+
if (!str_starts_with($className, $fullNamespacePrefix)) {
155152
$fullNamespacePrefix = $this->fileManager->getNamespacePrefixForClass($className);
156153
}
157154

@@ -163,7 +160,7 @@ public function getRootDirectory(): string
163160
return $this->fileManager->getRootDirectory();
164161
}
165162

166-
private function addOperation(string $targetPath, string $templateName, array $variables)
163+
private function addOperation(string $targetPath, string $templateName, array $variables): void
167164
{
168165
if ($this->fileManager->fileExists($targetPath)) {
169166
throw new RuntimeCommandException(sprintf('The file "%s" can\'t be generated because it already exists.', $this->fileManager->relativizePath($targetPath)));
@@ -193,6 +190,8 @@ public function hasPendingOperations(): bool
193190

194191
/**
195192
* Actually writes and file changes that are pending.
193+
*
194+
* @return void
196195
*/
197196
public function writeChanges()
198197
{
@@ -231,6 +230,8 @@ public function generateController(string $controllerClassName, string $controll
231230

232231
/**
233232
* Generate a template file.
233+
*
234+
* @return void
234235
*/
235236
public function generateTemplate(string $targetPath, string $templateName, array $variables = [])
236237
{

src/GeneratorTwigHelper.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,14 @@
1616
*/
1717
final class GeneratorTwigHelper
1818
{
19-
private $fileManager;
20-
21-
public function __construct(FileManager $fileManager)
22-
{
23-
$this->fileManager = $fileManager;
19+
public function __construct(
20+
private FileManager $fileManager,
21+
) {
2422
}
2523

2624
public function getEntityFieldPrintCode($entity, $field): string
2725
{
28-
$twigField = preg_replace_callback('/(?!^)_([a-z0-9])/', function ($s) {
26+
$twigField = preg_replace_callback('/(?!^)_([a-z0-9])/', static function ($s) {
2927
return strtoupper($s[1]);
3028
}, $field['fieldName']);
3129
$printCode = $entity.'.'.str_replace('_', '', $twigField);

src/InputConfiguration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
final class InputConfiguration
1515
{
16-
private $nonInteractiveArguments = [];
16+
private array $nonInteractiveArguments = [];
1717

1818
/**
1919
* Call in MakerInterface::configureCommand() to disable the automatic interactive

src/Str.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
*/
2222
final class Str
2323
{
24-
/** @var Inflector|null */
25-
private static $inflector;
24+
private static ?Inflector $inflector = null;
2625

2726
/**
2827
* Looks for suffixes in strings in a case-insensitive way.
@@ -203,6 +202,9 @@ public static function isValidPhpVariableName($name)
203202
return (bool) preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $name, $matches);
204203
}
205204

205+
/**
206+
* @return bool
207+
*/
206208
public static function areClassesAlphabetical(string $class1, string $class2)
207209
{
208210
$arr1 = [$class1, $class2];

src/Validator.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -146,20 +146,17 @@ public static function validateBoolean($value)
146146
return $valueAsBool;
147147
}
148148

149-
public static function validatePropertyName(string $name)
149+
public static function validatePropertyName(string $name): string
150150
{
151151
// check for valid PHP variable name
152-
if (null !== $name && !Str::isValidPhpVariableName($name)) {
152+
if (!Str::isValidPhpVariableName($name)) {
153153
throw new \InvalidArgumentException(sprintf('"%s" is not a valid PHP property name.', $name));
154154
}
155155

156156
return $name;
157157
}
158158

159-
/**
160-
* @param ManagerRegistry|LegacyManagerRegistry $registry
161-
*/
162-
public static function validateDoctrineFieldName(string $name, $registry)
159+
public static function validateDoctrineFieldName(string $name, ManagerRegistry|LegacyManagerRegistry $registry): string
163160
{
164161
if (!$registry instanceof ManagerRegistry && !$registry instanceof LegacyManagerRegistry) {
165162
throw new \InvalidArgumentException(sprintf('Argument 2 to %s::validateDoctrineFieldName must be an instance of %s, %s passed.', __CLASS__, ManagerRegistry::class, \is_object($registry) ? \get_class($registry) : \gettype($registry)));
@@ -184,12 +181,12 @@ public static function validateEmailAddress(?string $email): string
184181
return $email;
185182
}
186183

187-
public static function existsOrNull(string $className = null, array $entities = [])
184+
public static function existsOrNull(?string $className = null, array $entities = []): ?string
188185
{
189186
if (null !== $className) {
190187
self::validateClassName($className);
191188

192-
if (0 === strpos($className, '\\')) {
189+
if (str_starts_with($className, '\\')) {
193190
self::classExists($className);
194191
} else {
195192
self::entityExists($className, $entities);
@@ -220,7 +217,7 @@ public static function entityExists(string $className = null, array $entities =
220217
throw new RuntimeCommandException('There are no registered entities; please create an entity before using this command.');
221218
}
222219

223-
if (0 === strpos($className, '\\')) {
220+
if (str_starts_with($className, '\\')) {
224221
self::classExists($className, sprintf('Entity "%s" doesn\'t exist; please enter an existing one or create a new one.', $className));
225222
}
226223

0 commit comments

Comments
 (0)