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

+20-20
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

+25
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

+3
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

+4-4
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

+37-32
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

+5-3
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

+2-4
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

+2-1
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

+2-1
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

+4-2
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
{

Conversions/SpeedConversion.php

+9-8
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,26 @@
1414
* kn -> 1 knot which is equal to 1 nautical mile (1852 km/h)
1515
* The conversion is made using kilometers as base
1616
*
17-
* @param float $speed
18-
* @param string $unitFrom
19-
* @param string $unitTo
20-
* @return int
17+
* @param float $speed
18+
* @param string $unitFrom
19+
* @param string $unitTo
20+
* @return float
21+
* @throws \Exception
2122
*/
2223
function convertSpeed(float $speed, string $unitFrom, string $unitTo)
2324
{
2425
$speedUnitsFrom = [
2526
'mph' => 1.609344,
2627
'km/h' => 1,
27-
'm/s'=> 3.6,
28-
'ft/s'=> 1.097,
28+
'm/s' => 3.6,
29+
'ft/s' => 1.097,
2930
'kn' => 1.852,
3031
];
3132
$speedUnitsTo = [
3233
'mph' => 0.6213712,
3334
'km/h' => 1,
34-
'm/s'=> 0.277778,
35-
'ft/s'=> 0.911344,
35+
'm/s' => 0.277778,
36+
'ft/s' => 0.911344,
3637
'kn' => 0.539957,
3738
];
3839
$availableUnits = array_keys($speedUnitsFrom);

DataStructures/SinglyLinkedList.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
2+
23
/**
34
* Singly Linked List
45
*/
5-
66
class SinglyLinkedList
77
{
88
public ?SinglyLinkedList $next = null;
@@ -16,7 +16,6 @@ public function __construct($data)
1616
public function append($data): void
1717
{
1818
$current = $this;
19-
2019
while ($current instanceof SinglyLinkedList && isset($current->next)) {
2120
$current = $current->next;
2221
}
@@ -27,15 +26,13 @@ public function append($data): void
2726
public function delete($data): SinglyLinkedList
2827
{
2928
$current = $this;
30-
3129
if ($current->data == $data) {
3230
return $current->next;
3331
}
3432

3533
while ($current instanceof SinglyLinkedList && isset($current->next)) {
3634
if ($current->next->data === $data) {
3735
$current->next = $current->next->next;
38-
3936
return $this;
4037
}
4138

Graphs/BreadthFirstSearch.php

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
<?php
22

33
/**
4-
* Breadth-first search (BFS) is an algorithm for searching a tree data structure for a node that satisfies a given property.
4+
* Breadth-first search (BFS) is an algorithm for searching a tree data structure for a node that satisfies
5+
* a given property.
56
* (https://en.wikipedia.org/wiki/Breadth-first_search).
6-
*
7-
* This is a non recursive implementation.
8-
*
7+
*
8+
* This is a non-recursive implementation.
9+
*
910
* References:
1011
* https://cp-algorithms.com/graph/breadth-first-search.html
11-
*
12+
*
1213
* https://the-algorithms.com/algorithm/depth-first-search?lang=python
13-
*
14+
*
1415
* @author Aryansh Bhargavan https://github.com/aryanshb
1516
* @param array $adjList An array representing the grapth as an Adjacent List
1617
* @param int|string $start The starting vertex
1718
* @return bool if path between start and end vertex exists
1819
*/
1920

20-
function bfs($adjList, $start, $end, $yes = false){
21+
function bfs($adjList, $start, $end, $yes = false)
22+
{
2123
$visited = [];
2224
$queue = [$start];
2325
while (!empty($queue)) {

0 commit comments

Comments
 (0)