Skip to content

Commit ae20d0e

Browse files
committed
add utility return types
1 parent 1004e8d commit ae20d0e

File tree

4 files changed

+45
-66
lines changed

4 files changed

+45
-66
lines changed

src/Util/AutoloaderUtil.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,9 @@ public function __construct(ComposerAutoloaderFinder $autoloaderFinder)
3333
/**
3434
* Returns the relative path to where a new class should live.
3535
*
36-
* @return string|null
37-
*
3836
* @throws \Exception
3937
*/
40-
public function getPathForFutureClass(string $className)
38+
public function getPathForFutureClass(string $className): ?string
4139
{
4240
$classLoader = $this->getClassLoader();
4341

src/Util/ClassSourceManipulator.php

Lines changed: 40 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function __construct(string $sourceCode, bool $overwrite = false, bool $u
7373
$this->setSourceCode($sourceCode);
7474
}
7575

76-
public function setIo(ConsoleStyle $io)
76+
public function setIo(ConsoleStyle $io): void
7777
{
7878
$this->io = $io;
7979
}
@@ -83,7 +83,7 @@ public function getSourceCode(): string
8383
return $this->sourceCode;
8484
}
8585

86-
public function addEntityField(string $propertyName, array $columnOptions, array $comments = [])
86+
public function addEntityField(string $propertyName, array $columnOptions, array $comments = []): void
8787
{
8888
$typeHint = $this->getEntityTypeHint($columnOptions['type']);
8989
$nullable = $columnOptions['nullable'] ?? false;
@@ -110,7 +110,7 @@ public function addEntityField(string $propertyName, array $columnOptions, array
110110
}
111111
}
112112

113-
public function addEmbeddedEntity(string $propertyName, string $className)
113+
public function addEmbeddedEntity(string $propertyName, string $className): void
114114
{
115115
$typeHint = $this->addUseStatementIfNecessary($className);
116116

@@ -150,27 +150,27 @@ public function addEmbeddedEntity(string $propertyName, string $className)
150150
$this->addSetter($propertyName, $typeHint, false);
151151
}
152152

153-
public function addManyToOneRelation(RelationManyToOne $manyToOne)
153+
public function addManyToOneRelation(RelationManyToOne $manyToOne): void
154154
{
155155
$this->addSingularRelation($manyToOne);
156156
}
157157

158-
public function addOneToOneRelation(RelationOneToOne $oneToOne)
158+
public function addOneToOneRelation(RelationOneToOne $oneToOne): void
159159
{
160160
$this->addSingularRelation($oneToOne);
161161
}
162162

163-
public function addOneToManyRelation(RelationOneToMany $oneToMany)
163+
public function addOneToManyRelation(RelationOneToMany $oneToMany): void
164164
{
165165
$this->addCollectionRelation($oneToMany);
166166
}
167167

168-
public function addManyToManyRelation(RelationManyToMany $manyToMany)
168+
public function addManyToManyRelation(RelationManyToMany $manyToMany): void
169169
{
170170
$this->addCollectionRelation($manyToMany);
171171
}
172172

173-
public function addInterface(string $interfaceName)
173+
public function addInterface(string $interfaceName): void
174174
{
175175
$this->addUseStatementIfNecessary($interfaceName);
176176

@@ -181,7 +181,7 @@ public function addInterface(string $interfaceName)
181181
/**
182182
* @param string $trait the fully-qualified trait name
183183
*/
184-
public function addTrait(string $trait)
184+
public function addTrait(string $trait): void
185185
{
186186
$importedClassName = $this->addUseStatementIfNecessary($trait);
187187

@@ -217,19 +217,19 @@ public function addTrait(string $trait)
217217
$this->updateSourceCodeFromNewStmts();
218218
}
219219

220-
public function addAccessorMethod(string $propertyName, string $methodName, $returnType, bool $isReturnTypeNullable, array $commentLines = [], $typeCast = null)
220+
public function addAccessorMethod(string $propertyName, string $methodName, $returnType, bool $isReturnTypeNullable, array $commentLines = [], $typeCast = null): void
221221
{
222222
$this->addCustomGetter($propertyName, $methodName, $returnType, $isReturnTypeNullable, $commentLines, $typeCast);
223223
}
224224

225-
public function addGetter(string $propertyName, $returnType, bool $isReturnTypeNullable, array $commentLines = [])
225+
public function addGetter(string $propertyName, $returnType, bool $isReturnTypeNullable, array $commentLines = []): void
226226
{
227227
$methodName = 'get'.Str::asCamelCase($propertyName);
228228

229229
$this->addCustomGetter($propertyName, $methodName, $returnType, $isReturnTypeNullable, $commentLines);
230230
}
231231

232-
public function addSetter(string $propertyName, $type, bool $isNullable, array $commentLines = [])
232+
public function addSetter(string $propertyName, $type, bool $isNullable, array $commentLines = []): void
233233
{
234234
$builder = $this->createSetterNodeBuilder($propertyName, $type, $isNullable, $commentLines);
235235
$builder->addStmt(
@@ -245,7 +245,7 @@ public function addSetter(string $propertyName, $type, bool $isNullable, array $
245245
/**
246246
* @param Node[] $params
247247
*/
248-
public function addConstructor(array $params, string $methodBody)
248+
public function addConstructor(array $params, string $methodBody): void
249249
{
250250
if (null !== $this->getConstructorNode()) {
251251
throw new \LogicException('Constructor already exists.');
@@ -264,7 +264,7 @@ public function addConstructor(array $params, string $methodBody)
264264
/**
265265
* @param Node[] $params
266266
*/
267-
public function addMethodBuilder(Builder\Method $methodBuilder, array $params = [], string $methodBody = null)
267+
public function addMethodBuilder(Builder\Method $methodBuilder, array $params = [], string $methodBody = null): void
268268
{
269269
$this->addMethodParams($methodBuilder, $params);
270270

@@ -275,7 +275,7 @@ public function addMethodBuilder(Builder\Method $methodBuilder, array $params =
275275
$this->addMethod($methodBuilder->getNode());
276276
}
277277

278-
public function addMethodBody(Builder\Method $methodBuilder, string $methodBody)
278+
public function addMethodBody(Builder\Method $methodBuilder, string $methodBody): void
279279
{
280280
$nodes = $this->parser->parse($methodBody);
281281
$methodBuilder->addStmts($nodes);
@@ -311,7 +311,7 @@ public function createMethodLevelBlankLine()
311311
return $this->createBlankLineNode(self::CONTEXT_CLASS_METHOD);
312312
}
313313

314-
public function addProperty(string $name, array $annotationLines = [], $defaultValue = null)
314+
public function addProperty(string $name, array $annotationLines = [], $defaultValue = null): void
315315
{
316316
if ($this->propertyExists($name)) {
317317
// we never overwrite properties
@@ -331,7 +331,7 @@ public function addProperty(string $name, array $annotationLines = [], $defaultV
331331
$this->addNodeAfterProperties($newPropertyNode);
332332
}
333333

334-
public function addAnnotationToClass(string $annotationClass, array $options)
334+
public function addAnnotationToClass(string $annotationClass, array $options): void
335335
{
336336
$annotationClassAlias = $this->addUseStatementIfNecessary($annotationClass);
337337
$docComment = $this->getClassNode()->getDocComment();
@@ -369,7 +369,7 @@ public function addAnnotationToClass(string $annotationClass, array $options)
369369
$this->updateSourceCodeFromNewStmts();
370370
}
371371

372-
private function addCustomGetter(string $propertyName, string $methodName, $returnType, bool $isReturnTypeNullable, array $commentLines = [], $typeCast = null)
372+
private function addCustomGetter(string $propertyName, string $methodName, $returnType, bool $isReturnTypeNullable, array $commentLines = [], $typeCast = null): void
373373
{
374374
$propertyFetch = new Node\Expr\PropertyFetch(new Node\Expr\Variable('this'), $propertyName);
375375

@@ -402,7 +402,7 @@ private function addCustomGetter(string $propertyName, string $methodName, $retu
402402
$this->addMethod($getterNodeBuilder->getNode());
403403
}
404404

405-
private function createSetterNodeBuilder(string $propertyName, $type, bool $isNullable, array $commentLines = [])
405+
private function createSetterNodeBuilder(string $propertyName, $type, bool $isNullable, array $commentLines = []): Builder\Method
406406
{
407407
$methodName = 'set'.Str::asCamelCase($propertyName);
408408
$setterNodeBuilder = (new Builder\Method($methodName))->makePublic();
@@ -423,10 +423,8 @@ private function createSetterNodeBuilder(string $propertyName, $type, bool $isNu
423423
/**
424424
* @param string $annotationClass The annotation: e.g. "@ORM\Column"
425425
* @param array $options Key-value pair of options for the annotation
426-
*
427-
* @return string
428426
*/
429-
private function buildAnnotationLine(string $annotationClass, array $options)
427+
private function buildAnnotationLine(string $annotationClass, array $options): string
430428
{
431429
$formattedOptions = array_map(function ($option, $value) {
432430
if (\is_array($value)) {
@@ -472,7 +470,7 @@ private function quoteAnnotationValue($value)
472470
return sprintf('"%s"', $value);
473471
}
474472

475-
private function addSingularRelation(BaseRelation $relation)
473+
private function addSingularRelation(BaseRelation $relation): void
476474
{
477475
$typeHint = $this->addUseStatementIfNecessary($relation->getTargetClassName());
478476
if ($relation->getTargetClassName() == $this->getThisFullClassName()) {
@@ -550,7 +548,7 @@ private function addSingularRelation(BaseRelation $relation)
550548
$this->addMethod($setterNodeBuilder->getNode());
551549
}
552550

553-
private function addCollectionRelation(BaseCollectionRelation $relation)
551+
private function addCollectionRelation(BaseCollectionRelation $relation): void
554552
{
555553
$typeHint = $relation->isSelfReferencing() ? 'self' : $this->addUseStatementIfNecessary($relation->getTargetClassName());
556554

@@ -730,7 +728,7 @@ private function addCollectionRelation(BaseCollectionRelation $relation)
730728
$this->addMethod($removerNodeBuilder->getNode());
731729
}
732730

733-
private function addStatementToConstructor(Node\Stmt $stmt)
731+
private function addStatementToConstructor(Node\Stmt $stmt): void
734732
{
735733
if (!$this->getConstructorNode()) {
736734
$constructorNode = (new Builder\Method('__construct'))->makePublic()->getNode();
@@ -756,11 +754,9 @@ private function addStatementToConstructor(Node\Stmt $stmt)
756754
}
757755

758756
/**
759-
* @return Node\Stmt\ClassMethod|null
760-
*
761757
* @throws \Exception
762758
*/
763-
private function getConstructorNode()
759+
private function getConstructorNode(): ?Node\Stmt\ClassMethod
764760
{
765761
foreach ($this->getClassNode()->stmts as $classNode) {
766762
if ($classNode instanceof Node\Stmt\ClassMethod && '__construct' == $classNode->name) {
@@ -849,7 +845,7 @@ public function addUseStatementIfNecessary(string $class): string
849845
return $shortClassName;
850846
}
851847

852-
private function updateSourceCodeFromNewStmts()
848+
private function updateSourceCodeFromNewStmts(): void
853849
{
854850
$newCode = $this->printer->printFormatPreserving(
855851
$this->newStmts,
@@ -879,7 +875,7 @@ private function updateSourceCodeFromNewStmts()
879875
$this->setSourceCode($newCode);
880876
}
881877

882-
private function setSourceCode(string $sourceCode)
878+
private function setSourceCode(string $sourceCode): void
883879
{
884880
$this->sourceCode = $sourceCode;
885881
$this->oldStmts = $this->parser->parse($sourceCode);
@@ -919,10 +915,7 @@ private function getNamespaceNode(): Node\Stmt\Namespace_
919915
return $node;
920916
}
921917

922-
/**
923-
* @return Node|null
924-
*/
925-
private function findFirstNode(callable $filterCallback)
918+
private function findFirstNode(callable $filterCallback): ?Node
926919
{
927920
$traverser = new NodeTraverser();
928921
$visitor = new NodeVisitor\FirstFindingVisitor($filterCallback);
@@ -932,10 +925,7 @@ private function findFirstNode(callable $filterCallback)
932925
return $visitor->getFoundNode();
933926
}
934927

935-
/**
936-
* @return Node|null
937-
*/
938-
private function findLastNode(callable $filterCallback, array $ast)
928+
private function findLastNode(callable $filterCallback, array $ast): ?Node
939929
{
940930
$traverser = new NodeTraverser();
941931
$visitor = new NodeVisitor\FindingVisitor($filterCallback);
@@ -980,7 +970,7 @@ private function createBlankLineNode(string $context)
980970
}
981971
}
982972

983-
private function createSingleLineCommentNode(string $comment, string $context)
973+
private function createSingleLineCommentNode(string $comment, string $context): Node\Stmt
984974
{
985975
$this->pendingComments[] = $comment;
986976
switch ($context) {
@@ -997,7 +987,7 @@ private function createSingleLineCommentNode(string $comment, string $context)
997987
}
998988
}
999989

1000-
private function createDocBlock(array $commentLines)
990+
private function createDocBlock(array $commentLines): string
1001991
{
1002992
$docBlock = "/**\n";
1003993
foreach ($commentLines as $commentLine) {
@@ -1013,7 +1003,7 @@ private function createDocBlock(array $commentLines)
10131003
return $docBlock;
10141004
}
10151005

1016-
private function addMethod(Node\Stmt\ClassMethod $methodNode)
1006+
private function addMethod(Node\Stmt\ClassMethod $methodNode): void
10171007
{
10181008
$classNode = $this->getClassNode();
10191009
$methodName = $methodNode->name;
@@ -1058,7 +1048,7 @@ private function addMethod(Node\Stmt\ClassMethod $methodNode)
10581048
$this->updateSourceCodeFromNewStmts();
10591049
}
10601050

1061-
private function makeMethodFluent(Builder\Method $methodBuilder)
1051+
private function makeMethodFluent(Builder\Method $methodBuilder): void
10621052
{
10631053
if (!$this->fluentMutators) {
10641054
return;
@@ -1071,7 +1061,7 @@ private function makeMethodFluent(Builder\Method $methodBuilder)
10711061
$methodBuilder->setReturnType('self');
10721062
}
10731063

1074-
private function getEntityTypeHint($doctrineType)
1064+
private function getEntityTypeHint($doctrineType): ?string
10751065
{
10761066
switch ($doctrineType) {
10771067
case 'string':
@@ -1120,7 +1110,7 @@ private function getEntityTypeHint($doctrineType)
11201110
}
11211111
}
11221112

1123-
private function isInSameNamespace($class)
1113+
private function isInSameNamespace($class): bool
11241114
{
11251115
$namespace = substr($class, 0, strrpos($class, '\\'));
11261116

@@ -1137,7 +1127,7 @@ private function getThisFullClassName(): string
11371127
*
11381128
* Useful for adding properties, or adding a constructor.
11391129
*/
1140-
private function addNodeAfterProperties(Node $newNode)
1130+
private function addNodeAfterProperties(Node $newNode): void
11411131
{
11421132
$classNode = $this->getClassNode();
11431133

@@ -1185,12 +1175,12 @@ private function addNodeAfterProperties(Node $newNode)
11851175
$this->updateSourceCodeFromNewStmts();
11861176
}
11871177

1188-
private function createNullConstant()
1178+
private function createNullConstant(): Node\Expr\ConstFetch
11891179
{
11901180
return new Node\Expr\ConstFetch(new Node\Name('null'));
11911181
}
11921182

1193-
private function addNodesToSetOtherSideOfOneToOne(RelationOneToOne $relation, Builder\Method $setterNodeBuilder)
1183+
private function addNodesToSetOtherSideOfOneToOne(RelationOneToOne $relation, Builder\Method $setterNodeBuilder): void
11941184
{
11951185
if (!$relation->isNullable()) {
11961186
$setterNodeBuilder->addStmt($this->createSingleLineCommentNode(
@@ -1302,7 +1292,7 @@ private function getMethodIndex(string $methodName)
13021292
return false;
13031293
}
13041294

1305-
private function propertyExists(string $propertyName)
1295+
private function propertyExists(string $propertyName): bool
13061296
{
13071297
foreach ($this->getClassNode()->stmts as $i => $node) {
13081298
if ($node instanceof Node\Stmt\Property && $node->props[0]->name->toString() === $propertyName) {
@@ -1313,14 +1303,14 @@ private function propertyExists(string $propertyName)
13131303
return false;
13141304
}
13151305

1316-
private function writeNote(string $note)
1306+
private function writeNote(string $note): void
13171307
{
13181308
if (null !== $this->io) {
13191309
$this->io->text($note);
13201310
}
13211311
}
13221312

1323-
private function addMethodParams(Builder\Method $methodBuilder, array $params)
1313+
private function addMethodParams(Builder\Method $methodBuilder, array $params): void
13241314
{
13251315
foreach ($params as $param) {
13261316
$methodBuilder->addParam($param);

0 commit comments

Comments
 (0)