Skip to content

Commit

Permalink
test: improve code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
g105b committed May 9, 2024
1 parent 4a590ef commit 74f5d57
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 49 deletions.
1 change: 0 additions & 1 deletion phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
<rule ref="Generic.Files.EndFileNewline" />
<rule ref="Generic.Files.InlineHTML" />
<rule ref="Generic.Files.LineEndings" />
<rule ref="Generic.Files.LineLength" />
<rule ref="Generic.Files.OneClassPerFile" />
<rule ref="Generic.Files.OneInterfacePerFile" />
<rule ref="Generic.Files.OneObjectStructurePerFile" />
Expand Down
1 change: 1 addition & 0 deletions phpmd.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

<rule ref="rulesets/design.xml">
<exclude name="NumberOfChildren" />
<exclude name="ExitExpression" />
</rule>

<rule ref="rulesets/naming.xml">
Expand Down
3 changes: 2 additions & 1 deletion src/Command/BuildCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
namespace Gt\GtCommand\Command;

use Gt\Cli\Argument\ArgumentValueList;
use Gt\Build\Cli\RunCommand as CliRunCommand;

class BuildCommand extends AbstractProxyCommand {
public function __construct() {
$this->proxyCommand = new \Gt\Build\Cli\RunCommand();
$this->proxyCommand = new CliRunCommand();
}

public function run(ArgumentValueList $arguments = null):void {
Expand Down
97 changes: 56 additions & 41 deletions src/Command/CreateCommand.php
Original file line number Diff line number Diff line change
@@ -1,55 +1,22 @@
<?php
namespace Gt\GtCommand\Command;

use Gt\Cli\Argument\ArgumentValue;
use Gt\Cli\Argument\ArgumentValueList;
use Gt\Cli\Command\Command;
use Gt\Cli\Parameter\NamedParameter;
use Gt\Cli\Parameter\Parameter;
use Gt\Cli\Stream;
use Gt\Daemon\Process;
use Gt\GtCommand\Blueprint\BlueprintCollection;

class CreateCommand extends Command {
/**
* TODO: Simplify this function
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function run(ArgumentValueList $arguments = null):void {
$name = $arguments->get("projectName", "");

$i = 0;
while(!$this->isValidName($name)) {
if($i > 0) {
$this->writeLine("The name '$name' is not a valid directory name.", Stream::ERROR);
$this->writeLine("Please use only letters, numbers and underscores when naming.", Stream::ERROR);
}

$this->writeLine("What is the name of your project? (This will name its directory)");
$name = $this->readLine();
$i++;
}

if(file_exists($name)) {
$type = is_file($name) ? "file" : "directory";
$this->writeLine("Oops - there's already a $type called '$name' in the current directory.");
exit(1);
}

$this->writeLine();
$this->writeLine("Creating application '$name' in: " . getcwd() . "/$name");
$this->writeLine();

$namespace = $arguments->get("namespace", "");
$i = 0;
while(!$this->isValidNamespace($namespace)) {
if($i > 0) {
$this->writeLine("The namespace '$namespace' is not a valid PHP Namespace.", Stream::ERROR);
}

$this->writeLine("What namespace would you like to use for autoloading classes?");
$namespace = "App";
$namespace = $this->readLine($namespace);
$i++;
}
$this->writeLine();
$this->writeLine("Using namespace '$namespace'.");
$this->writeLine();
$name = $this->readValidName($arguments->get("projectName", ""));
$namespace = $this->readValidNamespace($arguments->get("namespace", ""));

$blueprintCollection = new BlueprintCollection();
$blueprintInput = "0";
Expand All @@ -68,7 +35,7 @@ public function run(ArgumentValueList $arguments = null):void {

if($blueprintInput < 0 || $blueprintInput >= count($blueprintCollection)) {
$this->writeLine("Cancelling due to invalid blueprint.");
exit;
exit; // phpcs:ignore
}
}

Expand Down Expand Up @@ -183,4 +150,52 @@ private function isValidNamespace(string $namespace):bool {

return !preg_match("/[^\w\\\]+/", $namespace);
}

private function readValidName(string $name):string {
$i = 0;

while(!$this->isValidName($name)) {
if($i > 0) {
$this->writeLine("The name '$name' is not a valid directory name.", Stream::ERROR);
$this->writeLine("Please use only letters, numbers and underscores when naming.", Stream::ERROR);
}

$this->writeLine("What is the name of your project? (This will name its directory)");
$name = $this->readLine();
$i++;
}

if(file_exists($name)) {
$type = is_file($name) ? "file" : "directory";
$this->writeLine("Oops - there's already a $type called '$name' in the current directory.");
exit(1); // phpcs:ignore
}

$this->writeLine();
$this->writeLine("Creating application '$name' in: " . getcwd() . "/$name");
$this->writeLine();

return $name;
}

private function readValidNamespace(ArgumentValue $namespace):string {
$i = 0;
while(!$this->isValidNamespace($namespace)) {
if($i > 0) {
$this->writeLine("The namespace '$namespace' is not a valid PHP Namespace.", Stream::ERROR);
}

$this->writeLine("What namespace would you like to use for autoloading classes?");
$namespace = "App";
$namespace = $this->readLine($namespace);
$i++;
}
$this->writeLine();
$this->writeLine("Using namespace '$namespace'.");
$this->writeLine();

return $namespace;
}


}
5 changes: 3 additions & 2 deletions src/Command/DeployCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
namespace Gt\GtCommand\Command;

use Gt\Cli\Argument\ArgumentValueList;
use Gt\Cli\Command\Command;
use Gt\Cli\Parameter\NamedParameter;
use Gt\Cli\Parameter\Parameter;

class DeployCommand extends AbstractProxyCommand {
/** @SuppressWarnings(PHPMD.UnusedFormalParameter) */
public function __construct() {
// TODO:
$this->proxyCommand = new class extends \Gt\Cli\Command\Command {
$this->proxyCommand = new class extends Command {
public function getDescription():string {
return "Not yet implemented";
}
Expand Down
14 changes: 10 additions & 4 deletions src/Command/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,23 @@
use Gt\Daemon\Process;

class RunCommand extends Command {
/**
* TODO: Simplify this function.
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function run(ArgumentValueList $arguments = null):void {
global $argv;

$serveArgs = [];
if($arguments->contains("debug")) {
array_push($serveArgs, "--debug");
}
$bindValue = $arguments->get("bind") ?? "0.0.0.0";
$bindValue = $arguments->get("bind", "0.0.0.0");
array_push($serveArgs, "--bind", $bindValue);
$portValue = $arguments->get("port") ?? "8080";
$portValue = $arguments->get("port", "8080");
array_push($serveArgs, "--port", $portValue);
$threadsValue = $arguments->get("threads") ?? "8";
$threadsValue = $arguments->get("threads", "8");
array_push($serveArgs, "--threads", $threadsValue);

$processList = [
Expand Down Expand Up @@ -61,7 +66,8 @@ public function run(ArgumentValueList $arguments = null):void {

usleep(100_000);
if($processList["serve"]->isRunning()) {
$this->writeLine("To view your application in your browser, visit: $localUrl");
$this->writeLine("To view your application in your "
. " browser, visit: $localUrl");
$this->writeLine("To stop running, press [CTRL]+[C].");
$this->writeLine();
}
Expand Down

0 comments on commit 74f5d57

Please sign in to comment.