Skip to content

Commit 1381227

Browse files
authored
Merge pull request #181 from php-school/various-code-inspections
Various code improvements
2 parents 0cce3c2 + 1a57923 commit 1381227

39 files changed

+89
-78
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
],
1717
"require" : {
1818
"php" : ">=7.2",
19+
"ext-pdo": "*",
1920
"ext-pdo_sqlite": "*",
2021
"php-di/php-di": "^6.0",
2122
"psr/container": "^1.0",

src/Application.php

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,18 @@
44

55
namespace PhpSchool\PhpWorkshop;
66

7+
use DI\Container;
78
use DI\ContainerBuilder;
89
use PhpSchool\PhpWorkshop\Check\CheckRepository;
910
use PhpSchool\PhpWorkshop\Exception\InvalidArgumentException;
1011
use PhpSchool\PhpWorkshop\Exception\MissingArgumentException;
11-
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
1212
use PhpSchool\PhpWorkshop\Factory\ResultRendererFactory;
1313
use PhpSchool\PhpWorkshop\Output\OutputInterface;
14+
use RuntimeException;
15+
16+
use function class_exists;
17+
use function is_file;
18+
use function sprintf;
1419

1520
/**
1621
* This is the main application class, this takes care of bootstrapping, routing and
@@ -46,7 +51,7 @@ final class Application
4651
/**
4752
* @var string|null
4853
*/
49-
private $logo = null;
54+
private $logo;
5055

5156
/**
5257
* @var string
@@ -72,8 +77,8 @@ final class Application
7277
*/
7378
public function __construct(string $workshopTitle, string $diConfigFile)
7479
{
75-
if (!\is_file($diConfigFile)) {
76-
throw new InvalidArgumentException(\sprintf('File "%s" was expected to exist.', $diConfigFile));
80+
if (!is_file($diConfigFile)) {
81+
throw new InvalidArgumentException(sprintf('File "%s" was expected to exist.', $diConfigFile));
7782
}
7883

7984
$this->workshopTitle = $workshopTitle;
@@ -108,12 +113,12 @@ public function addExercise(string $exercise): void
108113
*/
109114
public function addResult(string $resultClass, string $resultRendererClass): void
110115
{
111-
if (!\class_exists($resultClass)) {
112-
throw new InvalidArgumentException(\sprintf('Class "%s" does not exist', $resultClass));
116+
if (!class_exists($resultClass)) {
117+
throw new InvalidArgumentException(sprintf('Class "%s" does not exist', $resultClass));
113118
}
114119

115-
if (!\class_exists($resultRendererClass)) {
116-
throw new InvalidArgumentException(\sprintf('Class "%s" does not exist', $resultRendererClass));
120+
if (!class_exists($resultRendererClass)) {
121+
throw new InvalidArgumentException(sprintf('Class "%s" does not exist', $resultRendererClass));
117122
}
118123

119124
$this->results[] = [
@@ -167,7 +172,7 @@ public function run(): int
167172

168173
foreach ($this->exercises as $exercise) {
169174
if (false === $container->has($exercise)) {
170-
throw new \RuntimeException(
175+
throw new RuntimeException(
171176
sprintf('No DI config found for exercise: "%s". Register a factory.', $exercise)
172177
);
173178
}
@@ -176,7 +181,7 @@ public function run(): int
176181
$checkRepository = $container->get(CheckRepository::class);
177182
foreach ($this->checks as $check) {
178183
if (false === $container->has($check)) {
179-
throw new \RuntimeException(
184+
throw new RuntimeException(
180185
sprintf('No DI config found for check: "%s". Register a factory.', $check)
181186
);
182187
}
@@ -206,7 +211,7 @@ public function run(): int
206211
)
207212
);
208213
return 1;
209-
} catch (\RuntimeException $e) {
214+
} catch (RuntimeException $e) {
210215
$container
211216
->get(OutputInterface::class)
212217
->printError(
@@ -221,9 +226,9 @@ public function run(): int
221226
}
222227

223228
/**
224-
* @return \DI\Container
229+
* @return Container
225230
*/
226-
private function getContainer(): \DI\Container
231+
private function getContainer(): Container
227232
{
228233
$containerBuilder = new ContainerBuilder();
229234
$containerBuilder->addDefinitions(

src/Check/CodeParseCheck.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
namespace PhpSchool\PhpWorkshop\Check;
66

77
use PhpParser\Error;
8-
use PhpParser\Node\Expr\FuncCall;
9-
use PhpParser\NodeTraverser;
108
use PhpParser\Parser;
119
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
1210
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
@@ -66,6 +64,8 @@ public function check(ExerciseInterface $exercise, Input $input): ResultInterfac
6664

6765
/**
6866
* This check can run on any exercise type.
67+
* @param ExerciseType $exerciseType
68+
* @return bool
6969
*/
7070
public function canRun(ExerciseType $exerciseType): bool
7171
{

src/Check/ComposerCheck.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace PhpSchool\PhpWorkshop\Check;
66

7+
use InvalidArgumentException;
78
use PhpSchool\PhpWorkshop\ComposerUtil\LockFileParser;
89
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
910
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
@@ -35,11 +36,12 @@ public function getName(): string
3536
* @param ExerciseInterface $exercise The exercise to check against.
3637
* @param Input $input The command line arguments passed to the command.
3738
* @return ResultInterface The result of the check.
39+
* @noinspection SpellCheckingInspection
3840
*/
3941
public function check(ExerciseInterface $exercise, Input $input): ResultInterface
4042
{
4143
if (!$exercise instanceof ComposerExerciseCheck) {
42-
throw new \InvalidArgumentException();
44+
throw new InvalidArgumentException();
4345
}
4446

4547
if (!file_exists(sprintf('%s/composer.json', dirname($input->getRequiredArgument('program'))))) {
@@ -74,6 +76,9 @@ public function check(ExerciseInterface $exercise, Input $input): ResultInterfac
7476

7577
/**
7678
* This check can run on any exercise type.
79+
*
80+
* @param ExerciseType $exerciseType
81+
* @return bool
7782
*/
7883
public function canRun(ExerciseType $exerciseType): bool
7984
{

src/Check/DatabaseCheck.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
namespace PhpSchool\PhpWorkshop\Check;
66

77
use PDO;
8+
use PDOException;
89
use PhpSchool\PhpWorkshop\Event\CliExecuteEvent;
910
use PhpSchool\PhpWorkshop\Event\Event;
1011
use PhpSchool\PhpWorkshop\Event\EventDispatcher;
1112
use PhpSchool\PhpWorkshop\Exercise\TemporaryDirectoryTrait;
1213
use PhpSchool\PhpWorkshop\ExerciseCheck\DatabaseExerciseCheck;
1314
use PhpSchool\PhpWorkshop\Result\Failure;
1415
use PhpSchool\PhpWorkshop\Result\Success;
16+
use RuntimeException;
1517

1618
/**
1719
* This check sets up a database and a `PDO` object. It prepends the database DSN as a CLI argument to the student's
@@ -75,12 +77,14 @@ public function getExerciseInterface(): string
7577

7678
/**
7779
* Here we attach to various events to seed, verify and inject the DSN's
78-
* to the student & reference solution programs's CLI arguments.
80+
* to the student & reference solution programs' CLI arguments.
81+
*
82+
* @param EventDispatcher $eventDispatcher
7983
*/
8084
public function attach(EventDispatcher $eventDispatcher): void
8185
{
8286
if (file_exists($this->databaseDirectory)) {
83-
throw new \RuntimeException(
87+
throw new RuntimeException(
8488
sprintf('Database directory: "%s" already exists', $this->databaseDirectory)
8589
);
8690
}
@@ -90,7 +94,7 @@ public function attach(EventDispatcher $eventDispatcher): void
9094
try {
9195
$db = new PDO($this->userDsn);
9296
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
93-
} catch (\PDOException $e) {
97+
} catch (PDOException $e) {
9498
rmdir($this->databaseDirectory);
9599
throw $e;
96100
}
@@ -132,7 +136,7 @@ function (CliExecuteEvent $e) {
132136
'verify.finish',
133137
'run.finish'
134138
],
135-
function (Event $e) use ($db) {
139+
function () use ($db) {
136140
unset($db);
137141
@unlink($this->userDatabasePath);
138142
@unlink($this->solutionDatabasePath);

src/Check/FileExistsCheck.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ public function check(ExerciseInterface $exercise, Input $input): ResultInterfac
4545

4646
/**
4747
* This check can run on any exercise type.
48+
*
49+
* @param ExerciseType $exerciseType
50+
* @return bool
4851
*/
4952
public function canRun(ExerciseType $exerciseType): bool
5053
{
@@ -57,7 +60,7 @@ public function getExerciseInterface(): string
5760
}
5861

5962
/**
60-
* This check must run before executing the solution becuase it may not exist.
63+
* This check must run before executing the solution because it may not exist.
6164
*/
6265
public function getPosition(): string
6366
{

src/Check/FunctionRequirementsCheck.php

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

55
namespace PhpSchool\PhpWorkshop\Check;
66

7+
use InvalidArgumentException;
78
use PhpParser\Error;
89
use PhpParser\Node\Expr\FuncCall;
9-
use PhpParser\Node\Name;
1010
use PhpParser\NodeTraverser;
1111
use PhpParser\Parser;
1212
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
@@ -18,7 +18,6 @@
1818
use PhpSchool\PhpWorkshop\Result\FunctionRequirementsFailure;
1919
use PhpSchool\PhpWorkshop\Result\ResultInterface;
2020
use PhpSchool\PhpWorkshop\Result\Success;
21-
use PhpSchool\PhpWorkshop\Utils\Collection;
2221

2322
/**
2423
* This check verifies that the student's solution contains usages of some required functions
@@ -59,7 +58,7 @@ public function getName(): string
5958
public function check(ExerciseInterface $exercise, Input $input): ResultInterface
6059
{
6160
if (!$exercise instanceof FunctionRequirementsExerciseCheck) {
62-
throw new \InvalidArgumentException();
61+
throw new InvalidArgumentException();
6362
}
6463

6564
$requiredFunctions = $exercise->getRequiredFunctions();
@@ -100,6 +99,9 @@ public function check(ExerciseInterface $exercise, Input $input): ResultInterfac
10099

101100
/**
102101
* This check can run on any exercise type.
102+
*
103+
* @param ExerciseType $exerciseType
104+
* @return bool
103105
*/
104106
public function canRun(ExerciseType $exerciseType): bool
105107
{

src/Check/ListenableCheckInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ interface ListenableCheckInterface extends CheckInterface
1414
/**
1515
* Attach to events throughout the running/verifying process. Inject verifiers
1616
* and listeners.
17+
*
18+
* @param EventDispatcher $eventDispatcher
1719
*/
1820
public function attach(EventDispatcher $eventDispatcher): void;
1921
}

src/Check/PhpLintCheck.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ public function check(ExerciseInterface $exercise, Input $input): ResultInterfac
4747

4848
/**
4949
* This check can run on any exercise type.
50+
*
51+
* @param ExerciseType $exerciseType
52+
* @return bool
5053
*/
5154
public function canRun(ExerciseType $exerciseType): bool
5255
{

src/Check/SimpleCheckInterface.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ interface SimpleCheckInterface extends CheckInterface
3131

3232
/**
3333
* Can this check run this exercise?
34+
*
35+
* @param ExerciseType $exerciseType
36+
* @return bool
3437
*/
3538
public function canRun(ExerciseType $exerciseType): bool;
3639

0 commit comments

Comments
 (0)