Skip to content

Commit 4b21d50

Browse files
authored
Merge pull request #245 from php-school/ns-functions
Namespace functions
2 parents 507c23c + 1eebb75 commit 4b21d50

File tree

9 files changed

+78
-84
lines changed

9 files changed

+78
-84
lines changed

app/config.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use function DI\create;
1616
use function DI\factory;
1717
use Kadet\Highlighter\KeyLighter;
18+
use function PhpSchool\PhpWorkshop\canonicalise_path;
1819
use function PhpSchool\PhpWorkshop\Event\containerListener;
1920
use Psr\Container\ContainerInterface;
2021
use League\CommonMark\DocParser;

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"require-dev": {
3737
"phpunit/phpunit": "^8.5",
3838
"composer/composer": "^2.0",
39-
"squizlabs/php_codesniffer": "^3.4",
39+
"squizlabs/php_codesniffer": "^3.7",
4040
"phpstan/phpstan": "^1.8",
4141
"phpstan/extension-installer": "^1.0",
4242
"yoast/phpunit-polyfills": "^0.2.0"

composer.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Exception/ProblemFileDoesNotExistException.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace PhpSchool\PhpWorkshop\Exception;
66

7+
use function PhpSchool\PhpWorkshop\canonicalise_path;
8+
79
class ProblemFileDoesNotExistException extends \RuntimeException
810
{
911
public static function fromFile(string $file): self

src/ResultRenderer/ResultsRenderer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
use PhpSchool\PhpWorkshop\UserState;
2222
use PhpSchool\PhpWorkshop\Result\FailureInterface;
2323

24-
use function mb_str_pad;
24+
use function PhpSchool\PhpWorkshop\mb_str_pad;
2525

2626
/**
2727
* Renderer which renders a `PhpSchool\PhpWorkshop\ResultAggregator` and writes it to the output.

src/TestUtils/WorkshopExerciseTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
use Symfony\Component\Filesystem\Filesystem;
3232
use Symfony\Component\Process\Process;
3333

34+
use function PhpSchool\PhpWorkshop\collect;
35+
3436
abstract class WorkshopExerciseTest extends TestCase
3537
{
3638
/**

src/functions.php

Lines changed: 59 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -2,93 +2,76 @@
22

33
declare(strict_types=1);
44

5+
namespace PhpSchool\PhpWorkshop;
6+
57
use PhpSchool\PhpWorkshop\Utils\Collection;
68
use PhpSchool\PhpWorkshop\Utils\StringUtils;
79

8-
if (!function_exists('mb_str_pad')) {
9-
10-
/**
11-
* @param string $input
12-
* @param int $padLength
13-
* @param string $padString
14-
* @param int $padType
15-
* @return string
16-
*/
17-
function mb_str_pad(string $input, int $padLength, string $padString = ' ', int $padType = STR_PAD_RIGHT): string
18-
{
19-
$diff = strlen($input) - mb_strlen($input);
20-
return str_pad($input, $padLength + $diff, $padString, $padType);
21-
}
10+
/**
11+
* @param string $input
12+
* @param int $padLength
13+
* @param string $padString
14+
* @param int $padType
15+
* @return string
16+
*/
17+
function mb_str_pad(string $input, int $padLength, string $padString = ' ', int $padType = STR_PAD_RIGHT): string
18+
{
19+
$diff = strlen($input) - mb_strlen($input);
20+
return str_pad($input, $padLength + $diff, $padString, $padType);
2221
}
2322

24-
if (!function_exists('camel_case_to_kebab_case')) {
25-
26-
/**
27-
* @param string $string
28-
* @return string
29-
*/
30-
function camel_case_to_kebab_case(string $string): string
31-
{
32-
return (string) preg_replace_callback('/[A-Z]/', function ($matches) {
33-
return '-' . strtolower($matches[0]);
34-
}, $string);
35-
}
23+
/**
24+
* @param string $string
25+
* @return string
26+
*/
27+
function camel_case_to_kebab_case(string $string): string
28+
{
29+
return (string) preg_replace_callback('/[A-Z]/', function ($matches) {
30+
return '-' . strtolower($matches[0]);
31+
}, $string);
3632
}
3733

38-
if (!function_exists('canonicalise_path')) {
39-
40-
/**
41-
* @param string $path
42-
* @return string
43-
*/
44-
function canonicalise_path(string $path): string
45-
{
46-
return StringUtils::canonicalisePath($path);
47-
}
34+
/**
35+
* @param string $path
36+
* @return string
37+
*/
38+
function canonicalise_path(string $path): string
39+
{
40+
return StringUtils::canonicalisePath($path);
4841
}
4942

50-
if (!function_exists('pluralise')) {
51-
52-
/**
53-
* @param string $string
54-
* @param array<mixed> $items
55-
* @param string[] ...$args
56-
* @return string
57-
*/
58-
function pluralise(string $string, array $items, string ...$args): string
59-
{
60-
return StringUtils::pluralise($string, $items, ...$args);
61-
}
43+
/**
44+
* @param string $string
45+
* @param array<mixed> $items
46+
* @param string[] ...$args
47+
* @return string
48+
*/
49+
function pluralise(string $string, array $items, string ...$args): string
50+
{
51+
return StringUtils::pluralise($string, $items, ...$args);
6252
}
6353

64-
if (!function_exists('collect')) {
65-
66-
/**
67-
* @template TKey of array-key
68-
* @template T
69-
* @param array<TKey, T> $array
70-
* @return Collection<TKey, T>
71-
*/
72-
function collect(array $array): Collection
73-
{
74-
/** @var Collection<TKey, T> $collection */
75-
$collection = new Collection($array);
76-
return $collection;
77-
}
54+
/**
55+
* @template TKey of array-key
56+
* @template T
57+
* @param array<TKey, T> $array
58+
* @return Collection<TKey, T>
59+
*/
60+
function collect(array $array): Collection
61+
{
62+
/** @var Collection<TKey, T> $collection */
63+
$collection = new Collection($array);
64+
return $collection;
7865
}
7966

80-
81-
if (!function_exists('any')) {
82-
83-
/**
84-
* @param array<mixed> $values
85-
* @param callable $cb
86-
* @return bool
87-
*/
88-
function any(array $values, callable $cb): bool
89-
{
90-
return array_reduce($values, function (bool $carry, $value) use ($cb) {
91-
return $carry || $cb($value);
92-
}, false);
93-
}
67+
/**
68+
* @param array<mixed> $values
69+
* @param callable $cb
70+
* @return bool
71+
*/
72+
function any(array $values, callable $cb): bool
73+
{
74+
return array_reduce($values, function (bool $carry, $value) use ($cb) {
75+
return $carry || $cb($value);
76+
}, false);
9477
}

test/FunctionsTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
use PHPUnit\Framework\TestCase;
66

7+
use function PhpSchool\PhpWorkshop\any;
8+
use function PhpSchool\PhpWorkshop\camel_case_to_kebab_case;
9+
use function PhpSchool\PhpWorkshop\mb_str_pad;
10+
711
class FunctionsTest extends TestCase
812
{
913
/**

test/ResultRenderer/ResultsRendererTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
use PhpSchool\PhpWorkshop\UserState;
2424
use PHPUnit\Framework\TestCase;
2525

26+
use function PhpSchool\PhpWorkshop\camel_case_to_kebab_case;
27+
2628
class ResultsRendererTest extends BaseTest
2729
{
2830
public function testRenderIndividualResult(): void

0 commit comments

Comments
 (0)