Skip to content

Commit 3f513c7

Browse files
authored
Merge pull request #20 from amculin/test
Optimize Code
2 parents a004a53 + 6a2cdce commit 3f513c7

File tree

10 files changed

+94
-151
lines changed

10 files changed

+94
-151
lines changed

.github/workflows/build.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,14 @@ jobs:
4040
- name: Install dependencies
4141
run: composer install --prefer-dist
4242

43+
- name: Run PHP-CS-Fixer
44+
run: vendor/bin/php-cs-fixer fix --dry-run --diff --verbose
45+
4346
- name: Run PHPStan
4447
run: vendor/bin/phpstan analyse -l 10 source/ tests/
4548

49+
- name: Run Psalm
50+
run: vendor/bin/psalm --no-cache --stats
51+
4652
- name: Run unit tests
4753
run: vendor/bin/phpunit --no-coverage tests/

.php-cs-fixer.dist.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
$finder = (new PhpCsFixer\Finder())
4+
->in(__DIR__)
5+
;
6+
7+
return (new PhpCsFixer\Config())
8+
->setRules([
9+
'@PhpCsFixer' => true,
10+
])
11+
->setFinder($finder)
12+
;

rector.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66

77
return RectorConfig::configure()
88
->withPaths([
9-
__DIR__ . '/source',
10-
__DIR__ . '/tests',
9+
__DIR__.'/source',
10+
__DIR__.'/tests',
1111
])
1212
// uncomment to reach your current PHP version
1313
// ->withPhpSets()
1414
->withTypeCoverageLevel(0)
1515
->withDeadCodeLevel(0)
16-
->withCodeQualityLevel(0);
16+
->withCodeQualityLevel(0)
17+
;

source/AlnumVigenereCipher.php

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
use amculin\cryptography\classic\exceptions\InvalidAlnumException;
77

88
/**
9-
* This file is the main class for alpha-numric mode vigenere cipher algortithm
9+
* This file is the main class for alpha-numric mode vigenere cipher algortithm.
1010
*
1111
* @author Fahmi Auliya Tsani <[email protected]>
12+
*
1213
* @version 1.1
14+
*
1315
* @psalm-api
1416
*/
1517
class AlnumVigenereCipher extends VigenereCipherBlueprint
@@ -39,60 +41,48 @@ public function __construct(
3941
}
4042
}
4143

42-
/**
43-
* @inheritdoc
44-
*/
4544
public function isValidKey(string $pattern): bool
4645
{
47-
if ($pattern != '') {
48-
return preg_match($pattern, $this->key) == 1;
46+
if ('' != $pattern) {
47+
return 1 == preg_match($pattern, $this->key);
4948
}
5049

5150
return false;
5251
}
5352

54-
/**
55-
* @inheritdoc
56-
*/
5753
public function isValidPlainText(string $pattern): bool
5854
{
59-
if ($pattern != '') {
60-
return preg_match($pattern, $this->plainText) == 1;
55+
if ('' != $pattern) {
56+
return 1 == preg_match($pattern, $this->plainText);
6157
}
6258

6359
return false;
6460
}
6561

66-
/**
67-
* @inheritdoc
68-
*/
6962
public function isValidCipherText(string $pattern): bool
7063
{
71-
if ($pattern != '') {
72-
return preg_match($pattern, $this->cipherText) == 1;
64+
if ('' != $pattern) {
65+
return 1 == preg_match($pattern, $this->cipherText);
7366
}
7467

7568
return false;
7669
}
7770

78-
/**
79-
* @inheritdoc
80-
*/
8171
public function isValid(): bool
8272
{
8373
try {
8474
$pattern = '/^[a-zA-Z0-9]*$/';
8575

86-
if (! $this->isValidKey($pattern)) {
76+
if (!$this->isValidKey($pattern)) {
8777
throw new InvalidAlnumException('Key');
8878
}
8979

9080
if ($this->process == ProcessType::ENCRYPT->value) {
91-
if (! $this->isValidPlainText($pattern)) {
81+
if (!$this->isValidPlainText($pattern)) {
9282
throw new InvalidAlnumException('Plain text');
9383
}
9484
} else {
95-
if (! $this->isValidCipherText($pattern)) {
85+
if (!$this->isValidCipherText($pattern)) {
9686
throw new InvalidAlnumException('Cipher text');
9787
}
9888
}
@@ -103,6 +93,7 @@ public function isValid(): bool
10393
}
10494

10595
$this->setIsValid(true);
96+
10697
return true;
10798
}
10899
}

source/BasicVigenereCipher.php

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
use amculin\cryptography\classic\exceptions\InvalidBasicException;
77

88
/**
9-
* This file is the main class for basic vigenere cipher algortithm
9+
* This file is the main class for basic vigenere cipher algortithm.
1010
*
1111
* @author Fahmi Auliya Tsani <[email protected]>
12+
*
1213
* @version 1.1
14+
*
1315
* @psalm-api
1416
*/
1517
#[\AllowDynamicProperties]
@@ -40,60 +42,48 @@ public function __construct(
4042
}
4143
}
4244

43-
/**
44-
* @inheritdoc
45-
*/
4645
public function isValidKey(string $pattern): bool
4746
{
48-
if ($pattern != '') {
49-
return preg_match($pattern, $this->key) == 1;
47+
if ('' != $pattern) {
48+
return 1 == preg_match($pattern, $this->key);
5049
}
5150

5251
return false;
5352
}
5453

55-
/**
56-
* @inheritdoc
57-
*/
5854
public function isValidPlainText(string $pattern): bool
5955
{
60-
if ($pattern != '') {
61-
return preg_match($pattern, $this->plainText) == 1;
56+
if ('' != $pattern) {
57+
return 1 == preg_match($pattern, $this->plainText);
6258
}
6359

6460
return false;
6561
}
6662

67-
/**
68-
* @inheritdoc
69-
*/
7063
public function isValidCipherText(string $pattern): bool
7164
{
72-
if ($pattern != '') {
73-
return preg_match($pattern, $this->cipherText) == 1;
65+
if ('' != $pattern) {
66+
return 1 == preg_match($pattern, $this->cipherText);
7467
}
7568

7669
return false;
7770
}
7871

79-
/**
80-
* @inheritdoc
81-
*/
8272
public function isValid(): bool
8373
{
8474
try {
8575
$pattern = '/^[a-z]*$/';
8676

87-
if (! $this->isValidKey($pattern)) {
77+
if (!$this->isValidKey($pattern)) {
8878
throw new InvalidBasicException('Key');
8979
}
9080

9181
if ($this->process == ProcessType::ENCRYPT->value) {
92-
if (! $this->isValidPlainText($pattern)) {
82+
if (!$this->isValidPlainText($pattern)) {
9383
throw new InvalidBasicException('Plain text');
9484
}
9585
} else {
96-
if (! $this->isValidCipherText($pattern)) {
86+
if (!$this->isValidCipherText($pattern)) {
9787
throw new InvalidBasicException('Cipher text');
9888
}
9989
}

source/VigenereCipher.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22

33
namespace amculin\cryptography\classic;
44

5-
use amculin\cryptography\classic\AlnumVigenereCipher;
6-
use amculin\cryptography\classic\BasicVigenereCipher;
7-
use amculin\cryptography\classic\VigenereCipherBlueprint;
85
use amculin\cryptography\classic\enums\ProcessType;
96
use amculin\cryptography\classic\enums\VigenereMode;
107

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

25-
return $path . $className;
22+
return $path.$className;
2623
}
2724

2825
public static function getClass(
@@ -33,7 +30,8 @@ public static function getClass(
3330
): VigenereCipherBlueprint {
3431
if ($mode == VigenereMode::ALPHA_NUMERIC->value) {
3532
return new AlnumVigenereCipher($data, $key, $processName);
36-
} elseif ($mode == VigenereMode::BASE64->value) {
33+
}
34+
if ($mode == VigenereMode::BASE64->value) {
3735
return new Base64VigenereCipher($data, $key, $processName);
3836
}
3937

0 commit comments

Comments
 (0)