Skip to content

Commit e32ba36

Browse files
committed
[DEV] Comply with PHPStan Level 10
1 parent 3222f12 commit e32ba36

File tree

7 files changed

+195
-71
lines changed

7 files changed

+195
-71
lines changed

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,8 @@
1717
"psr-4": {
1818
"amculin\\cryptography\\classic\\": "source/"
1919
}
20+
},
21+
"require-dev": {
22+
"phpstan/phpstan": "^2.1"
2023
}
21-
}
24+
}

source/AlnumVigenereCipher.php

Lines changed: 54 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,39 +8,77 @@
88
* This file is the main class for alpha-numric mode vigenere cipher algortithm
99
*
1010
* @author Fahmi Auliya Tsani <[email protected]>
11-
* @version 0.1
11+
* @version 1.1
1212
*/
13-
1413
class AlnumVigenereCipher extends VigenereCipherBlueprint
1514
{
15+
public function __construct(
16+
public string $data,
17+
public string $key,
18+
public string $process = 'encrypt'
19+
) {
20+
$this->process = $process;
21+
$this->tabulaRecta = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
22+
23+
if ($process == ProcessType::ENCRYPT->value) {
24+
$this->plainText = $data;
25+
$this->key = $this->generateKey($key);
26+
27+
if ($this->isValid()) {
28+
$this->encrypt();
29+
}
30+
} else {
31+
$this->cipherText = $data;
32+
$this->key = $this->generateKey($key);
33+
34+
if ($this->isValid()) {
35+
$this->decrypt();
36+
}
37+
}
38+
}
39+
1640
/**
17-
* Default list of acceptable character to be used in vigenere cipher algorithm
18-
* This list cointains alpha-numeric characters including the capitalized
19-
*
20-
* @var string
41+
* @inheritdoc
2142
*/
22-
public $tabulaRecta = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
43+
public function isValidKey(string $pattern): bool
44+
{
45+
return preg_match($pattern, $this->key) == 1;
46+
}
2347

48+
/**
49+
* @inheritdoc
50+
*/
51+
public function isValidPlainText(string $pattern): bool
52+
{
53+
return preg_match($pattern, $this->plainText) == 1;
54+
}
55+
56+
/**
57+
* @inheritdoc
58+
*/
59+
public function isValidCipherText(string $pattern): bool
60+
{
61+
return preg_match($pattern, $this->cipherText) == 1;
62+
}
63+
64+
/**
65+
* @inheritdoc
66+
*/
2467
public function isValid(): bool
2568
{
2669
try {
2770
$pattern = '/^[a-zA-Z0-9]*$/';
28-
$isValid = preg_match($pattern, $this->key);
2971

30-
if (! $isValid) {
72+
if (! $this->isValidKey($pattern)) {
3173
throw new InvalidAlnumException('Key');
3274
}
3375

3476
if ($this->process == ProcessType::ENCRYPT->value) {
35-
$isValid = preg_match($pattern, $this->plainText) && $isValid;
36-
37-
if (! $isValid) {
77+
if (! $this->isValidPlainText($pattern)) {
3878
throw new InvalidAlnumException('Plain text');
3979
}
4080
} else {
41-
$isValid = preg_match($pattern, $this->cipherText) && $isValid;
42-
43-
if (! $isValid) {
81+
if (! $this->isValidCipherText($pattern)) {
4482
throw new InvalidAlnumException('Cipher text');
4583
}
4684
}
@@ -50,6 +88,7 @@ public function isValid(): bool
5088
return false;
5189
}
5290

91+
$this->setIsValid(true);
5392
return true;
5493
}
5594
}

source/BasicVigenereCipher.php

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,77 @@
88
* This file is the main class for basic vigenere cipher algortithm
99
*
1010
* @author Fahmi Auliya Tsani <[email protected]>
11-
* @version 0.2
11+
* @version 1.1
1212
*/
13-
1413
class BasicVigenereCipher extends VigenereCipherBlueprint
1514
{
15+
public function __construct(
16+
public string $data,
17+
public string $key,
18+
public string $process = 'encrypt'
19+
) {
20+
$this->process = $process;
21+
$this->tabulaRecta = 'abcdefghijklmnopqrstuvwxyz';
22+
23+
if ($process == ProcessType::ENCRYPT->value) {
24+
$this->plainText = $data;
25+
$this->key = $this->generateKey($key);
26+
27+
if ($this->isValid()) {
28+
$this->encrypt();
29+
}
30+
} else {
31+
$this->cipherText = $data;
32+
$this->key = $this->generateKey($key);
33+
34+
if ($this->isValid()) {
35+
$this->decrypt();
36+
}
37+
}
38+
}
39+
1640
/**
1741
* @inheritdoc
1842
*/
19-
public $tabulaRecta = 'abcdefghijklmnopqrstuvwxyz';
43+
public function isValidKey(string $pattern): bool
44+
{
45+
return preg_match($pattern, $this->key) == 1;
46+
}
2047

48+
/**
49+
* @inheritdoc
50+
*/
51+
public function isValidPlainText(string $pattern): bool
52+
{
53+
return preg_match($pattern, $this->plainText) == 1;
54+
}
55+
56+
/**
57+
* @inheritdoc
58+
*/
59+
public function isValidCipherText(string $pattern): bool
60+
{
61+
return preg_match($pattern, $this->cipherText) == 1;
62+
}
63+
64+
/**
65+
* @inheritdoc
66+
*/
2167
public function isValid(): bool
2268
{
2369
try {
2470
$pattern = '/^[a-z]*$/';
25-
$isValid = preg_match($pattern, $this->key);
2671

27-
if (! $isValid) {
72+
if (! $this->isValidKey($pattern)) {
2873
throw new InvalidBasicException('Key');
2974
}
3075

3176
if ($this->process == ProcessType::ENCRYPT->value) {
32-
$isValid = preg_match($pattern, $this->plainText) && $isValid;
33-
34-
if (! $isValid) {
77+
if (! $this->isValidPlainText($pattern)) {
3578
throw new InvalidBasicException('Plain text');
3679
}
3780
} else {
38-
$isValid = preg_match($pattern, $this->cipherText) && $isValid;
39-
40-
if (! $isValid) {
81+
if (! $this->isValidCipherText($pattern)) {
4182
throw new InvalidBasicException('Cipher text');
4283
}
4384
}
@@ -47,6 +88,8 @@ public function isValid(): bool
4788
return false;
4889
}
4990

91+
$this->setIsValid(true);
92+
5093
return true;
5194
}
5295
}

source/VigenereCipher.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace amculin\cryptography\classic;
33

4+
use amculin\cryptography\classic\VigenereCipherBlueprint;
45
use amculin\cryptography\classic\enums\ProcessType;
56
use amculin\cryptography\classic\enums\VigenereMode;
67

@@ -9,12 +10,13 @@ class VigenereCipher
910
public static function getClassName(string $mode): string
1011
{
1112
$path = 'amculin\cryptography\classic\\';
13+
$className = 'BasicVigenereCipher';
1214

13-
if ($mode == VigenereMode::BASIC->value) {
14-
return $path . 'BasicVigenereCipher';
15-
} elseif ($mode == VigenereMode::ALPHA_NUMERIC->value) {
16-
return $path . 'AlnumVigenereCipher';
15+
if ($mode == VigenereMode::ALPHA_NUMERIC->value) {
16+
$className = 'AlnumVigenereCipher';
1717
}
18+
19+
return $path . $className;
1820
}
1921

2022
public static function encrypt(string $data, string $key, string $mode = 'basic'): string|null
@@ -23,9 +25,10 @@ public static function encrypt(string $data, string $key, string $mode = 'basic'
2325

2426
$processName = ProcessType::ENCRYPT->value;
2527

26-
$encrypt = new $className($processName, $data, $key);
28+
/** @var VigenereCipherBlueprint $encrypt */
29+
$encrypt = new $className($data, $key, $processName);
2730

28-
return $encrypt->getCipherText();
31+
return $encrypt->cipherText;
2932
}
3033

3134
public static function decrypt(string $data, string $key, string $mode = 'basic'): string|null
@@ -34,8 +37,9 @@ public static function decrypt(string $data, string $key, string $mode = 'basic'
3437

3538
$processName = ProcessType::DECRYPT->value;
3639

37-
$decrypt = new $className($processName, $data, $key);
40+
/** @var VigenereCipherBlueprint $decrypt */
41+
$decrypt = new $className($data, $key, $processName);
3842

39-
return $decrypt->getPlainText();
43+
return $decrypt->plainText;
4044
}
4145
}

0 commit comments

Comments
 (0)