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
6 changes: 6 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/
12 changes: 12 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

$finder = (new PhpCsFixer\Finder())
->in(__DIR__)
;

return (new PhpCsFixer\Config())
->setRules([
'@PhpCsFixer' => true,
])
->setFinder($finder)
;
7 changes: 4 additions & 3 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
;
35 changes: 13 additions & 22 deletions source/AlnumVigenereCipher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
*
* @version 1.1
*
* @psalm-api
*/
class AlnumVigenereCipher extends VigenereCipherBlueprint
Expand Down Expand Up @@ -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');
}
}
Expand All @@ -103,6 +93,7 @@ public function isValid(): bool
}

$this->setIsValid(true);

return true;
}
}
34 changes: 12 additions & 22 deletions source/BasicVigenereCipher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
*
* @version 1.1
*
* @psalm-api
*/
#[\AllowDynamicProperties]
Expand Down Expand Up @@ -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');
}
}
Expand Down
8 changes: 3 additions & 5 deletions source/VigenereCipher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -22,7 +19,7 @@ public static function getClassName(string $mode): string
$className = 'AlnumVigenereCipher';
}

return $path . $className;
return $path.$className;
}

public static function getClass(
Expand All @@ -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);
}

Expand Down
Loading
Loading