From b5f12d1986b5211aab476d828f1b0ef2929e812c Mon Sep 17 00:00:00 2001 From: Steven Wichers Date: Sun, 3 Aug 2025 22:43:23 -0700 Subject: [PATCH 1/5] Remove negated logic --- src/Parser/Dig.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/Parser/Dig.php b/src/Parser/Dig.php index ad9427e..28f4b58 100644 --- a/src/Parser/Dig.php +++ b/src/Parser/Dig.php @@ -18,11 +18,7 @@ class Dig extends BlueprintParserBase */ public function checkHeader(): bool { - $header = $this->blueprintHeader; - if ($header['command'] !== 'dig') { - return false; - } - - return true; + $command = $this->blueprintHeader['command'] ?? null; + return $command === 'dig'; } } From 6138f22af65e6f23955e113abb7f3cf4dfc99c1f Mon Sep 17 00:00:00 2001 From: Steven Wichers Date: Sun, 3 Aug 2025 22:49:36 -0700 Subject: [PATCH 2/5] Minor type related updates --- src/Parser/BlueprintParserBase.php | 14 +++++++------- src/Parser/BlueprintParserInterface.php | 2 +- src/Parser/Command.php | 22 +++++++++++----------- src/Parser/Dig.php | 2 +- 4 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/Parser/BlueprintParserBase.php b/src/Parser/BlueprintParserBase.php index 25eb9fc..21497eb 100644 --- a/src/Parser/BlueprintParserBase.php +++ b/src/Parser/BlueprintParserBase.php @@ -15,30 +15,30 @@ class BlueprintParserBase implements BlueprintParserInterface * * @var array * - * @see \QuickFort\Parser\BlueprintParserInterface::getHeader(). + * @see BlueprintParserInterface::getHeader */ - protected $blueprintHeader; + protected array $blueprintHeader; /** * Original blueprint text. * * @var string */ - protected $originalBlueprint; + protected string $originalBlueprint; /** * The lines of the blueprint, minus the header. * * @var string[] */ - protected $blueprintLines; + protected array $blueprintLines; /** * BlueprintParserBase constructor. * - * @param string $blueprintText A blueprint to initialize with. + * @param null|string $blueprintText A blueprint to initialize with. */ - public function __construct(string $blueprintText = null) + public function __construct(?string $blueprintText = null) { if (!empty($blueprintText)) { $this->setBlueprint($blueprintText); @@ -94,7 +94,7 @@ protected function textToLines(string $text): array * @return array * An array of header information. * - * @see \QuickFort\Parser\BlueprintParserInterface::getHeader(). + * @see BlueprintParserInterface::getHeader */ protected function parseLineAsHeader(string $line): array { diff --git a/src/Parser/BlueprintParserInterface.php b/src/Parser/BlueprintParserInterface.php index cfd9959..c663c7c 100644 --- a/src/Parser/BlueprintParserInterface.php +++ b/src/Parser/BlueprintParserInterface.php @@ -28,7 +28,7 @@ interface BlueprintParserInterface * * @return void */ - public function setBlueprint(string $blueprintText); + public function setBlueprint(string $blueprintText): void; /** * Get the processed blueprint layers. diff --git a/src/Parser/Command.php b/src/Parser/Command.php index d08b9b4..588a84f 100644 --- a/src/Parser/Command.php +++ b/src/Parser/Command.php @@ -15,14 +15,14 @@ class Command * * @var string */ - protected $command; + protected string $command; /** * A keyed array (x,y) of expansion data. * * @var array */ - protected $expansion; + protected array $expansion; /** * Command constructor. @@ -79,12 +79,12 @@ protected function parseTextToCommand(string $text): void /** * Check if the command is an up layer navigation. * - * @return boolean + * @return bool * Returns true if the command moves up a layer. */ public function isLayerUp(): bool { - return $this->command == '#<'; + return $this->command === '#<'; } /** @@ -95,39 +95,39 @@ public function isLayerUp(): bool */ public function isLayerDown(): bool { - return $this->command == '#>'; + return $this->command === '#>'; } /** * Check if the command is allowed. * - * @return boolean + * @return bool * Returns true if the command is an allowed command. */ public function isAllowedCommand(): bool { $commands = 'djuihrx'; - return in_array($this->command, str_split($commands)); + return in_array($this->command, str_split($commands), true); } /** * Check if the command results in no operation. * - * @return boolean + * @return bool * Returns true if there is no operation to perform. */ public function isNoOp(): bool { $noops = '#~`'; - return in_array($this->command, str_split($noops)); + return in_array($this->command, str_split($noops), true); } /** * Check if the command was a comment. * - * @return boolean + * @return bool * Returns true if the command is a comment. */ public function isComment(): bool @@ -162,7 +162,7 @@ public function getFormatted(): string /** * Check if the command has expansion information. * - * @return boolean + * @return bool * Returns true if the command has worthwhile expansion information. */ public function hasExpansion(): bool diff --git a/src/Parser/Dig.php b/src/Parser/Dig.php index 28f4b58..e991679 100644 --- a/src/Parser/Dig.php +++ b/src/Parser/Dig.php @@ -13,7 +13,7 @@ class Dig extends BlueprintParserBase /** * Check if the header is valid for this type of parser. * - * @return boolean + * @return bool * Returns true if the blueprint is a dig command. */ public function checkHeader(): bool From 692a6d353c51cebadde3b9b627bfc18104d1d719 Mon Sep 17 00:00:00 2001 From: Steven Wichers Date: Sun, 3 Aug 2025 22:53:47 -0700 Subject: [PATCH 3/5] Use more modern PHP str functions --- src/Parser/BlueprintParserBase.php | 4 ++-- src/Parser/Command.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Parser/BlueprintParserBase.php b/src/Parser/BlueprintParserBase.php index 21497eb..1fa03fd 100644 --- a/src/Parser/BlueprintParserBase.php +++ b/src/Parser/BlueprintParserBase.php @@ -105,7 +105,7 @@ protected function parseLineAsHeader(string $line): array ]; $line = trim($line); - if (0 !== stripos($line, '#')) { + if (!str_starts_with($line, '#')) { return $header; } @@ -133,7 +133,7 @@ protected function parseLineAsHeader(string $line): array $line = trim(substr($line, $command_end_pos)); // Parse out starting position information. - if (0 === strpos($line, 'start(')) { + if (str_starts_with($line, 'start(')) { $start_len = strlen('start('); $closing_paren_pos = strpos($line, ')'); diff --git a/src/Parser/Command.php b/src/Parser/Command.php index 588a84f..b2b9735 100644 --- a/src/Parser/Command.php +++ b/src/Parser/Command.php @@ -65,7 +65,7 @@ protected function parseTextToCommand(string $text): void 'y' => 1, ]; - if (strpos($text, '(') !== false) { + if (str_contains($text, '(')) { $parts = explode('(', trim($text, ')')); $xy_values = explode('x', $parts[1]); $this->command = $parts[0]; From 9411045cdcee62351d5a6f56f3f7b0c408651057 Mon Sep 17 00:00:00 2001 From: Steven Wichers Date: Sun, 3 Aug 2025 23:07:28 -0700 Subject: [PATCH 4/5] Clean up Command implementation --- src/Enums/DigCommands.php | 14 +++++++ src/Enums/LayerCommands.php | 9 +++++ src/Parser/Command.php | 37 +++++++++++++------ .../DataProviders/CommandDataProviders.php | 26 ++++++------- 4 files changed, 61 insertions(+), 25 deletions(-) create mode 100644 src/Enums/DigCommands.php create mode 100644 src/Enums/LayerCommands.php diff --git a/src/Enums/DigCommands.php b/src/Enums/DigCommands.php new file mode 100644 index 0000000..7c7a497 --- /dev/null +++ b/src/Enums/DigCommands.php @@ -0,0 +1,14 @@ +'; +} diff --git a/src/Parser/Command.php b/src/Parser/Command.php index b2b9735..78da704 100644 --- a/src/Parser/Command.php +++ b/src/Parser/Command.php @@ -2,6 +2,9 @@ namespace QuickFort\Parser; +use QuickFort\Enums\DigCommands; +use QuickFort\Enums\LayerCommands; + /** * Class Command * @@ -66,16 +69,28 @@ protected function parseTextToCommand(string $text): void ]; if (str_contains($text, '(')) { - $parts = explode('(', trim($text, ')')); - $xy_values = explode('x', $parts[1]); - $this->command = $parts[0]; - $this->expansion = [ - 'x' => $xy_values[0], - 'y' => $xy_values[1], - ]; + $this->parseTextWithExpansion($text); } } + /** + * Parses command text that contains an expansion marker + * + * @param string $text The text to parse into command data. + * + * @return void + */ + protected function parseTextWithExpansion(string $text): void + { + $parts = explode('(', trim($text, ')')); + $xy_values = explode('x', $parts[1]); + $this->command = $parts[0]; + $this->expansion = [ + 'x' => $xy_values[0], + 'y' => $xy_values[1], + ]; + } + /** * Check if the command is an up layer navigation. * @@ -84,7 +99,7 @@ protected function parseTextToCommand(string $text): void */ public function isLayerUp(): bool { - return $this->command === '#<'; + return $this->command === LayerCommands::UP->value; } /** @@ -95,7 +110,7 @@ public function isLayerUp(): bool */ public function isLayerDown(): bool { - return $this->command === '#>'; + return $this->command === LayerCommands::DOWN->value; } /** @@ -106,9 +121,7 @@ public function isLayerDown(): bool */ public function isAllowedCommand(): bool { - $commands = 'djuihrx'; - - return in_array($this->command, str_split($commands), true); + return DigCommands::tryFrom($this->command) !== null; } /** diff --git a/tests/Unit/DataProviders/CommandDataProviders.php b/tests/Unit/DataProviders/CommandDataProviders.php index 1fb26cf..0dc178e 100644 --- a/tests/Unit/DataProviders/CommandDataProviders.php +++ b/tests/Unit/DataProviders/CommandDataProviders.php @@ -2,6 +2,9 @@ namespace QuickFort\tests\Unit\DataProviders; +use QuickFort\Enums\DigCommands; +use QuickFort\Enums\LayerCommands; + class CommandDataProviders { public static function commandWithExpansion(): array @@ -23,14 +26,11 @@ public static function commandHasExpansion():array { public static function allowedCommands(): array { - return [ - ['d'], - ['j'], - ['u'], - ['h'], - ['r'], - ['x'], - ]; + $data = []; + foreach (DigCommands::cases() as $case) { + $data[] = [$case->value]; + } + return $data; } public static function disallowedCommands(): array @@ -42,16 +42,16 @@ public static function disallowedCommands(): array public static function complexCommandWithBase():array { return [ - ['d', 'd'], - ['d(3x3)', 'd'], - ['d(1x3)', 'd'], + ['d',DigCommands::DIG->value], + ['d(3x3)',DigCommands::DIG->value], + ['d(1x3)',DigCommands::DIG->value], ]; } public static function layerShifting():array { return [ - 'layer shift up' => ['#<', true], - 'layer shift down' => ['#>', false], + 'layer shift up' => [LayerCommands::UP->value, true], + 'layer shift down' => [LayerCommands::DOWN->value, false], ]; } From 828a5f454b8dfee9d0c7a059aabe4b3d69da1745 Mon Sep 17 00:00:00 2001 From: Steven Wichers Date: Sun, 3 Aug 2025 23:18:50 -0700 Subject: [PATCH 5/5] Update README --- README.md | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 865bc87..1dae177 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ # QuickFort Blueprint Parser -Library for parsing QuickFort blueprints. +This is a simple library for parsing basic QuickFort blueprints. Only dig +blueprints are implemented. There are no plans to actively work on this project, +but pull requests for new features and layer types will be accepted. ## Example @@ -50,3 +52,29 @@ $layers = $parser->getLayers(); [![Build Status](https://travis-ci.com/swichers/php-quickfort-parser.svg?branch=master)](https://travis-ci.com/swichers/php-quickfort-parser) [![Codacy Badge](https://api.codacy.com/project/badge/Grade/07a22d50e78e4b66b25d0dad19567d81)](https://www.codacy.com/app/swichers/php-quickfort-parser?utm_source=github.com&utm_medium=referral&utm_content=swichers/php-quickfort-parser&utm_campaign=Badge_Grade) [![Codacy Badge](https://api.codacy.com/project/badge/Coverage/07a22d50e78e4b66b25d0dad19567d81)](https://www.codacy.com/app/swichers/php-quickfort-parser?utm_source=github.com&utm_medium=referral&utm_content=swichers/php-quickfort-parser&utm_campaign=Badge_Coverage) + +```text +###*:::=####*:*############################ + =++##*******++++ + +++++++++*#++ + -+++++++*++ + -#####**#+ + ...... -##*+*#+#* + .:=====+--===.. :-#*+*#*#* + . := .*###*+#+ + := .####### + := =#*++* + := @@@%: -+#### + -##@@@@@@: -++++* + -##@####. =####+ + -###@@@%-. =###++ + -######%@: =###+* + -#####%%@-. =*##+* + -####%%@@@#+. .=***#++ + -######@@@@%=. .***#*++ + -##=:::-#=---. +#**++** + .=#*.. .*#- -*######## + .+##**. .*##+..+**#####*+## +#-.:-+###.*################################ +########################################### +```