Skip to content

Commit 75f6492

Browse files
authored
Add types for most of the code generator (#1479)
1 parent e4628d2 commit 75f6492

28 files changed

+281
-31
lines changed

src/CodeGenerator/src/Command/GenerateCommand.php

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
use Symfony\Component\Console\Style\SymfonyStyle;
2828

2929
/**
30-
* Update a existing response class or API client method.
30+
* Update an existing response class or API client method.
3131
*
3232
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
3333
*/
@@ -38,14 +38,29 @@ class GenerateCommand extends Command
3838

3939
private $manifest;
4040

41+
/**
42+
* @var string
43+
*/
4144
private $manifestFile;
4245

46+
/**
47+
* @var Cache
48+
*/
4349
private $cache;
4450

51+
/**
52+
* @var ApiGenerator
53+
*/
4554
private $generator;
4655

56+
/**
57+
* @var ClassWriter
58+
*/
4759
private $classWriter;
4860

61+
/**
62+
* @var ComposerWriter
63+
*/
4964
private $composerWriter;
5065

5166
public function __construct(string $manifestFile, Cache $cache, ClassWriter $classWriter, ComposerWriter $composerWriter, ApiGenerator $generator)
@@ -59,7 +74,7 @@ public function __construct(string $manifestFile, Cache $cache, ClassWriter $cla
5974
parent::__construct();
6075
}
6176

62-
protected function configure()
77+
protected function configure(): void
6378
{
6479
$this->setAliases(['update']);
6580
$this->setDescription('Create or update API client methods.');
@@ -140,6 +155,11 @@ private function generateServicesParallel(SymfonyStyle $io, InputInterface $inpu
140155
return $manifest;
141156
}
142157

158+
/**
159+
* @param string[] $serviceNames
160+
*
161+
* @return array|int
162+
*/
143163
private function generateServicesSequential(SymfonyStyle $io, InputInterface $input, ConsoleOutputInterface $output, array $manifest, array $endpoints, array $serviceNames)
144164
{
145165
if (\count($serviceNames) > 1) {
@@ -302,7 +322,9 @@ private function generateService(SymfonyStyle $io, InputInterface $input, array
302322
}
303323

304324
/**
305-
* @return array|int
325+
* @param array<string, mixed> $manifest
326+
*
327+
* @return list<string>|int
306328
*/
307329
private function getServiceNames(?string $inputServiceName, bool $returnAll, SymfonyStyle $io, array $manifest)
308330
{
@@ -331,7 +353,11 @@ private function getServiceNames(?string $inputServiceName, bool $returnAll, Sym
331353
}
332354

333355
/**
334-
* @return array|int
356+
* @param array{operations: array<string, mixed>,...} $definition
357+
* @param array{waiters: array<string, mixed>} $waiter
358+
* @param array{methods: list<string>,...} $manifest
359+
*
360+
* @return string[]|int
335361
*/
336362
private function getOperationNames(?string $inputOperationName, bool $returnAll, SymfonyStyle $io, array $definition, array $waiter, array $manifest)
337363
{

src/CodeGenerator/src/Definition/ServiceDefinition.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
*/
1414
class ServiceDefinition
1515
{
16+
/**
17+
* @var string
18+
*/
1619
private $name;
1720

1821
private $endpoints;
@@ -29,8 +32,14 @@ class ServiceDefinition
2932

3033
private $hooks;
3134

35+
/**
36+
* @var array<string, list<Hook>>|null
37+
*/
3238
private $hooksByTarget;
3339

40+
/**
41+
* @var string
42+
*/
3443
private $apiReferenceUrl;
3544

3645
public function __construct(string $name, array $endpoints, array $definition, array $documentation, array $pagination, array $waiter, array $example, array $hooks, string $apiReferenceUrl)
@@ -155,6 +164,9 @@ private function getExample(string $name): Example
155164
return Example::create($this->example['examples'][$name][0] ?? []);
156165
}
157166

167+
/**
168+
* @param array<string, mixed> $extra
169+
*/
158170
private function getShape(string $name, ?Member $member, array $extra): ?Shape
159171
{
160172
if (isset($this->definition['shapes'][$name])) {
@@ -170,6 +182,9 @@ private function getShape(string $name, ?Member $member, array $extra): ?Shape
170182
return null;
171183
}
172184

185+
/**
186+
* @return \Closure(string, ?Member=, array<string, mixed>=): ?Shape
187+
*/
173188
private function createClosureToFindShape(): \Closure
174189
{
175190
$definition = $this;
@@ -179,6 +194,9 @@ private function createClosureToFindShape(): \Closure
179194
});
180195
}
181196

197+
/**
198+
* @return \Closure(): self
199+
*/
182200
private function createClosureToService(): \Closure
183201
{
184202
$definition = $this;
@@ -188,6 +206,9 @@ private function createClosureToService(): \Closure
188206
});
189207
}
190208

209+
/**
210+
* @return \Closure(string): ?Operation
211+
*/
191212
private function createClosureToFindOperation(): \Closure
192213
{
193214
$definition = $this;

src/CodeGenerator/src/File/Cache.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,19 @@
1111
*/
1212
class Cache
1313
{
14+
/**
15+
* @var string
16+
*/
1417
private $cacheFile;
1518

19+
/**
20+
* @var bool
21+
*/
1622
private $splitFile;
1723

24+
/**
25+
* @var resource
26+
*/
1827
private $handle;
1928

2029
public function __construct(string $cacheFile)
@@ -23,6 +32,9 @@ public function __construct(string $cacheFile)
2332
$this->splitFile = is_dir($cacheFile);
2433
}
2534

35+
/**
36+
* @return mixed
37+
*/
2638
public function get(string $key)
2739
{
2840
$this->lock($key);
@@ -37,6 +49,11 @@ public function get(string $key)
3749
}
3850
}
3951

52+
/**
53+
* @param callable(mixed): mixed $callable
54+
*
55+
* @return mixed
56+
*/
4057
public function update(string $key, callable $callable)
4158
{
4259
$this->lock($key);
@@ -61,24 +78,28 @@ public function update(string $key, callable $callable)
6178
}
6279
}
6380

81+
/**
82+
* @param mixed $content
83+
*/
6484
public function set(string $key, $content): void
6585
{
6686
$this->update($key, static function () use ($content) {
6787
return $content;
6888
});
6989
}
7090

71-
private function unlock()
91+
private function unlock(): void
7292
{
7393
flock($this->handle, \LOCK_UN);
7494
fclose($this->handle);
7595
}
7696

77-
private function lock(string $key)
97+
private function lock(string $key): void
7898
{
79-
/** @phpstan-ignore-next-line */
8099
set_error_handler(function ($type, $msg) use (&$error) {
81100
$error = $msg;
101+
102+
return true;
82103
});
83104
$filename = $this->cacheFile;
84105
if ($this->splitFile) {

src/CodeGenerator/src/File/CachedFileDumper.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,19 @@
1212
*/
1313
class CachedFileDumper extends FileDumper
1414
{
15+
/**
16+
* @var Cache
17+
*/
1518
private $cache;
1619

20+
/**
21+
* @var array<string, array{0?: string, 1?: string}>
22+
*/
1723
private $status = [];
1824

25+
/**
26+
* @var array<string, string>
27+
*/
1928
private $added = [];
2029

2130
public function __construct(Cache $cache)

src/CodeGenerator/src/File/ClassWriter.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,19 @@
1313
*/
1414
class ClassWriter
1515
{
16+
/**
17+
* @var string
18+
*/
1619
private $srcDirectory;
1720

21+
/**
22+
* @var FileDumper
23+
*/
1824
private $fileDumper;
1925

26+
/**
27+
* @var Printer
28+
*/
2029
private $printer;
2130

2231
public function __construct(string $srcDirectory, FileDumper $fileDumper)
@@ -26,7 +35,7 @@ public function __construct(string $srcDirectory, FileDumper $fileDumper)
2635
$this->printer = new Printer();
2736
}
2837

29-
public function write(ClassBuilder $classBuilder)
38+
public function write(ClassBuilder $classBuilder): void
3039
{
3140
$content = $this->printer->printNamespace($classBuilder->build());
3241

src/CodeGenerator/src/File/ComposerWriter.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,19 @@
1111
*/
1212
class ComposerWriter
1313
{
14+
/**
15+
* @var string
16+
*/
1417
private $srcDirectory;
1518

1619
public function __construct(string $srcDirectory)
1720
{
1821
$this->srcDirectory = $srcDirectory;
1922
}
2023

24+
/**
25+
* @param array<string, string> $requirements
26+
*/
2127
public function setRequirements(string $namespace, array $requirements, bool $clean): void
2228
{
2329
$filename = $this->findFile($namespace);

src/CodeGenerator/src/File/FileDumper.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
*/
1616
class FileDumper
1717
{
18+
/**
19+
* @var string[]|null
20+
*/
1821
private $phpBin;
1922

2023
public function dump(string $filename, string $content): void
@@ -28,8 +31,8 @@ private function verifyFileSyntax(string $filename): void
2831
{
2932
if (null === $this->phpBin) {
3033
$executableFinder = new PhpExecutableFinder();
31-
$this->phpBin = $executableFinder->find(false);
32-
$this->phpBin = false === $this->phpBin ? null : array_merge([$this->phpBin], $executableFinder->findArguments());
34+
$phpBin = $executableFinder->find(false);
35+
$this->phpBin = false === $phpBin ? null : array_merge([$phpBin], $executableFinder->findArguments());
3336
}
3437

3538
$process = new Process(array_merge($this->phpBin, ['-l', $filename]));

src/CodeGenerator/src/Generator/ApiGenerator.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace AsyncAws\CodeGenerator\Generator;
66

77
use AsyncAws\CodeGenerator\Generator\Composer\RequirementsRegistry;
8+
use AsyncAws\CodeGenerator\Generator\PhpGenerator\ClassBuilder;
89
use AsyncAws\CodeGenerator\Generator\PhpGenerator\ClassRegistry;
910

1011
/**
@@ -15,8 +16,14 @@
1516
*/
1617
class ApiGenerator
1718
{
19+
/**
20+
* @var ClassRegistry
21+
*/
1822
private $classRegistry;
1923

24+
/**
25+
* @var RequirementsRegistry
26+
*/
2027
private $requirementsRegistry;
2128

2229
public function __construct(?ClassRegistry $classRegistry = null, ?RequirementsRegistry $requirementsRegistry = null)
@@ -25,16 +32,25 @@ public function __construct(?ClassRegistry $classRegistry = null, ?RequirementsR
2532
$this->requirementsRegistry = $requirementsRegistry ?? new RequirementsRegistry();
2633
}
2734

35+
/**
36+
* @param list<string> $managedOperations
37+
*/
2838
public function service(string $baseNamespace, array $managedOperations): ServiceGenerator
2939
{
3040
return new ServiceGenerator($this->classRegistry, $this->requirementsRegistry, $baseNamespace, $managedOperations);
3141
}
3242

43+
/**
44+
* @return iterable<ClassBuilder>
45+
*/
3346
public function getUpdatedClasses(): iterable
3447
{
3548
return $this->classRegistry->getRegisteredClasses();
3649
}
3750

51+
/**
52+
* @return array<string, string>
53+
*/
3854
public function getUpdatedRequirements(): array
3955
{
4056
return $this->requirementsRegistry->getRequirements();

src/CodeGenerator/src/Generator/CodeGenerator/TypeGenerator.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public function __construct(NamespaceRegistry $namespaceRegistry)
3434
/**
3535
* Return docblock information for the given shape.
3636
*
37+
* @param string[] $extra
38+
*
3739
* @return array{string, ClassName[]} [docblock representation, ClassName related]
3840
*/
3941
public function generateDocblock(StructureShape $shape, ClassName $shapeClassName, bool $alternateClass = true, bool $allNullable = false, bool $isObject = false, array $extra = []): array

src/CodeGenerator/src/Generator/Composer/RequirementsRegistry.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
class RequirementsRegistry
66
{
7+
/**
8+
* @var array<string, string>
9+
*/
710
private $requirements = [];
811

912
public function addRequirement(string $package, string $version = '*'): void
@@ -21,6 +24,9 @@ public function addRequirement(string $package, string $version = '*'): void
2124
$this->requirements[$package] = $version;
2225
}
2326

27+
/**
28+
* @return array<string, string>
29+
*/
2430
public function getRequirements(): array
2531
{
2632
return $this->requirements;

0 commit comments

Comments
 (0)