Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement PSR-12 Coding Standards and Add CS Check to CI Flow #122

Merged
40 changes: 20 additions & 20 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Validate composer.json and composer.lock
run: composer validate

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v2
with:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-

- name: Install dependencies
if: steps.composer-cache.outputs.cache-hit != 'true'
run: composer install --prefer-dist --no-progress --no-suggest

- name: Run PHPUnit
run: composer run-script test
- uses: actions/checkout@v2

- name: Validate composer.json and composer.lock
run: composer validate

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v2
with:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-

- name: Install dependencies
if: steps.composer-cache.outputs.cache-hit != 'true'
run: composer install --prefer-dist --no-progress --no-suggest

- name: Run PHPUnit
run: composer run-script test
25 changes: 25 additions & 0 deletions .github/workflows/code-style.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Code style

on: [push, pull_request]

permissions:
contents: read

jobs:
phpcs:
name: PHPCS
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '7.4'

- name: Install dependencies
run: composer update --prefer-dist --no-progress --no-suggest

- name: Run script
run: vendor/bin/phpcs -n
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@
.phan
composer.lock

/.phpcs-cache
/phpcs.xml

.phpunit.result.cache
8 changes: 4 additions & 4 deletions Ciphers/CaesarCipher.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
/**
* Encrypt given text using caesar cipher.
*
* @param string text text to be encrypted
* @param int shift number of shifts to be applied
* @param string $text text text to be encrypted
* @param int $shift shift number of shifts to be applied
* @return string new encrypted text
*/
function encrypt(string $text, int $shift): string
Expand All @@ -27,8 +27,8 @@ function encrypt(string $text, int $shift): string

/**
* Decrypt given text using caesar cipher.
* @param string text text to be decrypted
* @param int shift number of shifts to be applied
* @param string $text text text to be decrypted
* @param int $shift shift number of shifts to be applied
* @return string new decrypted text
*/
function decrypt(string $text, int $shift): string
Expand Down
69 changes: 37 additions & 32 deletions Ciphers/MonoAlphabeticCipher.php
Original file line number Diff line number Diff line change
@@ -1,32 +1,37 @@
<?php
// A mono-alphabetic cipher is a simple substitution cipher
// https://www.101computing.net/mono-alphabetic-substitution-cipher/

function monoAlphabeticCipher($key, $alphabet, $text){

$cipherText = ''; // the cipher text (can be decrypted and encrypted)

if ( strlen($key) != strlen($alphabet) ) { return false; } // check if the text length matches
$text = preg_replace('/[0-9]+/', '', $text); // remove all the numbers

for( $i = 0; $i < strlen($text); $i++ ){
$index = strripos( $alphabet, $text[$i] );
if( $text[$i] == " " ){ $cipherText .= " "; }
else{ $cipherText .= ( ctype_upper($text[$i]) ? strtoupper($key[$index]) : $key[$index] ); }
}
return $cipherText;
}

function maEncrypt($key, $alphabet, $text){

return monoAlphabeticCipher($key, $alphabet, $text);

}

function maDecrypt($key, $alphabet, $text){

return monoAlphabeticCipher($alphabet, $key, $text);

}

?>
<?php

// A mono-alphabetic cipher is a simple substitution cipher
// https://www.101computing.net/mono-alphabetic-substitution-cipher/

function monoAlphabeticCipher($key, $alphabet, $text)
{
$cipherText = ''; // the cipher text (can be decrypted and encrypted)

// check if the text length matches
if (strlen($key) != strlen($alphabet)) {
return false;
}

$text = preg_replace('/[0-9]+/', '', $text); // remove all the numbers

for ($i = 0; $i < strlen($text); $i++) {
$index = strripos($alphabet, $text[$i]);
if ($text[$i] == " ") {
$cipherText .= " ";
} else {
$cipherText .= ( ctype_upper($text[$i]) ? strtoupper($key[$index]) : $key[$index] );
}
}

return $cipherText;
}

function maEncrypt($key, $alphabet, $text)
{
return monoAlphabeticCipher($key, $alphabet, $text);
}

function maDecrypt($key, $alphabet, $text)
{
return monoAlphabeticCipher($alphabet, $key, $text);
}
8 changes: 5 additions & 3 deletions Ciphers/MorseCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
/**
* Encode text to Morse Code.
*
* @param string text to encode
* @param string $text text to encode
* @return string encoded text
* @throws \Exception
*/
function encode(string $text): string
{
Expand Down Expand Up @@ -57,13 +58,14 @@ function encode(string $text): string
throw new \Exception("Invalid character: $c");
}
}
substr_replace($encodedText ,"", -1); // Removes trailing space
substr_replace($encodedText, "", -1); // Removes trailing space
return $encodedText;
}

/**
* Decode Morse Code to text.
* @param string text to decode
* @param string $text text to decode
* @throws \Exception
*/
function decode(string $text): string
{
Expand Down
6 changes: 2 additions & 4 deletions Ciphers/XORCipher.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,16 @@
* character with the key.
* The key is repeated until it is the same length as the input.
*
* @param string $input The input string.
* @param string $input_string The input string.
* @param string $key The key to use.
* @return string The encrypted string.
*/
function xorCipher(string $input_string, string $key)
{

$key_len = strlen($key);
$result = array();

for ($idx = 0; $idx < strlen($input_string); $idx++)
{
for ($idx = 0; $idx < strlen($input_string); $idx++) {
array_push($result, $input_string[$idx] ^ $key[$idx % $key_len]);
}

Expand Down
3 changes: 2 additions & 1 deletion Conversions/BinaryToDecimal.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
* (2 + 0) base 10
* 2 base 10
*
* @param string $binaryNumber
* @param string $binaryNumber
* @return int
* @throws \Exception
*/
function binaryToDecimal($binaryNumber)
{
Expand Down
3 changes: 2 additions & 1 deletion Conversions/DecimalToBinary.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
* submitted Decimal Number to
* Binary Number.
*
* @param string $decimalNumber
* @param string $decimalNumber
* @return string
* @throws \Exception
*/
function decimalToBinary($decimalNumber)
{
Expand Down
6 changes: 4 additions & 2 deletions Conversions/OctalToDecimal.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
* (1 * (8 ^ 1) + 0 * (8 ^ 0)) base 10
* (8 + 0) base 10
* 9 base 10
* @param string $octalNumber
* @param string $octalNumber
* @return int
* @throws \Exception
*/
function octalToDecimal($octalNumber)
{
Expand All @@ -34,8 +35,9 @@ function octalToDecimal($octalNumber)
* submitted Decimal Number to
* Octal Number.
*
* @param string $decimalNumber
* @param string $decimalNumber
* @return string
* @throws \Exception
*/
function decimalToOctal($decimalNumber)
{
Expand Down
17 changes: 9 additions & 8 deletions Conversions/SpeedConversion.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,26 @@
* kn -> 1 knot which is equal to 1 nautical mile (1852 km/h)
* The conversion is made using kilometers as base
*
* @param float $speed
* @param string $unitFrom
* @param string $unitTo
* @return int
* @param float $speed
* @param string $unitFrom
* @param string $unitTo
* @return float
* @throws \Exception
*/
function convertSpeed(float $speed, string $unitFrom, string $unitTo)
{
$speedUnitsFrom = [
'mph' => 1.609344,
'km/h' => 1,
'm/s'=> 3.6,
'ft/s'=> 1.097,
'm/s' => 3.6,
'ft/s' => 1.097,
'kn' => 1.852,
];
$speedUnitsTo = [
'mph' => 0.6213712,
'km/h' => 1,
'm/s'=> 0.277778,
'ft/s'=> 0.911344,
'm/s' => 0.277778,
'ft/s' => 0.911344,
'kn' => 0.539957,
];
$availableUnits = array_keys($speedUnitsFrom);
Expand Down
5 changes: 1 addition & 4 deletions DataStructures/SinglyLinkedList.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

/**
* Singly Linked List
*/

class SinglyLinkedList
{
public ?SinglyLinkedList $next = null;
Expand All @@ -16,7 +16,6 @@ public function __construct($data)
public function append($data): void
{
$current = $this;

while ($current instanceof SinglyLinkedList && isset($current->next)) {
$current = $current->next;
}
Expand All @@ -27,15 +26,13 @@ public function append($data): void
public function delete($data): SinglyLinkedList
{
$current = $this;

if ($current->data == $data) {
return $current->next;
}

while ($current instanceof SinglyLinkedList && isset($current->next)) {
if ($current->next->data === $data) {
$current->next = $current->next->next;

return $this;
}

Expand Down
16 changes: 9 additions & 7 deletions Graphs/BreadthFirstSearch.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
<?php

/**
* Breadth-first search (BFS) is an algorithm for searching a tree data structure for a node that satisfies a given property.
* Breadth-first search (BFS) is an algorithm for searching a tree data structure for a node that satisfies
* a given property.
* (https://en.wikipedia.org/wiki/Breadth-first_search).
*
* This is a non recursive implementation.
*
*
* This is a non-recursive implementation.
*
* References:
* https://cp-algorithms.com/graph/breadth-first-search.html
*
*
* https://the-algorithms.com/algorithm/depth-first-search?lang=python
*
*
* @author Aryansh Bhargavan https://github.com/aryanshb
* @param array $adjList An array representing the grapth as an Adjacent List
* @param int|string $start The starting vertex
* @return bool if path between start and end vertex exists
*/

function bfs($adjList, $start, $end, $yes = false){
function bfs($adjList, $start, $end, $yes = false)
{
$visited = [];
$queue = [$start];
while (!empty($queue)) {
Expand Down
Loading
Loading