Skip to content

Commit 437caa3

Browse files
committed
Phpstan fixes
1 parent bbe90a6 commit 437caa3

27 files changed

+207
-113
lines changed

phpstan.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ parameters:
1212
message: '#Class PHPUnit\\Framework\\TestCase not found#'
1313
path: src/TestUtils/WorkshopExerciseTest.php
1414

15-
excludes_analyse:
15+
excludePaths:
1616
- src/TestUtils/WorkshopExerciseTest.php

src/Application.php

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use DI\Container;
88
use DI\ContainerBuilder;
9+
use PhpSchool\PhpWorkshop\Check\CheckInterface;
910
use PhpSchool\PhpWorkshop\Check\CheckRepository;
1011
use PhpSchool\PhpWorkshop\Event\Event;
1112
use PhpSchool\PhpWorkshop\Event\EventDispatcher;
@@ -185,6 +186,7 @@ public function configure(bool $debugMode = false): ContainerInterface
185186
}
186187
}
187188

189+
/** @var CheckRepository $checkRepository */
188190
$checkRepository = $container->get(CheckRepository::class);
189191
foreach ($this->checks as $check) {
190192
if (false === $container->has($check)) {
@@ -193,10 +195,23 @@ public function configure(bool $debugMode = false): ContainerInterface
193195
);
194196
}
195197

196-
$checkRepository->registerCheck($container->get($check));
198+
$checkInstance = $container->get($check);
199+
200+
if (!$checkInstance instanceof CheckInterface) {
201+
throw new RuntimeException(
202+
sprintf(
203+
'Check: "%s" does not implement the required interface: "%s".',
204+
$check,
205+
CheckInterface::class
206+
)
207+
);
208+
}
209+
210+
$checkRepository->registerCheck($checkInstance);
197211
}
198212

199213
if (!empty($this->results)) {
214+
/** @var ResultRendererFactory $resultFactory */
200215
$resultFactory = $container->get(ResultRendererFactory::class);
201216

202217
foreach ($this->results as $result) {
@@ -234,32 +249,35 @@ public function run(): int
234249
$container = $this->configure($debug);
235250

236251
try {
237-
$exitCode = $container->get(CommandRouter::class)->route($args);
252+
/** @var CommandRouter $router */
253+
$router = $container->get(CommandRouter::class);
254+
$exitCode = $router->route($args);
238255
} catch (MissingArgumentException $e) {
239-
$container
240-
->get(OutputInterface::class)
241-
->printError(
242-
sprintf(
243-
'Argument%s: "%s" %s missing!',
244-
count($e->getMissingArguments()) > 1 ? 's' : '',
245-
implode('", "', $e->getMissingArguments()),
246-
count($e->getMissingArguments()) > 1 ? 'are' : 'is'
247-
)
248-
);
256+
/** @var OutputInterface $output */
257+
$output = $container->get(OutputInterface::class);
258+
$output->printError(
259+
sprintf(
260+
'Argument%s: "%s" %s missing!',
261+
count($e->getMissingArguments()) > 1 ? 's' : '',
262+
implode('", "', $e->getMissingArguments()),
263+
count($e->getMissingArguments()) > 1 ? 'are' : 'is'
264+
)
265+
);
249266
return 1;
250267
} catch (\Throwable $e) {
251268
$message = $e->getMessage();
252-
$basePath = canonicalise_path($container->get('basePath'));
269+
$basePath = canonicalise_path(is_string($path = $container->get('basePath')) ? $path : '');
253270

254271
if (strpos($message, $basePath) !== null) {
255272
$message = str_replace($basePath, '', $message);
256273
}
257274

258275
$this->tearDown($container);
259276

260-
$container
261-
->get(OutputInterface::class)
262-
->printException($e);
277+
/** @var OutputInterface $output */
278+
$output = $container->get(OutputInterface::class);
279+
$output->printException($e);
280+
263281
return 1;
264282
}
265283

@@ -307,11 +325,13 @@ private function getContainer(bool $debugMode): Container
307325
private function tearDown(ContainerInterface $container): void
308326
{
309327
try {
310-
$container
311-
->get(EventDispatcher::class)
312-
->dispatch(new Event('application.tear-down'));
328+
/** @var EventDispatcher $eventDispatcher */
329+
$eventDispatcher = $container->get(EventDispatcher::class);
330+
$eventDispatcher->dispatch(new Event('application.tear-down'));
313331
} catch (\Throwable $t) {
314-
$container->get(LoggerInterface::class)->error($t->getMessage(), ['exception' => $t]);
332+
/** @var LoggerInterface $logger */
333+
$logger = $container->get(LoggerInterface::class);
334+
$logger->error($t->getMessage(), ['exception' => $t]);
315335
}
316336
}
317337
}

src/Check/DatabaseCheck.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,17 @@ public function attach(EventDispatcher $eventDispatcher): void
100100
}
101101

102102
$eventDispatcher->listen('verify.start', function (Event $e) use ($db) {
103-
$e->getParameter('exercise')->seed($db);
103+
/** @var DatabaseExerciseCheck $exercise */
104+
$exercise = $e->getParameter('exercise');
105+
$exercise->seed($db);
104106
//make a copy - so solution can modify without effecting database user has access to
105107
copy($this->userDatabasePath, $this->solutionDatabasePath);
106108
});
107109

108110
$eventDispatcher->listen('run.start', function (Event $e) use ($db) {
109-
$e->getParameter('exercise')->seed($db);
111+
/** @var DatabaseExerciseCheck $exercise */
112+
$exercise = $e->getParameter('exercise');
113+
$exercise->seed($db);
110114
});
111115

112116
$eventDispatcher->listen('cli.verify.reference-execute.pre', function (CliExecuteEvent $e) {
@@ -121,7 +125,9 @@ function (CliExecuteEvent $e) {
121125
);
122126

123127
$eventDispatcher->insertVerifier('verify.finish', function (Event $e) use ($db) {
124-
$verifyResult = $e->getParameter('exercise')->verify($db);
128+
/** @var DatabaseExerciseCheck $exercise */
129+
$exercise = $e->getParameter('exercise');
130+
$verifyResult = $exercise->verify($db);
125131

126132
if (false === $verifyResult) {
127133
return Failure::fromNameAndReason($this->getName(), 'Database verification failed');

src/Check/FileComparisonCheck.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public function check(ExerciseInterface $exercise, Input $input): ResultInterfac
8080
/**
8181
* @param int|string $key
8282
* @param string|array<string, string> $file
83-
* @return array{0: array, 1:string}
83+
* @return array{0: array{strip?: string}, 1:string}
8484
*/
8585
private function getOptionsAndFile($key, $file): array
8686
{

src/ComposerUtil/LockFileParser.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
class LockFileParser
1313
{
1414
/**
15-
* @var array<string, mixed>
15+
* @var array{packages: array<array{name: string, version: string}>}
1616
*/
1717
private $contents;
1818

@@ -26,11 +26,18 @@ public function __construct(string $lockFilePath)
2626
throw new InvalidArgumentException(sprintf('Lock File: "%s" does not exist', $lockFilePath));
2727
}
2828

29-
$this->contents = json_decode((string) file_get_contents($lockFilePath), true);
29+
$content = json_decode((string) file_get_contents($lockFilePath), true);
3030

31-
if (!isset($this->contents['packages'])) {
31+
if (!is_array($content)) {
32+
throw new InvalidArgumentException(sprintf('Lock File: "%s" is corrupted', $lockFilePath));
33+
}
34+
35+
if (!isset($content['packages']) || !is_array($content['packages'])) {
3236
throw new InvalidArgumentException(sprintf('Lock File: "%s" does not contain packages key', $lockFilePath));
3337
}
38+
39+
/** @var array{packages: array<array{name: string, version: string}>} $content */
40+
$this->contents = $content;
3441
}
3542

3643
/**

src/Event/CliExecuteEvent.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
class CliExecuteEvent extends Event
1414
{
1515
/**
16-
* @var ArrayObject<string>
16+
* @var ArrayObject<int, string>
1717
*/
1818
private $args;
1919

2020
/**
2121
* @param string $name The event name.
22-
* @param ArrayObject<string> $args The arguments that should be/have been passed to the program.
22+
* @param ArrayObject<int, string> $args The arguments that should be/have been passed to the program.
2323
* @param array<mixed> $parameters The event parameters.
2424
*/
2525
public function __construct(string $name, ArrayObject $args, array $parameters = [])
@@ -52,7 +52,7 @@ public function appendArg(string $arg): void
5252
/**
5353
* Get the arguments to be passed to the program.
5454
*
55-
* @return ArrayObject<string>
55+
* @return ArrayObject<int, string>
5656
*/
5757
public function getArgs(): ArrayObject
5858
{

src/Exception/InvalidArgumentException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ public static function notValidParameter(string $parameterName, array $allowedVa
4141
sprintf(
4242
'Parameter: "%s" can only be one of: "%s" Received: "%s"',
4343
$parameterName,
44-
static::stringify($allowedValues),
45-
static::stringify($actualValue)
44+
self::stringify($allowedValues),
45+
self::stringify($actualValue)
4646
)
4747
);
4848
}

src/Exercise/ExerciseType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,6 @@ class ExerciseType extends Enum
4545
*/
4646
public function getExerciseInterface(): string
4747
{
48-
return static::$exerciseTypeToExerciseInterfaceMap[$this->getKey()];
48+
return self::$exerciseTypeToExerciseInterfaceMap[$this->getKey()];
4949
}
5050
}

src/ExerciseRunner/CgiRunner.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public function getName(): string
119119
*/
120120
public function getRequiredChecks(): array
121121
{
122-
return static::$requiredChecks;
122+
return self::$requiredChecks;
123123
}
124124

125125
/**

src/ExerciseRunner/CliRunner.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,12 @@ public function getName(): string
8585
*/
8686
public function getRequiredChecks(): array
8787
{
88-
return static::$requiredChecks;
88+
return self::$requiredChecks;
8989
}
9090

9191
/**
9292
* @param string $fileName
93-
* @param ArrayObject<string> $args
93+
* @param ArrayObject<int, string> $args
9494
* @param string $type
9595
* @return string
9696
*/
@@ -111,7 +111,7 @@ private function executePhpFile(string $fileName, ArrayObject $args, string $typ
111111

112112
/**
113113
* @param string $fileName
114-
* @param ArrayObject<string> $args
114+
* @param ArrayObject<int, string> $args
115115
*
116116
* @return Process
117117
*/

0 commit comments

Comments
 (0)