From 1eebb75bd86ab1373819835b3e332613b14ae978 Mon Sep 17 00:00:00 2001 From: Aydin Hassan Date: Wed, 2 Nov 2022 15:04:12 +0100 Subject: [PATCH] Namespace functions --- app/config.php | 1 + composer.json | 2 +- composer.lock | 12 +- .../ProblemFileDoesNotExistException.php | 2 + src/ResultRenderer/ResultsRenderer.php | 2 +- src/TestUtils/WorkshopExerciseTest.php | 2 + src/functions.php | 135 ++++++++---------- test/FunctionsTest.php | 4 + test/ResultRenderer/ResultsRendererTest.php | 2 + 9 files changed, 78 insertions(+), 84 deletions(-) diff --git a/app/config.php b/app/config.php index b96c1db3..f8d7677b 100644 --- a/app/config.php +++ b/app/config.php @@ -15,6 +15,7 @@ use function DI\create; use function DI\factory; use Kadet\Highlighter\KeyLighter; +use function PhpSchool\PhpWorkshop\canonicalise_path; use function PhpSchool\PhpWorkshop\Event\containerListener; use Psr\Container\ContainerInterface; use League\CommonMark\DocParser; diff --git a/composer.json b/composer.json index 6a0fa1e4..292d56d4 100644 --- a/composer.json +++ b/composer.json @@ -36,7 +36,7 @@ "require-dev": { "phpunit/phpunit": "^8.5", "composer/composer": "^2.0", - "squizlabs/php_codesniffer": "^3.4", + "squizlabs/php_codesniffer": "^3.7", "phpstan/phpstan": "^1.8", "phpstan/extension-installer": "^1.0", "yoast/phpunit-polyfills": "^0.2.0" diff --git a/composer.lock b/composer.lock index 4544ec4b..0ce85e2d 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "adb94abf9c90848fac38603659614bf9", + "content-hash": "0fbfa2965bb50984225e1613c6ad8bae", "packages": [ { "name": "aydin-hassan/cli-md-renderer", @@ -4114,16 +4114,16 @@ }, { "name": "squizlabs/php_codesniffer", - "version": "3.6.0", + "version": "3.7.1", "source": { "type": "git", "url": "https://github.com/squizlabs/PHP_CodeSniffer.git", - "reference": "ffced0d2c8fa8e6cdc4d695a743271fab6c38625" + "reference": "1359e176e9307e906dc3d890bcc9603ff6d90619" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/ffced0d2c8fa8e6cdc4d695a743271fab6c38625", - "reference": "ffced0d2c8fa8e6cdc4d695a743271fab6c38625", + "url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/1359e176e9307e906dc3d890bcc9603ff6d90619", + "reference": "1359e176e9307e906dc3d890bcc9603ff6d90619", "shasum": "" }, "require": { @@ -4166,7 +4166,7 @@ "source": "https://github.com/squizlabs/PHP_CodeSniffer", "wiki": "https://github.com/squizlabs/PHP_CodeSniffer/wiki" }, - "time": "2021-04-09T00:54:41+00:00" + "time": "2022-06-18T07:21:10+00:00" }, { "name": "symfony/console", diff --git a/src/Exception/ProblemFileDoesNotExistException.php b/src/Exception/ProblemFileDoesNotExistException.php index e26808f3..698e198a 100644 --- a/src/Exception/ProblemFileDoesNotExistException.php +++ b/src/Exception/ProblemFileDoesNotExistException.php @@ -4,6 +4,8 @@ namespace PhpSchool\PhpWorkshop\Exception; +use function PhpSchool\PhpWorkshop\canonicalise_path; + class ProblemFileDoesNotExistException extends \RuntimeException { public static function fromFile(string $file): self diff --git a/src/ResultRenderer/ResultsRenderer.php b/src/ResultRenderer/ResultsRenderer.php index 02dd5250..014bbcbd 100644 --- a/src/ResultRenderer/ResultsRenderer.php +++ b/src/ResultRenderer/ResultsRenderer.php @@ -21,7 +21,7 @@ use PhpSchool\PhpWorkshop\UserState; use PhpSchool\PhpWorkshop\Result\FailureInterface; -use function mb_str_pad; +use function PhpSchool\PhpWorkshop\mb_str_pad; /** * Renderer which renders a `PhpSchool\PhpWorkshop\ResultAggregator` and writes it to the output. diff --git a/src/TestUtils/WorkshopExerciseTest.php b/src/TestUtils/WorkshopExerciseTest.php index 701b4379..ae8a35af 100644 --- a/src/TestUtils/WorkshopExerciseTest.php +++ b/src/TestUtils/WorkshopExerciseTest.php @@ -31,6 +31,8 @@ use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Process\Process; +use function PhpSchool\PhpWorkshop\collect; + abstract class WorkshopExerciseTest extends TestCase { /** diff --git a/src/functions.php b/src/functions.php index 334cfb9a..a0bf6a23 100644 --- a/src/functions.php +++ b/src/functions.php @@ -2,93 +2,76 @@ declare(strict_types=1); +namespace PhpSchool\PhpWorkshop; + use PhpSchool\PhpWorkshop\Utils\Collection; use PhpSchool\PhpWorkshop\Utils\StringUtils; -if (!function_exists('mb_str_pad')) { - - /** - * @param string $input - * @param int $padLength - * @param string $padString - * @param int $padType - * @return string - */ - function mb_str_pad(string $input, int $padLength, string $padString = ' ', int $padType = STR_PAD_RIGHT): string - { - $diff = strlen($input) - mb_strlen($input); - return str_pad($input, $padLength + $diff, $padString, $padType); - } +/** + * @param string $input + * @param int $padLength + * @param string $padString + * @param int $padType + * @return string + */ +function mb_str_pad(string $input, int $padLength, string $padString = ' ', int $padType = STR_PAD_RIGHT): string +{ + $diff = strlen($input) - mb_strlen($input); + return str_pad($input, $padLength + $diff, $padString, $padType); } -if (!function_exists('camel_case_to_kebab_case')) { - - /** - * @param string $string - * @return string - */ - function camel_case_to_kebab_case(string $string): string - { - return (string) preg_replace_callback('/[A-Z]/', function ($matches) { - return '-' . strtolower($matches[0]); - }, $string); - } +/** + * @param string $string + * @return string + */ +function camel_case_to_kebab_case(string $string): string +{ + return (string) preg_replace_callback('/[A-Z]/', function ($matches) { + return '-' . strtolower($matches[0]); + }, $string); } -if (!function_exists('canonicalise_path')) { - - /** - * @param string $path - * @return string - */ - function canonicalise_path(string $path): string - { - return StringUtils::canonicalisePath($path); - } +/** + * @param string $path + * @return string + */ +function canonicalise_path(string $path): string +{ + return StringUtils::canonicalisePath($path); } -if (!function_exists('pluralise')) { - - /** - * @param string $string - * @param array $items - * @param string[] ...$args - * @return string - */ - function pluralise(string $string, array $items, string ...$args): string - { - return StringUtils::pluralise($string, $items, ...$args); - } +/** + * @param string $string + * @param array $items + * @param string[] ...$args + * @return string + */ +function pluralise(string $string, array $items, string ...$args): string +{ + return StringUtils::pluralise($string, $items, ...$args); } -if (!function_exists('collect')) { - - /** - * @template TKey of array-key - * @template T - * @param array $array - * @return Collection - */ - function collect(array $array): Collection - { - /** @var Collection $collection */ - $collection = new Collection($array); - return $collection; - } +/** + * @template TKey of array-key + * @template T + * @param array $array + * @return Collection + */ +function collect(array $array): Collection +{ + /** @var Collection $collection */ + $collection = new Collection($array); + return $collection; } - -if (!function_exists('any')) { - - /** - * @param array $values - * @param callable $cb - * @return bool - */ - function any(array $values, callable $cb): bool - { - return array_reduce($values, function (bool $carry, $value) use ($cb) { - return $carry || $cb($value); - }, false); - } +/** + * @param array $values + * @param callable $cb + * @return bool + */ +function any(array $values, callable $cb): bool +{ + return array_reduce($values, function (bool $carry, $value) use ($cb) { + return $carry || $cb($value); + }, false); } diff --git a/test/FunctionsTest.php b/test/FunctionsTest.php index cff066a1..c36e2e8e 100644 --- a/test/FunctionsTest.php +++ b/test/FunctionsTest.php @@ -4,6 +4,10 @@ use PHPUnit\Framework\TestCase; +use function PhpSchool\PhpWorkshop\any; +use function PhpSchool\PhpWorkshop\camel_case_to_kebab_case; +use function PhpSchool\PhpWorkshop\mb_str_pad; + class FunctionsTest extends TestCase { /** diff --git a/test/ResultRenderer/ResultsRendererTest.php b/test/ResultRenderer/ResultsRendererTest.php index 7494e290..f4470e45 100644 --- a/test/ResultRenderer/ResultsRendererTest.php +++ b/test/ResultRenderer/ResultsRendererTest.php @@ -23,6 +23,8 @@ use PhpSchool\PhpWorkshop\UserState; use PHPUnit\Framework\TestCase; +use function PhpSchool\PhpWorkshop\camel_case_to_kebab_case; + class ResultsRendererTest extends BaseTest { public function testRenderIndividualResult(): void