Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
],
"require" : {
"php" : ">=7.2",
"ext-pdo": "*",
"ext-pdo_sqlite": "*",
"php-di/php-di": "^6.0",
"psr/container": "^1.0",
Expand Down
31 changes: 18 additions & 13 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@

namespace PhpSchool\PhpWorkshop;

use DI\Container;
use DI\ContainerBuilder;
use PhpSchool\PhpWorkshop\Check\CheckRepository;
use PhpSchool\PhpWorkshop\Exception\InvalidArgumentException;
use PhpSchool\PhpWorkshop\Exception\MissingArgumentException;
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
use PhpSchool\PhpWorkshop\Factory\ResultRendererFactory;
use PhpSchool\PhpWorkshop\Output\OutputInterface;
use RuntimeException;

use function class_exists;
use function is_file;
use function sprintf;

/**
* This is the main application class, this takes care of bootstrapping, routing and
Expand Down Expand Up @@ -46,7 +51,7 @@ final class Application
/**
* @var string|null
*/
private $logo = null;
private $logo;

/**
* @var string
Expand All @@ -72,8 +77,8 @@ final class Application
*/
public function __construct(string $workshopTitle, string $diConfigFile)
{
if (!\is_file($diConfigFile)) {
throw new InvalidArgumentException(\sprintf('File "%s" was expected to exist.', $diConfigFile));
if (!is_file($diConfigFile)) {
throw new InvalidArgumentException(sprintf('File "%s" was expected to exist.', $diConfigFile));
}

$this->workshopTitle = $workshopTitle;
Expand Down Expand Up @@ -108,12 +113,12 @@ public function addExercise(string $exercise): void
*/
public function addResult(string $resultClass, string $resultRendererClass): void
{
if (!\class_exists($resultClass)) {
throw new InvalidArgumentException(\sprintf('Class "%s" does not exist', $resultClass));
if (!class_exists($resultClass)) {
throw new InvalidArgumentException(sprintf('Class "%s" does not exist', $resultClass));
}

if (!\class_exists($resultRendererClass)) {
throw new InvalidArgumentException(\sprintf('Class "%s" does not exist', $resultRendererClass));
if (!class_exists($resultRendererClass)) {
throw new InvalidArgumentException(sprintf('Class "%s" does not exist', $resultRendererClass));
}

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

foreach ($this->exercises as $exercise) {
if (false === $container->has($exercise)) {
throw new \RuntimeException(
throw new RuntimeException(
sprintf('No DI config found for exercise: "%s". Register a factory.', $exercise)
);
}
Expand All @@ -176,7 +181,7 @@ public function run(): int
$checkRepository = $container->get(CheckRepository::class);
foreach ($this->checks as $check) {
if (false === $container->has($check)) {
throw new \RuntimeException(
throw new RuntimeException(
sprintf('No DI config found for check: "%s". Register a factory.', $check)
);
}
Expand Down Expand Up @@ -206,7 +211,7 @@ public function run(): int
)
);
return 1;
} catch (\RuntimeException $e) {
} catch (RuntimeException $e) {
$container
->get(OutputInterface::class)
->printError(
Expand All @@ -221,9 +226,9 @@ public function run(): int
}

/**
* @return \DI\Container
* @return Container
*/
private function getContainer(): \DI\Container
private function getContainer(): Container
{
$containerBuilder = new ContainerBuilder();
$containerBuilder->addDefinitions(
Expand Down
4 changes: 2 additions & 2 deletions src/Check/CodeParseCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
namespace PhpSchool\PhpWorkshop\Check;

use PhpParser\Error;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\NodeTraverser;
use PhpParser\Parser;
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
Expand Down Expand Up @@ -66,6 +64,8 @@ public function check(ExerciseInterface $exercise, Input $input): ResultInterfac

/**
* This check can run on any exercise type.
* @param ExerciseType $exerciseType
* @return bool
*/
public function canRun(ExerciseType $exerciseType): bool
{
Expand Down
7 changes: 6 additions & 1 deletion src/Check/ComposerCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace PhpSchool\PhpWorkshop\Check;

use InvalidArgumentException;
use PhpSchool\PhpWorkshop\ComposerUtil\LockFileParser;
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
Expand Down Expand Up @@ -35,11 +36,12 @@ public function getName(): string
* @param ExerciseInterface $exercise The exercise to check against.
* @param Input $input The command line arguments passed to the command.
* @return ResultInterface The result of the check.
* @noinspection SpellCheckingInspection
*/
public function check(ExerciseInterface $exercise, Input $input): ResultInterface
{
if (!$exercise instanceof ComposerExerciseCheck) {
throw new \InvalidArgumentException();
throw new InvalidArgumentException();
}

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

/**
* This check can run on any exercise type.
*
* @param ExerciseType $exerciseType
* @return bool
*/
public function canRun(ExerciseType $exerciseType): bool
{
Expand Down
12 changes: 8 additions & 4 deletions src/Check/DatabaseCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
namespace PhpSchool\PhpWorkshop\Check;

use PDO;
use PDOException;
use PhpSchool\PhpWorkshop\Event\CliExecuteEvent;
use PhpSchool\PhpWorkshop\Event\Event;
use PhpSchool\PhpWorkshop\Event\EventDispatcher;
use PhpSchool\PhpWorkshop\Exercise\TemporaryDirectoryTrait;
use PhpSchool\PhpWorkshop\ExerciseCheck\DatabaseExerciseCheck;
use PhpSchool\PhpWorkshop\Result\Failure;
use PhpSchool\PhpWorkshop\Result\Success;
use RuntimeException;

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

/**
* Here we attach to various events to seed, verify and inject the DSN's
* to the student & reference solution programs's CLI arguments.
* to the student & reference solution programs' CLI arguments.
*
* @param EventDispatcher $eventDispatcher
*/
public function attach(EventDispatcher $eventDispatcher): void
{
if (file_exists($this->databaseDirectory)) {
throw new \RuntimeException(
throw new RuntimeException(
sprintf('Database directory: "%s" already exists', $this->databaseDirectory)
);
}
Expand All @@ -90,7 +94,7 @@ public function attach(EventDispatcher $eventDispatcher): void
try {
$db = new PDO($this->userDsn);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (\PDOException $e) {
} catch (PDOException $e) {
rmdir($this->databaseDirectory);
throw $e;
}
Expand Down Expand Up @@ -132,7 +136,7 @@ function (CliExecuteEvent $e) {
'verify.finish',
'run.finish'
],
function (Event $e) use ($db) {
function () use ($db) {
unset($db);
@unlink($this->userDatabasePath);
@unlink($this->solutionDatabasePath);
Expand Down
5 changes: 4 additions & 1 deletion src/Check/FileExistsCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public function check(ExerciseInterface $exercise, Input $input): ResultInterfac

/**
* This check can run on any exercise type.
*
* @param ExerciseType $exerciseType
* @return bool
*/
public function canRun(ExerciseType $exerciseType): bool
{
Expand All @@ -57,7 +60,7 @@ public function getExerciseInterface(): string
}

/**
* This check must run before executing the solution becuase it may not exist.
* This check must run before executing the solution because it may not exist.
*/
public function getPosition(): string
{
Expand Down
8 changes: 5 additions & 3 deletions src/Check/FunctionRequirementsCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace PhpSchool\PhpWorkshop\Check;

use InvalidArgumentException;
use PhpParser\Error;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name;
use PhpParser\NodeTraverser;
use PhpParser\Parser;
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
Expand All @@ -18,7 +18,6 @@
use PhpSchool\PhpWorkshop\Result\FunctionRequirementsFailure;
use PhpSchool\PhpWorkshop\Result\ResultInterface;
use PhpSchool\PhpWorkshop\Result\Success;
use PhpSchool\PhpWorkshop\Utils\Collection;

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

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

/**
* This check can run on any exercise type.
*
* @param ExerciseType $exerciseType
* @return bool
*/
public function canRun(ExerciseType $exerciseType): bool
{
Expand Down
2 changes: 2 additions & 0 deletions src/Check/ListenableCheckInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ interface ListenableCheckInterface extends CheckInterface
/**
* Attach to events throughout the running/verifying process. Inject verifiers
* and listeners.
*
* @param EventDispatcher $eventDispatcher
*/
public function attach(EventDispatcher $eventDispatcher): void;
}
3 changes: 3 additions & 0 deletions src/Check/PhpLintCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ public function check(ExerciseInterface $exercise, Input $input): ResultInterfac

/**
* This check can run on any exercise type.
*
* @param ExerciseType $exerciseType
* @return bool
*/
public function canRun(ExerciseType $exerciseType): bool
{
Expand Down
3 changes: 3 additions & 0 deletions src/Check/SimpleCheckInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ interface SimpleCheckInterface extends CheckInterface

/**
* Can this check run this exercise?
*
* @param ExerciseType $exerciseType
* @return bool
*/
public function canRun(ExerciseType $exerciseType): bool;

Expand Down
1 change: 0 additions & 1 deletion src/Command/PrintCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace PhpSchool\PhpWorkshop\Command;

use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
use PhpSchool\PhpWorkshop\ExerciseRepository;
use PhpSchool\PhpWorkshop\MarkdownRenderer;
use PhpSchool\PhpWorkshop\Output\OutputInterface;
Expand Down
1 change: 0 additions & 1 deletion src/Command/VerifyCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use PhpSchool\PhpWorkshop\ExerciseDispatcher;
use PhpSchool\PhpWorkshop\ExerciseRepository;
use PhpSchool\PhpWorkshop\ExerciseRunner;
use PhpSchool\PhpWorkshop\Input\Input;
use PhpSchool\PhpWorkshop\Output\OutputInterface;
use PhpSchool\PhpWorkshop\ResultRenderer\ResultsRenderer;
Expand Down
14 changes: 8 additions & 6 deletions src/CommandRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

namespace PhpSchool\PhpWorkshop;

use InvalidArgumentException;
use PhpSchool\PhpWorkshop\Event\EventDispatcher;
use PhpSchool\PhpWorkshop\Exception\CliRouteNotExistsException;
use PhpSchool\PhpWorkshop\Exception\MissingArgumentException;
use Psr\Container\ContainerInterface;
use PhpSchool\PhpWorkshop\Input\Input;
use RuntimeException;

/**
* Parses $argv (or passed array) and attempts to find a command
Expand Down Expand Up @@ -60,7 +62,7 @@ public function __construct(
}

if (!isset($this->commands[$default])) {
throw new \InvalidArgumentException(sprintf('Default command: "%s" is not available', $default));
throw new InvalidArgumentException(sprintf('Default command: "%s" is not available', $default));
}
$this->defaultCommand = $default;
$this->eventDispatcher = $eventDispatcher;
Expand All @@ -73,7 +75,7 @@ public function __construct(
private function addCommand(CommandDefinition $c): void
{
if (isset($this->commands[$c->getName()])) {
throw new \InvalidArgumentException(sprintf('Command with name: "%s" already exists', $c->getName()));
throw new InvalidArgumentException(sprintf('Command with name: "%s" already exists', $c->getName()));
}

$this->commands[$c->getName()] = $c;
Expand All @@ -100,7 +102,7 @@ public function route(array $args = null): int
{

if (null === $args) {
$args = isset($_SERVER['argv']) ? $_SERVER['argv'] : [];
$args = $_SERVER['argv'] ?? [];
}

$appName = array_shift($args);
Expand Down Expand Up @@ -196,17 +198,17 @@ private function resolveCallable(CommandDefinition $command, Input $input): int
}

if (!is_string($commandCallable)) {
throw new \RuntimeException('Callable must be a callable or a container entry for a callable service');
throw new RuntimeException('Callable must be a callable or a container entry for a callable service');
}

if (!$this->container->has($commandCallable)) {
throw new \RuntimeException(sprintf('Container has no entry named: "%s"', $commandCallable));
throw new RuntimeException(sprintf('Container has no entry named: "%s"', $commandCallable));
}

$callable = $this->container->get($commandCallable);

if (!is_callable($callable)) {
throw new \RuntimeException(sprintf('Container entry: "%s" not callable', $commandCallable));
throw new RuntimeException(sprintf('Container entry: "%s" not callable', $commandCallable));
}

$return = $this->callCommand($command, $callable, $input);
Expand Down
2 changes: 1 addition & 1 deletion src/ComposerUtil/LockFileParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function getInstalledPackages(): array
* @param string $packageName
* @return bool
*/
public function hasInstalledPackage($packageName): bool
public function hasInstalledPackage(string $packageName): bool
{
foreach ($this->contents['packages'] as $packageDetails) {
if ($packageName === $packageDetails['name']) {
Expand Down
2 changes: 0 additions & 2 deletions src/Event/EventDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,6 @@ public function insertVerifier(string $eventName, callable $verifier): void
//return type hints pls
if ($result instanceof ResultInterface) {
$this->resultAggregator->add($result);
} else {
//??!!
}
});
}
Expand Down
Loading