Skip to content

Commit e7da002

Browse files
committed
Typehint checks
1 parent c66d5d6 commit e7da002

9 files changed

+43
-116
lines changed

src/Check/CheckRepository.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,8 @@ public function __construct(array $checks = [])
2727

2828
/**
2929
* Add a new check to the repository.
30-
*
31-
* @param CheckInterface $check The check instance to add.
3230
*/
33-
public function registerCheck(CheckInterface $check)
31+
public function registerCheck(CheckInterface $check) : void
3432
{
3533
$this->checks[get_class($check)] = $check;
3634
}
@@ -40,7 +38,7 @@ public function registerCheck(CheckInterface $check)
4038
*
4139
* @return array
4240
*/
43-
public function getAll()
41+
public function getAll() : array
4442
{
4543
return array_values($this->checks);
4644
}
@@ -52,7 +50,7 @@ public function getAll()
5250
* @return CheckInterface The instance.
5351
* @throws InvalidArgumentException If an instance of the check does not exist.
5452
*/
55-
public function getByClass($class)
53+
public function getByClass(string $class) : CheckInterface
5654
{
5755
if (!isset($this->checks[$class])) {
5856
throw new InvalidArgumentException(sprintf('Check: "%s" does not exist', $class));
@@ -63,11 +61,8 @@ public function getByClass($class)
6361

6462
/**
6563
* Query whether a check instance exists in this repository via its class name.
66-
*
67-
* @param string $class
68-
* @return bool
6964
*/
70-
public function has($class)
65+
public function has(string $class) : bool
7166
{
7267
try {
7368
$this->getByClass($class);

src/Check/CodeParseCheck.php

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
*/
2020
class CodeParseCheck implements SimpleCheckInterface
2121
{
22-
2322
/**
2423
* @var Parser
2524
*/
@@ -35,10 +34,8 @@ public function __construct(Parser $parser)
3534

3635
/**
3736
* Return the check's name
38-
*
39-
* @return string
4037
*/
41-
public function getName()
38+
public function getName() : string
4239
{
4340
return 'Code Parse Check';
4441
}
@@ -52,7 +49,7 @@ public function getName()
5249
* @param Input $input The command line arguments passed to the command.
5350
* @return ResultInterface The result of the check.
5451
*/
55-
public function check(ExerciseInterface $exercise, Input $input)
52+
public function check(ExerciseInterface $exercise, Input $input) : ResultInterface
5653
{
5754

5855
$code = file_get_contents($input->getArgument('program'));
@@ -68,30 +65,25 @@ public function check(ExerciseInterface $exercise, Input $input)
6865

6966
/**
7067
* This check can run on any exercise type.
71-
*
72-
* @param ExerciseType $exerciseType
73-
* @return bool
7468
*/
75-
public function canRun(ExerciseType $exerciseType)
69+
public function canRun(ExerciseType $exerciseType) : bool
7670
{
77-
return in_array($exerciseType->getValue(), [ExerciseType::CGI, ExerciseType::CLI]);
71+
return in_array($exerciseType->getValue(), [ExerciseType::CGI, ExerciseType::CLI], true);
7872
}
7973

8074
/**
8175
* @return string
8276
*/
83-
public function getExerciseInterface()
77+
public function getExerciseInterface() : string
8478
{
8579
return ExerciseInterface::class;
8680
}
8781

8882
/**
8983
* This check should be run before executing the student's solution, as, if it cannot be parsed
9084
* it probably cannot be executed.
91-
*
92-
* @return string
9385
*/
94-
public function getPosition()
86+
public function getPosition() : string
9587
{
9688
return SimpleCheckInterface::CHECK_BEFORE;
9789
}

src/Check/ComposerCheck.php

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,14 @@
1414
/**
1515
* This check looks for a set of composer packages specified by the exercise
1616
* in the students `composer.lock` file.
17-
*
18-
* @author Aydin Hassan <[email protected]>
1917
*/
2018
class ComposerCheck implements SimpleCheckInterface
2119
{
2220

2321
/**
2422
* Return the check's name
25-
*
26-
* @return string
2723
*/
28-
public function getName()
24+
public function getName() : string
2925
{
3026
return 'Composer Dependency Check';
3127
}
@@ -39,7 +35,7 @@ public function getName()
3935
* @param Input $input The command line arguments passed to the command.
4036
* @return ResultInterface The result of the check.
4137
*/
42-
public function check(ExerciseInterface $exercise, Input $input)
38+
public function check(ExerciseInterface $exercise, Input $input) : ResultInterface
4339
{
4440
if (!$exercise instanceof ComposerExerciseCheck) {
4541
throw new \InvalidArgumentException;
@@ -77,30 +73,21 @@ public function check(ExerciseInterface $exercise, Input $input)
7773

7874
/**
7975
* This check can run on any exercise type.
80-
*
81-
* @param ExerciseType $exerciseType
82-
* @return bool
8376
*/
84-
public function canRun(ExerciseType $exerciseType)
77+
public function canRun(ExerciseType $exerciseType) : bool
8578
{
86-
return in_array($exerciseType->getValue(), [ExerciseType::CGI, ExerciseType::CLI]);
79+
return in_array($exerciseType->getValue(), [ExerciseType::CGI, ExerciseType::CLI], true);
8780
}
8881

89-
/**
90-
*
91-
* @return string
92-
*/
93-
public function getExerciseInterface()
82+
public function getExerciseInterface() : string
9483
{
9584
return ComposerExerciseCheck::class;
9685
}
9786

9887
/**
9988
* This check can run before because if it fails, there is no point executing the solution.
100-
*
101-
* @return string
10289
*/
103-
public function getPosition()
90+
public function getPosition() : string
10491
{
10592
return SimpleCheckInterface::CHECK_BEFORE;
10693
}

src/Check/DatabaseCheck.php

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,19 @@
33
namespace PhpSchool\PhpWorkshop\Check;
44

55
use PDO;
6-
use PhpSchool\PhpWorkshop\Event\CliEvent;
76
use PhpSchool\PhpWorkshop\Event\CliExecuteEvent;
87
use PhpSchool\PhpWorkshop\Event\Event;
98
use PhpSchool\PhpWorkshop\Event\EventDispatcher;
109
use PhpSchool\PhpWorkshop\Exercise\TemporaryDirectoryTrait;
1110
use PhpSchool\PhpWorkshop\ExerciseCheck\DatabaseExerciseCheck;
1211
use PhpSchool\PhpWorkshop\Result\Failure;
1312
use PhpSchool\PhpWorkshop\Result\Success;
14-
use Symfony\Component\Process\Process;
1513

1614
/**
1715
* This check sets up a database and a `PDO` object. It prepends the database DSN as a CLI argument to the student's
1816
* solution so they can connect to the database. The `PDO` object is passed to the exercise before and after the
1917
* student's solution has been executed, allowing you to first seed the database and then verify the contents of the
2018
* database.
21-
*
22-
* @author Aydin Hassan <[email protected]>
2319
*/
2420
class DatabaseCheck implements ListenableCheckInterface
2521
{
@@ -64,29 +60,22 @@ public function __construct()
6460

6561
/**
6662
* Return the check's name
67-
*
68-
* @return string
6963
*/
70-
public function getName()
64+
public function getName() : string
7165
{
7266
return 'Database Verification Check';
7367
}
7468

75-
/**
76-
* @return string
77-
*/
78-
public function getExerciseInterface()
69+
public function getExerciseInterface() : string
7970
{
8071
return DatabaseExerciseCheck::class;
8172
}
8273

8374
/**
8475
* Here we attach to various events to seed, verify and inject the DSN's
8576
* to the student & reference solution programs's CLI arguments.
86-
*
87-
* @param EventDispatcher $eventDispatcher
8877
*/
89-
public function attach(EventDispatcher $eventDispatcher)
78+
public function attach(EventDispatcher $eventDispatcher) : void
9079
{
9180
if (file_exists($this->databaseDirectory)) {
9281
throw new \RuntimeException(

src/Check/FileExistsCheck.php

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@ class FileExistsCheck implements SimpleCheckInterface
1616
{
1717
/**
1818
* Return the check's name.
19-
*
20-
* @return string
2119
*/
22-
public function getName()
20+
public function getName() : string
2321
{
2422
return 'File Exists Check';
2523
}
@@ -31,7 +29,7 @@ public function getName()
3129
* @param Input $input The command line arguments passed to the command.
3230
* @return ResultInterface The result of the check.
3331
*/
34-
public function check(ExerciseInterface $exercise, Input $input)
32+
public function check(ExerciseInterface $exercise, Input $input) : ResultInterface
3533
{
3634
if (file_exists($input->getArgument('program'))) {
3735
return Success::fromCheck($this);
@@ -42,30 +40,21 @@ public function check(ExerciseInterface $exercise, Input $input)
4240

4341
/**
4442
* This check can run on any exercise type.
45-
*
46-
* @param ExerciseType $exerciseType
47-
* @return bool
4843
*/
49-
public function canRun(ExerciseType $exerciseType)
44+
public function canRun(ExerciseType $exerciseType) : bool
5045
{
51-
return in_array($exerciseType->getValue(), [ExerciseType::CGI, ExerciseType::CLI]);
46+
return in_array($exerciseType->getValue(), [ExerciseType::CGI, ExerciseType::CLI], true);
5247
}
5348

54-
/**
55-
*
56-
* @return string
57-
*/
58-
public function getExerciseInterface()
49+
public function getExerciseInterface() : string
5950
{
6051
return ExerciseInterface::class;
6152
}
6253

6354
/**
6455
* This check must run before executing the solution becuase it may not exist.
65-
*
66-
* @return string
6756
*/
68-
public function getPosition()
57+
public function getPosition() : string
6958
{
7059
return SimpleCheckInterface::CHECK_BEFORE;
7160
}

src/Check/FunctionRequirementsCheck.php

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,8 @@ public function __construct(Parser $parser)
3838

3939
/**
4040
* Return the check's name.
41-
*
42-
* @return string
4341
*/
44-
public function getName()
42+
public function getName() : string
4543
{
4644
return 'Function Requirements Check';
4745
}
@@ -55,7 +53,7 @@ public function getName()
5553
* @param Input $input The command line arguments passed to the command.
5654
* @return ResultInterface The result of the check.
5755
*/
58-
public function check(ExerciseInterface $exercise, Input $input)
56+
public function check(ExerciseInterface $exercise, Input $input) : ResultInterface
5957
{
6058
if (!$exercise instanceof FunctionRequirementsExerciseCheck) {
6159
throw new \InvalidArgumentException;
@@ -99,19 +97,13 @@ public function check(ExerciseInterface $exercise, Input $input)
9997

10098
/**
10199
* This check can run on any exercise type.
102-
*
103-
* @param ExerciseType $exerciseType
104-
* @return bool
105100
*/
106-
public function canRun(ExerciseType $exerciseType)
101+
public function canRun(ExerciseType $exerciseType) : bool
107102
{
108-
return in_array($exerciseType->getValue(), [ExerciseType::CGI, ExerciseType::CLI]);
103+
return in_array($exerciseType->getValue(), [ExerciseType::CGI, ExerciseType::CLI], true);
109104
}
110105

111-
/**
112-
* @return string
113-
*/
114-
public function getExerciseInterface()
106+
public function getExerciseInterface() : string
115107
{
116108
return FunctionRequirementsExerciseCheck::class;
117109
}
@@ -120,10 +112,8 @@ public function getExerciseInterface()
120112
* This is performed after executing the student's solution because the solution may produce the correct
121113
* output, but do it in a way that was not correct for the task. This way the student can see the program works
122114
* but missed some requirements.
123-
*
124-
* @return string
125115
*/
126-
public function getPosition()
116+
public function getPosition() : string
127117
{
128118
return SimpleCheckInterface::CHECK_AFTER;
129119
}

src/Check/ListenableCheckInterface.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ interface ListenableCheckInterface extends CheckInterface
1212
/**
1313
* Attach to events throughout the running/verifying process. Inject verifiers
1414
* and listeners.
15-
*
16-
* @param EventDispatcher $eventDispatcher
1715
*/
18-
public function attach(EventDispatcher $eventDispatcher);
16+
public function attach(EventDispatcher $eventDispatcher) : void;
1917
}

0 commit comments

Comments
 (0)