From 8574f6ef4577c187ede732d5acc87f49bb9a87c4 Mon Sep 17 00:00:00 2001 From: Fahmi Auliya Date: Wed, 25 Jun 2025 09:54:10 +0700 Subject: [PATCH 1/2] [ENH] Optimize code with PHP-CS-Fixer --- rector.php | 7 +- source/AlnumVigenereCipher.php | 35 +++--- source/BasicVigenereCipher.php | 34 ++---- source/VigenereCipher.php | 8 +- source/VigenereCipherBlueprint.php | 115 +++++--------------- source/exceptions/InvalidAlnumException.php | 4 +- source/exceptions/InvalidBasicException.php | 4 +- tests/VigenereCipherTest.php | 20 ++-- 8 files changed, 76 insertions(+), 151 deletions(-) diff --git a/rector.php b/rector.php index e5eb1f2..5da381e 100644 --- a/rector.php +++ b/rector.php @@ -6,11 +6,12 @@ return RectorConfig::configure() ->withPaths([ - __DIR__ . '/source', - __DIR__ . '/tests', + __DIR__.'/source', + __DIR__.'/tests', ]) // uncomment to reach your current PHP version // ->withPhpSets() ->withTypeCoverageLevel(0) ->withDeadCodeLevel(0) - ->withCodeQualityLevel(0); + ->withCodeQualityLevel(0) +; diff --git a/source/AlnumVigenereCipher.php b/source/AlnumVigenereCipher.php index a2a336b..c2a152e 100644 --- a/source/AlnumVigenereCipher.php +++ b/source/AlnumVigenereCipher.php @@ -6,10 +6,12 @@ use amculin\cryptography\classic\exceptions\InvalidAlnumException; /** - * This file is the main class for alpha-numric mode vigenere cipher algortithm + * This file is the main class for alpha-numric mode vigenere cipher algortithm. * * @author Fahmi Auliya Tsani + * * @version 1.1 + * * @psalm-api */ class AlnumVigenereCipher extends VigenereCipherBlueprint @@ -39,60 +41,48 @@ public function __construct( } } - /** - * @inheritdoc - */ public function isValidKey(string $pattern): bool { - if ($pattern != '') { - return preg_match($pattern, $this->key) == 1; + if ('' != $pattern) { + return 1 == preg_match($pattern, $this->key); } return false; } - /** - * @inheritdoc - */ public function isValidPlainText(string $pattern): bool { - if ($pattern != '') { - return preg_match($pattern, $this->plainText) == 1; + if ('' != $pattern) { + return 1 == preg_match($pattern, $this->plainText); } return false; } - /** - * @inheritdoc - */ public function isValidCipherText(string $pattern): bool { - if ($pattern != '') { - return preg_match($pattern, $this->cipherText) == 1; + if ('' != $pattern) { + return 1 == preg_match($pattern, $this->cipherText); } return false; } - /** - * @inheritdoc - */ public function isValid(): bool { try { $pattern = '/^[a-zA-Z0-9]*$/'; - if (! $this->isValidKey($pattern)) { + if (!$this->isValidKey($pattern)) { throw new InvalidAlnumException('Key'); } if ($this->process == ProcessType::ENCRYPT->value) { - if (! $this->isValidPlainText($pattern)) { + if (!$this->isValidPlainText($pattern)) { throw new InvalidAlnumException('Plain text'); } } else { - if (! $this->isValidCipherText($pattern)) { + if (!$this->isValidCipherText($pattern)) { throw new InvalidAlnumException('Cipher text'); } } @@ -103,6 +93,7 @@ public function isValid(): bool } $this->setIsValid(true); + return true; } } diff --git a/source/BasicVigenereCipher.php b/source/BasicVigenereCipher.php index de740fe..bb5012e 100644 --- a/source/BasicVigenereCipher.php +++ b/source/BasicVigenereCipher.php @@ -6,10 +6,12 @@ use amculin\cryptography\classic\exceptions\InvalidBasicException; /** - * This file is the main class for basic vigenere cipher algortithm + * This file is the main class for basic vigenere cipher algortithm. * * @author Fahmi Auliya Tsani + * * @version 1.1 + * * @psalm-api */ #[\AllowDynamicProperties] @@ -40,60 +42,48 @@ public function __construct( } } - /** - * @inheritdoc - */ public function isValidKey(string $pattern): bool { - if ($pattern != '') { - return preg_match($pattern, $this->key) == 1; + if ('' != $pattern) { + return 1 == preg_match($pattern, $this->key); } return false; } - /** - * @inheritdoc - */ public function isValidPlainText(string $pattern): bool { - if ($pattern != '') { - return preg_match($pattern, $this->plainText) == 1; + if ('' != $pattern) { + return 1 == preg_match($pattern, $this->plainText); } return false; } - /** - * @inheritdoc - */ public function isValidCipherText(string $pattern): bool { - if ($pattern != '') { - return preg_match($pattern, $this->cipherText) == 1; + if ('' != $pattern) { + return 1 == preg_match($pattern, $this->cipherText); } return false; } - /** - * @inheritdoc - */ public function isValid(): bool { try { $pattern = '/^[a-z]*$/'; - if (! $this->isValidKey($pattern)) { + if (!$this->isValidKey($pattern)) { throw new InvalidBasicException('Key'); } if ($this->process == ProcessType::ENCRYPT->value) { - if (! $this->isValidPlainText($pattern)) { + if (!$this->isValidPlainText($pattern)) { throw new InvalidBasicException('Plain text'); } } else { - if (! $this->isValidCipherText($pattern)) { + if (!$this->isValidCipherText($pattern)) { throw new InvalidBasicException('Cipher text'); } } diff --git a/source/VigenereCipher.php b/source/VigenereCipher.php index 2fa54d3..cc995c3 100644 --- a/source/VigenereCipher.php +++ b/source/VigenereCipher.php @@ -2,9 +2,6 @@ namespace amculin\cryptography\classic; -use amculin\cryptography\classic\AlnumVigenereCipher; -use amculin\cryptography\classic\BasicVigenereCipher; -use amculin\cryptography\classic\VigenereCipherBlueprint; use amculin\cryptography\classic\enums\ProcessType; use amculin\cryptography\classic\enums\VigenereMode; @@ -22,7 +19,7 @@ public static function getClassName(string $mode): string $className = 'AlnumVigenereCipher'; } - return $path . $className; + return $path.$className; } public static function getClass( @@ -33,7 +30,8 @@ public static function getClass( ): VigenereCipherBlueprint { if ($mode == VigenereMode::ALPHA_NUMERIC->value) { return new AlnumVigenereCipher($data, $key, $processName); - } elseif ($mode == VigenereMode::BASE64->value) { + } + if ($mode == VigenereMode::BASE64->value) { return new Base64VigenereCipher($data, $key, $processName); } diff --git a/source/VigenereCipherBlueprint.php b/source/VigenereCipherBlueprint.php index 49c7b9e..ee99e8e 100644 --- a/source/VigenereCipherBlueprint.php +++ b/source/VigenereCipherBlueprint.php @@ -10,82 +10,57 @@ abstract class VigenereCipherBlueprint /** * Default list of acceptable character to be used in vigenere cipher algorithm * By default, it just an alphabetical list. - * - * @var string */ public string $tabulaRecta; /** - * The current process whether it is encrypt or decrypt - * - * @var string + * The current process whether it is encrypt or decrypt. */ public string $process; /** - * The plain text/message to be encrypted - * - * @var string + * The plain text/message to be encrypted. */ public string $plainText = ''; /** - * The key used to encrypt plain text/message - * - * @var string + * The key used to encrypt plain text/message. */ public string $key; /** * The isValid attribute to ensure whether the key, plainText, or cipherText - * is valid - * - * @var bool + * is valid. */ public bool $isValid = false; /** - * The cipher text to be decrypted - * - * @var string + * The cipher text to be decrypted. */ public string $cipherText = ''; /** - * Method to validate key, plainText, and cipherText - * - * @return bool + * Method to validate key, plainText, and cipherText. */ abstract public function isValid(): bool; /** - * Method to validate the key using regex pattern - * - * @param string $pattern - * @return bool + * Method to validate the key using regex pattern. */ abstract public function isValidKey(string $pattern): bool; /** - * Method to validate the plain text using regex pattern - * - * @param string $pattern - * @return bool + * Method to validate the plain text using regex pattern. */ abstract public function isValidPlainText(string $pattern): bool; /** - * Method to validate the cipher text using regex pattern - * - * @param string $pattern - * @return bool + * Method to validate the cipher text using regex pattern. */ abstract public function isValidCipherText(string $pattern): bool; /** - * Method to get is valid status - * - * @return bool + * Method to get is valid status. */ public function getIsValid(): bool { @@ -93,10 +68,7 @@ public function getIsValid(): bool } /** - * Set the is valid status - * - * @param bool $isValid - * @return void + * Set the is valid status. */ public function setIsValid(bool $isValid): void { @@ -104,9 +76,7 @@ public function setIsValid(bool $isValid): void } /** - * Method to get current process - * - * @return string + * Method to get current process. */ public function getProcess(): string { @@ -114,10 +84,7 @@ public function getProcess(): string } /** - * Set the current process - * - * @param string $process - * @return void + * Set the current process. */ public function setProcess(string $process): void { @@ -125,9 +92,7 @@ public function setProcess(string $process): void } /** - * Method to get plain text - * - * @return string + * Method to get plain text. */ public function getPlainText(): string { @@ -135,10 +100,7 @@ public function getPlainText(): string } /** - * Set the plain text/message/data to be encrypted - * - * @param string $plainText - * @return void + * Set the plain text/message/data to be encrypted. */ public function setPlainText(string $plainText): void { @@ -146,9 +108,7 @@ public function setPlainText(string $plainText): void } /** - * Method to get key - * - * @return string + * Method to get key. */ public function getKey(): string { @@ -156,10 +116,7 @@ public function getKey(): string } /** - * Set the key to be be used in encryption/decryption process - * - * @param string $key - * @return void + * Set the key to be be used in encryption/decryption process. */ public function setKey(string $key): void { @@ -169,9 +126,7 @@ public function setKey(string $key): void } /** - * Method to get cipher text - * - * @return string + * Method to get cipher text. */ public function getCipherText(): string { @@ -179,10 +134,7 @@ public function getCipherText(): string } /** - * Set the cipher text result from encryption process - * - * @param string $cipherText - * @return void + * Set the cipher text result from encryption process. */ public function setCipherText(string $cipherText): void { @@ -195,44 +147,39 @@ public function setCipherText(string $cipherText): void * Example: * Plain text: vigenerecipher (14 characters) * Key: abcd (4 characters) - * Repeated key: abcdabcdabcdab (14 characters) - * - * @param string $key - * @return string + * Repeated key: abcdabcdabcdab (14 characters). */ public function generateKey(string $key): string { $keyLength = strlen($key); - $messageLength = strlen($this->process == ProcessType::ENCRYPT->value ? $this->plainText : - $this->cipherText); + $messageLength = strlen($this->process == ProcessType::ENCRYPT->value ? $this->plainText + : $this->cipherText); $repeatTimes = floor($messageLength / $keyLength); $paddingKeyLength = (int) ($messageLength - ($keyLength * $repeatTimes)); $repeatedKey = ''; - for ($i = 0; $i < $repeatTimes; $i++) { + for ($i = 0; $i < $repeatTimes; ++$i) { $repeatedKey .= $key; } - return $repeatedKey . substr($key, 0, $paddingKeyLength); + return $repeatedKey.substr($key, 0, $paddingKeyLength); } /** - * Method to encrypt the plain text - * - * @return void + * Method to encrypt the plain text. */ public function encrypt(): void { $messageLength = strlen($this->plainText); $cipher = ''; - for ($i = 0; $i < $messageLength; $i++) { + for ($i = 0; $i < $messageLength; ++$i) { $messageCharPosition = strpos($this->tabulaRecta, substr($this->plainText, $i, 1)); $keyCharPosition = strpos($this->tabulaRecta, substr($this->key, $i, 1)); - if ($messageCharPosition !== false && $keyCharPosition !== false) { + if (false !== $messageCharPosition && false !== $keyCharPosition) { $shift = $messageCharPosition + $keyCharPosition; $cipherCharPosition = $shift % strlen($this->tabulaRecta); $cipher .= substr($this->tabulaRecta, $cipherCharPosition, 1); @@ -243,20 +190,18 @@ public function encrypt(): void } /** - * Method to decrypt the cipher text - * - * @return void + * Method to decrypt the cipher text. */ public function decrypt(): void { $messageLength = strlen($this->cipherText); $plain = ''; - for ($i = 0; $i < $messageLength; $i++) { + for ($i = 0; $i < $messageLength; ++$i) { $messageCharPosition = strpos($this->tabulaRecta, substr($this->cipherText, $i, 1)); $keyCharPosition = strpos($this->tabulaRecta, substr($this->key, $i, 1)); - if ($messageCharPosition !== false && $keyCharPosition !== false) { + if (false !== $messageCharPosition && false !== $keyCharPosition) { $shift = $messageCharPosition - $keyCharPosition; $plainCharPosition = $shift % strlen($this->tabulaRecta); diff --git a/source/exceptions/InvalidAlnumException.php b/source/exceptions/InvalidAlnumException.php index aa80020..2815c44 100644 --- a/source/exceptions/InvalidAlnumException.php +++ b/source/exceptions/InvalidAlnumException.php @@ -6,8 +6,8 @@ class InvalidAlnumException extends \Exception { public function errorMessage(): string { - $message = 'Error on line ' . $this->getLine() . ' in ' . $this->getFile() . ': '. PHP_EOL; - $message .= $this->getMessage() . ' is invalid, must be combination of a-z, A-Z, and 0-9!' . PHP_EOL; + $message = 'Error on line '.$this->getLine().' in '.$this->getFile().': '.PHP_EOL; + $message .= $this->getMessage().' is invalid, must be combination of a-z, A-Z, and 0-9!'.PHP_EOL; return $message; } diff --git a/source/exceptions/InvalidBasicException.php b/source/exceptions/InvalidBasicException.php index f472824..4502192 100644 --- a/source/exceptions/InvalidBasicException.php +++ b/source/exceptions/InvalidBasicException.php @@ -6,8 +6,8 @@ class InvalidBasicException extends \Exception { public function errorMessage(): string { - $message = 'Error on line ' . $this->getLine() . ' in ' . $this->getFile() . ': '. PHP_EOL; - $message .= $this->getMessage() . ' is invalid, must be combination of a-z!' . PHP_EOL; + $message = 'Error on line '.$this->getLine().' in '.$this->getFile().': '.PHP_EOL; + $message .= $this->getMessage().' is invalid, must be combination of a-z!'.PHP_EOL; return $message; } diff --git a/tests/VigenereCipherTest.php b/tests/VigenereCipherTest.php index cde27ca..38a8e58 100644 --- a/tests/VigenereCipherTest.php +++ b/tests/VigenereCipherTest.php @@ -15,7 +15,7 @@ final class VigenereCipherTest extends TestCase public const ALNUM_ALLOWED_CHARS = '/[a-zA-Z0-9]/'; public const BASE64_ALLOWED_CHARS = '/[A-Za-z0-9+\/=]/'; - public function testCanEncryptInBasicMode():void + public function testCanEncryptInBasicMode(): void { $allowedChars = $this::BASIC_ALLOWED_CHARS; $data = 'encryptionprocess'; @@ -31,7 +31,7 @@ public function testCanEncryptInBasicMode():void $this->assertMatchesRegularExpression($allowedChars, $encrypted); } - public function testCanNotEncryptInBasicModeWithInvalidKey():void + public function testCanNotEncryptInBasicModeWithInvalidKey(): void { $data = 'encryptionprocess'; $key = 'thekey-'; @@ -41,7 +41,7 @@ public function testCanNotEncryptInBasicModeWithInvalidKey():void $this->assertEquals('', $encrypted); } - public function testCanDecryptInBasicMode():void + public function testCanDecryptInBasicMode(): void { $allowedChars = $this::BASIC_ALLOWED_CHARS; $data = 'xugbcnmpsxtphjicw'; @@ -57,7 +57,7 @@ public function testCanDecryptInBasicMode():void $this->assertMatchesRegularExpression($allowedChars, $decrypted); } - public function testCanNotDecryptInBasicModeWithInvalidKey():void + public function testCanNotDecryptInBasicModeWithInvalidKey(): void { $data = 'xugbcnmpsxtphjicw'; $key = 'thekey-'; @@ -67,7 +67,7 @@ public function testCanNotDecryptInBasicModeWithInvalidKey():void $this->assertEquals('', $decrypted); } - public function testCanEncryptInAlphaNumericMode():void + public function testCanEncryptInAlphaNumericMode(): void { $allowedChars = $this::ALNUM_ALLOWED_CHARS; $data = 'Encrypti0nProC3s5'; @@ -83,7 +83,7 @@ public function testCanEncryptInAlphaNumericMode():void $this->assertMatchesRegularExpression($allowedChars, $encrypted); } - public function testCanNotEncryptInAlphaNumericModeWithInvalidKey():void + public function testCanNotEncryptInAlphaNumericModeWithInvalidKey(): void { $data = 'Encrypti0nProC3s5'; $key = 'th3kEy-'; @@ -93,7 +93,7 @@ public function testCanNotEncryptInAlphaNumericModeWithInvalidKey():void $this->assertEquals('', $encrypted); } - public function testCanDecryptWithAlphaNumericMode():void + public function testCanDecryptWithAlphaNumericMode(): void { $allowedChars = $this::ALNUM_ALLOWED_CHARS; $data = 'Xu5B2NMpTxjPHJWCz'; @@ -109,7 +109,7 @@ public function testCanDecryptWithAlphaNumericMode():void $this->assertMatchesRegularExpression($allowedChars, $decrypted); } - public function testCanNotDecryptInAlphaNumericModeWithInvalidKey():void + public function testCanNotDecryptInAlphaNumericModeWithInvalidKey(): void { $data = 'Xu5B2NMpTxjPHJWCz'; $key = 'th3kEy-'; @@ -145,7 +145,7 @@ public function testCanNotEncryptInBase64ModeWithInvalidKey(): void $this->assertEquals('', $encrypted); } - public function testCanDecryptWithBase64Mode():void + public function testCanDecryptWithBase64Mode(): void { $allowedChars = $this::ALNUM_ALLOWED_CHARS; $data = '+3vGgYRQTqohHF4Vfl5TSWYx'; @@ -161,7 +161,7 @@ public function testCanDecryptWithBase64Mode():void $this->assertMatchesRegularExpression($allowedChars, $decrypted); } - public function testCanNotDecryptInBase64ModeWithInvalidKey():void + public function testCanNotDecryptInBase64ModeWithInvalidKey(): void { $data = '+3vGgYRQTqohHF4Vfl5TSWYx'; $key = 'th3kEy-'; From 6a2cdce8e29b4c5e89c365a034d6a4e4d61dd430 Mon Sep 17 00:00:00 2001 From: Fahmi Auliya Date: Wed, 25 Jun 2025 09:55:27 +0700 Subject: [PATCH 2/2] [FLOW] Update build file --- .github/workflows/build.yml | 6 ++++++ .php-cs-fixer.dist.php | 12 ++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 .php-cs-fixer.dist.php diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index c446989..08f2cf7 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -40,8 +40,14 @@ jobs: - name: Install dependencies run: composer install --prefer-dist + - name: Run PHP-CS-Fixer + run: vendor/bin/php-cs-fixer fix --dry-run --diff --verbose + - name: Run PHPStan run: vendor/bin/phpstan analyse -l 10 source/ tests/ + - name: Run Psalm + run: vendor/bin/psalm --no-cache --stats + - name: Run unit tests run: vendor/bin/phpunit --no-coverage tests/ \ No newline at end of file diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php new file mode 100644 index 0000000..5360116 --- /dev/null +++ b/.php-cs-fixer.dist.php @@ -0,0 +1,12 @@ +in(__DIR__) +; + +return (new PhpCsFixer\Config()) + ->setRules([ + '@PhpCsFixer' => true, + ]) + ->setFinder($finder) +;