Skip to content

Namespace functions #245

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

Merged
merged 1 commit into from
Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
12 changes: 6 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/Exception/ProblemFileDoesNotExistException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/ResultRenderer/ResultsRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions src/TestUtils/WorkshopExerciseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Process\Process;

use function PhpSchool\PhpWorkshop\collect;

abstract class WorkshopExerciseTest extends TestCase
{
/**
Expand Down
135 changes: 59 additions & 76 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<mixed> $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<mixed> $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<TKey, T> $array
* @return Collection<TKey, T>
*/
function collect(array $array): Collection
{
/** @var Collection<TKey, T> $collection */
$collection = new Collection($array);
return $collection;
}
/**
* @template TKey of array-key
* @template T
* @param array<TKey, T> $array
* @return Collection<TKey, T>
*/
function collect(array $array): Collection
{
/** @var Collection<TKey, T> $collection */
$collection = new Collection($array);
return $collection;
}


if (!function_exists('any')) {

/**
* @param array<mixed> $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<mixed> $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);
}
4 changes: 4 additions & 0 deletions test/FunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
/**
Expand Down
2 changes: 2 additions & 0 deletions test/ResultRenderer/ResultsRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down