Skip to content

Commit 618fe7f

Browse files
authored
Merge pull request #122 from salehhashemi1992/ci/setup-code-style-check
Implement PSR-12 Coding Standards and Add CS Check to CI Flow
2 parents a554210 + 658618a commit 618fe7f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+918
-788
lines changed

.github/workflows/ci.yml

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,23 @@ jobs:
1212
runs-on: ubuntu-latest
1313

1414
steps:
15-
- uses: actions/checkout@v2
16-
17-
- name: Validate composer.json and composer.lock
18-
run: composer validate
19-
20-
- name: Cache Composer packages
21-
id: composer-cache
22-
uses: actions/cache@v2
23-
with:
24-
path: vendor
25-
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
26-
restore-keys: |
27-
${{ runner.os }}-php-
28-
29-
- name: Install dependencies
30-
if: steps.composer-cache.outputs.cache-hit != 'true'
31-
run: composer install --prefer-dist --no-progress --no-suggest
32-
33-
- name: Run PHPUnit
34-
run: composer run-script test
15+
- uses: actions/checkout@v2
16+
17+
- name: Validate composer.json and composer.lock
18+
run: composer validate
19+
20+
- name: Cache Composer packages
21+
id: composer-cache
22+
uses: actions/cache@v2
23+
with:
24+
path: vendor
25+
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
26+
restore-keys: |
27+
${{ runner.os }}-php-
28+
29+
- name: Install dependencies
30+
if: steps.composer-cache.outputs.cache-hit != 'true'
31+
run: composer install --prefer-dist --no-progress --no-suggest
32+
33+
- name: Run PHPUnit
34+
run: composer run-script test

.github/workflows/code-style.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Code style
2+
3+
on: [push, pull_request]
4+
5+
permissions:
6+
contents: read
7+
8+
jobs:
9+
phpcs:
10+
name: PHPCS
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Checkout
14+
uses: actions/checkout@v3
15+
16+
- name: Setup PHP
17+
uses: shivammathur/setup-php@v2
18+
with:
19+
php-version: '7.4'
20+
21+
- name: Install dependencies
22+
run: composer update --prefer-dist --no-progress --no-suggest
23+
24+
- name: Run script
25+
run: vendor/bin/phpcs -n

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@
44
.phan
55
composer.lock
66

7+
/.phpcs-cache
8+
/phpcs.xml
9+
710
.phpunit.result.cache

Ciphers/CaesarCipher.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
/**
44
* Encrypt given text using caesar cipher.
55
*
6-
* @param string text text to be encrypted
7-
* @param int shift number of shifts to be applied
6+
* @param string $text text text to be encrypted
7+
* @param int $shift shift number of shifts to be applied
88
* @return string new encrypted text
99
*/
1010
function encrypt(string $text, int $shift): string
@@ -27,8 +27,8 @@ function encrypt(string $text, int $shift): string
2727

2828
/**
2929
* Decrypt given text using caesar cipher.
30-
* @param string text text to be decrypted
31-
* @param int shift number of shifts to be applied
30+
* @param string $text text text to be decrypted
31+
* @param int $shift shift number of shifts to be applied
3232
* @return string new decrypted text
3333
*/
3434
function decrypt(string $text, int $shift): string

Ciphers/MonoAlphabeticCipher.php

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,37 @@
1-
<?php
2-
// A mono-alphabetic cipher is a simple substitution cipher
3-
// https://www.101computing.net/mono-alphabetic-substitution-cipher/
4-
5-
function monoAlphabeticCipher($key, $alphabet, $text){
6-
7-
$cipherText = ''; // the cipher text (can be decrypted and encrypted)
8-
9-
if ( strlen($key) != strlen($alphabet) ) { return false; } // check if the text length matches
10-
$text = preg_replace('/[0-9]+/', '', $text); // remove all the numbers
11-
12-
for( $i = 0; $i < strlen($text); $i++ ){
13-
$index = strripos( $alphabet, $text[$i] );
14-
if( $text[$i] == " " ){ $cipherText .= " "; }
15-
else{ $cipherText .= ( ctype_upper($text[$i]) ? strtoupper($key[$index]) : $key[$index] ); }
16-
}
17-
return $cipherText;
18-
}
19-
20-
function maEncrypt($key, $alphabet, $text){
21-
22-
return monoAlphabeticCipher($key, $alphabet, $text);
23-
24-
}
25-
26-
function maDecrypt($key, $alphabet, $text){
27-
28-
return monoAlphabeticCipher($alphabet, $key, $text);
29-
30-
}
31-
32-
?>
1+
<?php
2+
3+
// A mono-alphabetic cipher is a simple substitution cipher
4+
// https://www.101computing.net/mono-alphabetic-substitution-cipher/
5+
6+
function monoAlphabeticCipher($key, $alphabet, $text)
7+
{
8+
$cipherText = ''; // the cipher text (can be decrypted and encrypted)
9+
10+
// check if the text length matches
11+
if (strlen($key) != strlen($alphabet)) {
12+
return false;
13+
}
14+
15+
$text = preg_replace('/[0-9]+/', '', $text); // remove all the numbers
16+
17+
for ($i = 0; $i < strlen($text); $i++) {
18+
$index = strripos($alphabet, $text[$i]);
19+
if ($text[$i] == " ") {
20+
$cipherText .= " ";
21+
} else {
22+
$cipherText .= ( ctype_upper($text[$i]) ? strtoupper($key[$index]) : $key[$index] );
23+
}
24+
}
25+
26+
return $cipherText;
27+
}
28+
29+
function maEncrypt($key, $alphabet, $text)
30+
{
31+
return monoAlphabeticCipher($key, $alphabet, $text);
32+
}
33+
34+
function maDecrypt($key, $alphabet, $text)
35+
{
36+
return monoAlphabeticCipher($alphabet, $key, $text);
37+
}

Ciphers/MorseCode.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
/**
44
* Encode text to Morse Code.
55
*
6-
* @param string text to encode
6+
* @param string $text text to encode
77
* @return string encoded text
8+
* @throws \Exception
89
*/
910
function encode(string $text): string
1011
{
@@ -57,13 +58,14 @@ function encode(string $text): string
5758
throw new \Exception("Invalid character: $c");
5859
}
5960
}
60-
substr_replace($encodedText ,"", -1); // Removes trailing space
61+
substr_replace($encodedText, "", -1); // Removes trailing space
6162
return $encodedText;
6263
}
6364

6465
/**
6566
* Decode Morse Code to text.
66-
* @param string text to decode
67+
* @param string $text text to decode
68+
* @throws \Exception
6769
*/
6870
function decode(string $text): string
6971
{

Ciphers/XORCipher.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,16 @@
77
* character with the key.
88
* The key is repeated until it is the same length as the input.
99
*
10-
* @param string $input The input string.
10+
* @param string $input_string The input string.
1111
* @param string $key The key to use.
1212
* @return string The encrypted string.
1313
*/
1414
function xorCipher(string $input_string, string $key)
1515
{
16-
1716
$key_len = strlen($key);
1817
$result = array();
1918

20-
for ($idx = 0; $idx < strlen($input_string); $idx++)
21-
{
19+
for ($idx = 0; $idx < strlen($input_string); $idx++) {
2220
array_push($result, $input_string[$idx] ^ $key[$idx % $key_len]);
2321
}
2422

Conversions/BinaryToDecimal.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
* (2 + 0) base 10
1212
* 2 base 10
1313
*
14-
* @param string $binaryNumber
14+
* @param string $binaryNumber
1515
* @return int
16+
* @throws \Exception
1617
*/
1718
function binaryToDecimal($binaryNumber)
1819
{

Conversions/DecimalToBinary.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
* submitted Decimal Number to
66
* Binary Number.
77
*
8-
* @param string $decimalNumber
8+
* @param string $decimalNumber
99
* @return string
10+
* @throws \Exception
1011
*/
1112
function decimalToBinary($decimalNumber)
1213
{

Conversions/OctalToDecimal.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
* (1 * (8 ^ 1) + 0 * (8 ^ 0)) base 10
1111
* (8 + 0) base 10
1212
* 9 base 10
13-
* @param string $octalNumber
13+
* @param string $octalNumber
1414
* @return int
15+
* @throws \Exception
1516
*/
1617
function octalToDecimal($octalNumber)
1718
{
@@ -34,8 +35,9 @@ function octalToDecimal($octalNumber)
3435
* submitted Decimal Number to
3536
* Octal Number.
3637
*
37-
* @param string $decimalNumber
38+
* @param string $decimalNumber
3839
* @return string
40+
* @throws \Exception
3941
*/
4042
function decimalToOctal($decimalNumber)
4143
{

0 commit comments

Comments
 (0)