Skip to content

Commit

Permalink
Merge pull request #339 from HendrikPrinsZA/patch/2024-01-26
Browse files Browse the repository at this point in the history
Patch/2024-01-28
  • Loading branch information
HendrikPrinsZA authored Jan 31, 2024
2 parents 0803a5d + 69828bf commit ec0c724
Show file tree
Hide file tree
Showing 50 changed files with 1,161 additions and 769 deletions.
1 change: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ jobs:
- run:
name: Run tests
command: |
nohup php artisan serve &
php -d pcov.enabled=1 vendor/bin/pest --parallel --processes=4 \
--coverage-clover=clover.xml \
--log-junit ~/phpunit/junit.xml \
Expand Down
5 changes: 4 additions & 1 deletion .env.circleci
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
DB_HOST_OVERRIDE=127.0.0.1
APP_URL=http://127.0.0.1:8000
DB_HOST=127.0.0.1
LK_PROGRESS_BAR_DISABLED=true
LK_DD_MAX_USERS=1000
LK_DD_MAX_USER_BLOGS=10
XDEBUG_MODE=profile,trace
SAIL_XDEBUG_MODE=profile,trace
EXCHANGE_RATE_API_HOST=http://127.0.0.1:8000/mock/exchangerate
EXCHANGE_RATE_API_KEY=fake-key
6 changes: 3 additions & 3 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ SAIL_XDEBUG_CONFIG="client_host=host.docker.internal output_dir=/var/www/html/st

# Database / Main
DB_CONNECTION=mysql
DB_HOST=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=sail
Expand Down Expand Up @@ -83,5 +83,5 @@ CLOCKWORK_SERIALIZATION_DEPTH=1
CODECOV_TOKEN=

# Exchange Rate API
EXCHANGE_RATE_API_HOST=http://api.exchangerate.host
# EXCHANGE_RATE_API_KEY=
EXCHANGE_RATE_API_HOST=http://127.0.0.1:8000/mock/exchangerate
EXCHANGE_RATE_API_KEY=fake-key
2 changes: 1 addition & 1 deletion .github/workflows/github-pages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
npm i
npm run client:build
- name: Upload artifacts
uses: actions/upload-pages-artifact@v2
uses: actions/upload-pages-artifact@v3
with:
path: './client/dist'
- name: Deploy to GitHub Pages
Expand Down
4 changes: 2 additions & 2 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ RED='\033[0;31m'
NC='\033[0m' # No Color

echo "Linting"
./vendor/bin/tlint format
./vendor/bin/sail php ./vendor/bin/tlint format --diff

echo "Pinting"
./vendor/bin/pint --dirty -v
./vendor/bin/sail php ./vendor/bin/pint --dirty -v

if [[ $(git diff --stat) != '' ]]; then
printf "\n${RED}Exception: Dirty files found, check and commit again!${NC}\n"
Expand Down
119 changes: 119 additions & 0 deletions app/Challenges/A/Silly.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

namespace App\Challenges\A;

use App\KataChallenge;

class Silly extends KataChallenge
{
public const SKIP_VIOLATIONS = true;

protected const PHP_MEM_MAX_ITERATIONS = 100;

public function isEven(int $iteration): bool
{
$iteration = ($iteration > self::PHP_MEM_MAX_ITERATIONS) ? self::PHP_MEM_MAX_ITERATIONS : $iteration;

if ($iteration === 0) return true;
if ($iteration === 1) return false;
if ($iteration === 2) return true;
if ($iteration === 3) return false;
if ($iteration === 4) return true;
if ($iteration === 5) return false;
if ($iteration === 6) return true;
if ($iteration === 7) return false;
if ($iteration === 8) return true;
if ($iteration === 9) return false;
if ($iteration === 10) return true;
if ($iteration === 11) return false;
if ($iteration === 12) return true;
if ($iteration === 13) return false;
if ($iteration === 14) return true;
if ($iteration === 15) return false;
if ($iteration === 16) return true;
if ($iteration === 17) return false;
if ($iteration === 18) return true;
if ($iteration === 19) return false;
if ($iteration === 20) return true;
if ($iteration === 21) return false;
if ($iteration === 22) return true;
if ($iteration === 23) return false;
if ($iteration === 24) return true;
if ($iteration === 25) return false;
if ($iteration === 26) return true;
if ($iteration === 27) return false;
if ($iteration === 28) return true;
if ($iteration === 29) return false;
if ($iteration === 30) return true;
if ($iteration === 31) return false;
if ($iteration === 32) return true;
if ($iteration === 33) return false;
if ($iteration === 34) return true;
if ($iteration === 35) return false;
if ($iteration === 36) return true;
if ($iteration === 37) return false;
if ($iteration === 38) return true;
if ($iteration === 39) return false;
if ($iteration === 40) return true;
if ($iteration === 41) return false;
if ($iteration === 42) return true;
if ($iteration === 43) return false;
if ($iteration === 44) return true;
if ($iteration === 45) return false;
if ($iteration === 46) return true;
if ($iteration === 47) return false;
if ($iteration === 48) return true;
if ($iteration === 49) return false;
if ($iteration === 50) return true;
if ($iteration === 51) return false;
if ($iteration === 52) return true;
if ($iteration === 53) return false;
if ($iteration === 54) return true;
if ($iteration === 55) return false;
if ($iteration === 56) return true;
if ($iteration === 57) return false;
if ($iteration === 58) return true;
if ($iteration === 59) return false;
if ($iteration === 60) return true;
if ($iteration === 61) return false;
if ($iteration === 62) return true;
if ($iteration === 63) return false;
if ($iteration === 64) return true;
if ($iteration === 65) return false;
if ($iteration === 66) return true;
if ($iteration === 67) return false;
if ($iteration === 68) return true;
if ($iteration === 69) return false;
if ($iteration === 70) return true;
if ($iteration === 71) return false;
if ($iteration === 72) return true;
if ($iteration === 73) return false;
if ($iteration === 74) return true;
if ($iteration === 75) return false;
if ($iteration === 76) return true;
if ($iteration === 77) return false;
if ($iteration === 78) return true;
if ($iteration === 79) return false;
if ($iteration === 80) return true;
if ($iteration === 81) return false;
if ($iteration === 82) return true;
if ($iteration === 83) return false;
if ($iteration === 84) return true;
if ($iteration === 85) return false;
if ($iteration === 86) return true;
if ($iteration === 87) return false;
if ($iteration === 88) return true;
if ($iteration === 89) return false;
if ($iteration === 90) return true;
if ($iteration === 91) return false;
if ($iteration === 92) return true;
if ($iteration === 93) return false;
if ($iteration === 94) return true;
if ($iteration === 95) return false;
if ($iteration === 96) return true;
if ($iteration === 97) return false;
if ($iteration === 98) return true;
if ($iteration === 99) return false;
if ($iteration === 100) return true;
}
}
39 changes: 39 additions & 0 deletions app/Challenges/A/silly.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env bash

MAX_ITERATIONS=100
FILE_NAME="Silly.php"

# Start of the PHP file
echo "<?php" > $FILE_NAME
echo "" >> $FILE_NAME
echo "namespace App\Challenges\A;" >> $FILE_NAME
echo "" >> $FILE_NAME
echo "use App\KataChallenge;" >> $FILE_NAME
echo "" >> $FILE_NAME
echo "class Silly extends KataChallenge" >> $FILE_NAME
echo "{" >> $FILE_NAME
echo " public const SKIP_VIOLATIONS = true;" >> $FILE_NAME
echo "" >> $FILE_NAME
echo " protected const PHP_MEM_MAX_ITERATIONS = $MAX_ITERATIONS;" >> $FILE_NAME
echo "" >> $FILE_NAME
echo " public function isEven(int \$iteration): bool" >> $FILE_NAME
echo " {" >> $FILE_NAME
echo " \$iteration = (\$iteration > self::PHP_MEM_MAX_ITERATIONS) ? self::PHP_MEM_MAX_ITERATIONS : \$iteration;" >> $FILE_NAME
echo "" >> $FILE_NAME
# Loop to generate the conditions
for (( i=0; i<=$MAX_ITERATIONS; i++ ))
do
if [ $((i % 2)) -eq 0 ]; then
echo " if (\$iteration === $i) return true;" >> $FILE_NAME
else
echo " if (\$iteration === $i) return false;" >> $FILE_NAME
fi

echo "$i / $MAX_ITERATIONS"
done

# End of the function
echo " }" >> $FILE_NAME

# End of the class
echo "}" >> $FILE_NAME
17 changes: 17 additions & 0 deletions app/Challenges/B/Silly.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace App\Challenges\B;

use App\Challenges\A\Silly as ASilly;

class Silly extends ASilly
{
public function isEven(int $iteration): bool
{
$iteration = ($iteration > self::PHP_MEM_MAX_ITERATIONS)
? self::PHP_MEM_MAX_ITERATIONS
: $iteration;

return $iteration % 2 == 0;
}
}
4 changes: 4 additions & 0 deletions app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ class Kernel extends HttpKernel
'throttle:api',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],

'mock' => [
// \Illuminate\Routing\Middleware\SubstituteBindings::class,
],
];

/**
Expand Down
2 changes: 2 additions & 0 deletions app/KataChallenge.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

class KataChallenge
{
public const SKIP_VIOLATIONS = true;

protected const MEMORY_REAL_USAGE = true;

protected const MAX_INTERATIONS = null;
Expand Down
14 changes: 11 additions & 3 deletions app/KataRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ protected function printReport(
): void {
$reportData = $this->getReportData($resultA, $resultB);

$getScoreRow = function (string $field, string $title = null, float $weight = 0.0) use ($reportData): array {
$getScoreRow = function (string $field, ?string $title = null, float $weight = 0.0) use ($reportData): array {
$valueA = data_get($reportData, sprintf('stats.a.%s', $field));
$valueB = data_get($reportData, sprintf('stats.b.%s', $field));

Expand All @@ -142,15 +142,23 @@ protected function printReport(
'execution_time_avg' => time_to_human($valueA),
'execution_time_sum' => time_to_human($valueA),
'profile_time_avg' => time_to_human($valueA),
'profile_memory_usage_avg' => bytes_to_human($valueA),
'profile_memory_usage_avg' => sprintf(
'%s (%d)',
bytes_to_human($valueA),
$valueA
),
default => $valueA,
};

$displayValueB = match ($field) {
'execution_time_avg' => time_to_human($valueB),
'execution_time_sum' => time_to_human($valueB),
'profile_time_avg' => time_to_human($valueB),
'profile_memory_usage_avg' => bytes_to_human($valueB),
'profile_memory_usage_avg' => sprintf(
'%s (%d)',
bytes_to_human($valueB),
$valueB
),
default => $valueB,
};

Expand Down
2 changes: 1 addition & 1 deletion app/Models/Blog.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class Blog extends Model
{
use HasFactory, HasCollection;
use HasCollection, HasFactory;

protected $fillable = [
'user_id',
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Country.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class Country extends Model
{
use HasFactory, HasCollection;
use HasCollection, HasFactory;

protected $fillable = [
'currency_id',
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class Currency extends Model
{
use HasFactory, HasCollection;
use HasCollection, HasFactory;

protected $fillable = [
'code',
Expand Down
2 changes: 1 addition & 1 deletion app/Models/ExchangeRate.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class ExchangeRate extends Model
{
use HasFactory, HasCollection;
use HasCollection, HasFactory;

protected $fillable = [
'base_currency_id',
Expand Down
2 changes: 1 addition & 1 deletion app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class User extends Authenticatable
{
use HasApiTokens, HasFactory, HasCollection, Notifiable;
use HasApiTokens, HasCollection, HasFactory, Notifiable;

protected $fillable = [
'name',
Expand Down
7 changes: 4 additions & 3 deletions app/Objects/KataChallengeResultObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ public function getReflectionMethod(): ReflectionMethod
public function getStats(): array
{
$extraReturn = [];

$violations = $this->getViolations();
$violations = $this->reflectionMethod->class::SKIP_VIOLATIONS
? []
: $this->getViolations();

$profile = data_get($this->result, KataRunnerIterationMode::XDEBUG_PROFILE->value, []);
if (! empty($profile)) {
Expand Down Expand Up @@ -72,7 +73,7 @@ public function getStats(): array

public function getStat(
string $key,
KataRunnerIterationMode $kataRunnerIterationMode = null
?KataRunnerIterationMode $kataRunnerIterationMode = null
): mixed {
if (! is_null($kataRunnerIterationMode)) {
return $this->result[$kataRunnerIterationMode->value][$key];
Expand Down
4 changes: 4 additions & 0 deletions app/Providers/RouteServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public function boot()
->prefix('api')
->group(base_path('routes/api.php'));

Route::middleware('mock')
->prefix('mock')
->group(base_path('routes/mock.php'));

Route::middleware('web')
->group(base_path('routes/web.php'));
});
Expand Down
3 changes: 2 additions & 1 deletion app/Services/ExchangeRateService.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ public function getRates(string $url): array
return Cache::get($cacheKey);
}

$rates = Http::get($url)->json('quotes');
$rates = Http::get($url)->json('quotes')
?? Http::get($url)->json('data.quotes');

// Note: Used to reset the static cache (for testing)
// $filepath = sprintf('%s/../../database/seeders/Models/Files/fx_%s.json', __DIR__, $urlMd5);
Expand Down
2 changes: 1 addition & 1 deletion app/Utilities/CodeUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class CodeUtility
{
public static function getCodeSnippet(
ReflectionMethod $reflectionMethod,
int $limit = null
?int $limit = null
): string {
$fileName = $reflectionMethod->getFileName();
$startLine = $reflectionMethod->getStartLine() - 1;
Expand Down
1 change: 1 addition & 0 deletions bin/benchmark.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ source $PATH_TO_REPO/.env

if [ "${CI_MODE}" == "circleci" ]; then
echo "LK_RUN_MODE=benchmark" >> "$PATH_TO_REPO/.env"
echo "EXCHANGE_RATE_API_HOST=$EXCHANGE_RATE_API_HOST" >> "$PATH_TO_REPO/.env"
echo "EXCHANGE_RATE_API_KEY=$EXCHANGE_RATE_API_KEY" >> "$PATH_TO_REPO/.env"
php -d xdebug.mode=profile artisan kata:run --all
else
Expand Down
Loading

0 comments on commit ec0c724

Please sign in to comment.