Skip to content

Commit 0d63a5b

Browse files
committed
add code
1 parent 2945c59 commit 0d63a5b

12 files changed

+97
-109
lines changed

README.md

+4-32
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,7 @@
1-
# REPLACE
1+
# archtechx/helpers
22

3-
Simple and flexible package template.
3+
A collection of helpers we commonly use in packages or applications.
44

5-
# Usage
5+
Documentation will come later, currently not intended for public use.
66

7-
- Replace all occurances of `REPLACE` (case sensitive) with the name of the package namespace. E.g. the `Foo` in `ArchTech\Foo`.
8-
- Also do this for file names, e.g. `REPLACEServiceProvider.php`.
9-
- Replace all occurances of `replace` with the name of the package on composer, e.g. the `bar` in `archtechx/bar`.
10-
- If MySQL is not needed, remove `docker-compose.yml`, remove the line that runs docker from `./check`, and remove it from the `.github/ci.yml` file.
11-
- If SQLite is wanted, change `DB_CONNECTION` in `phpunit.xml` to `sqlite`, and `DB_DATABASE` to `:memory:`.
12-
13-
---
14-
15-
## Installation
16-
17-
```sh
18-
composer require archtechx/replace
19-
```
20-
21-
## Usage
22-
23-
```php
24-
// ...
25-
```
26-
27-
## Development
28-
29-
Run all checks locally:
30-
31-
```sh
32-
./check
33-
```
34-
35-
Code style will be automatically fixed by php-cs-fixer.
7+
Breaking changes may occur, and renamings are likely.

check

-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ else
4343
offer_run './vendor/bin/phpstan analyse'
4444
fi
4545

46-
(MYSQL_PORT=3307 docker-compose up -d > /dev/null 2>/dev/null) || true
47-
4846
if (./vendor/bin/pest > /dev/null 2>/dev/null); then
4947
echo '✅ PEST OK'
5048
else

composer.json

+4-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"name": "archtechx/replace",
3-
"description": "",
2+
"name": "archtechx/helpers",
3+
"description": "A collection of helpers we commonly use.",
44
"type": "library",
55
"license": "MIT",
66
"authors": [
@@ -10,13 +10,11 @@
1010
}
1111
],
1212
"autoload": {
13-
"psr-4": {
14-
"ArchTech\\REPLACE\\": "src/"
15-
}
13+
"files": ["src/helpers.php"]
1614
},
1715
"autoload-dev": {
1816
"psr-4": {
19-
"ArchTech\\REPLACE\\Tests\\": "tests/"
17+
"ArchTech\\Helpers\\Tests\\": "tests/"
2018
}
2119
},
2220
"require": {
@@ -28,12 +26,5 @@
2826
"nunomaduro/larastan": "^0.6.10",
2927
"pestphp/pest": "^1.2",
3028
"pestphp/pest-plugin-laravel": "^1.0"
31-
},
32-
"extra": {
33-
"laravel": {
34-
"providers": [
35-
"ArchTech\\REPLACE\\PackageServiceProvider"
36-
]
37-
}
3829
}
3930
}

docker-compose.yml

-12
This file was deleted.

src/REPLACEServiceProvider.php

-32
This file was deleted.

src/helpers.php

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
if (! function_exists('uses_trait')) {
4+
/** Check if a class or an object uses the specified trait. */
5+
function uses_trait(object|string $class, string $trait): bool
6+
{
7+
return in_array($trait, class_uses_recursive($class), true);
8+
}
9+
}
10+
11+
if (! function_exists('is_simple_array')) {
12+
/** Checks if a variadic array only has one element that's an array.
13+
* @example [['foo', 'bar']] is a simple array
14+
* @example [['foo'], ['bar']] is not a simple array
15+
*/
16+
function is_simple_array(array $variadicArray): bool
17+
{
18+
return count($variadicArray) === 1 && isset($variadicArray[0]) && is_array($variadicArray[0]);
19+
}
20+
}
21+
22+
if (! function_exists('variadic_array')) {
23+
/** Converts [[a]] into [a] and [a, b] into [a, b] */
24+
function variadic_array(array $items): array
25+
{
26+
if (is_simple_array($items)) {
27+
$items = $items[0];
28+
}
29+
30+
return $items;
31+
}
32+
}

tests/Pest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
|
1212
*/
1313

14-
uses(ArchTech\REPLACE\Tests\TestCase::class)->in('Pest');
14+
uses(ArchTech\Helpers\Tests\TestCase::class)->in('Pest');
1515

1616
/*
1717
|--------------------------------------------------------------------------

tests/Pest/ExampleTest.php

-9
This file was deleted.

tests/Pest/SimpleArrayTest.php

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
test("arrays containing multiple items aren't simple arrays")
4+
->expect(is_simple_array(['foo', 'bar']))->toBeFalse()
5+
->expect(is_simple_array([['foo'], ['bar']]))->toBeFalse();
6+
7+
test("arrays containing a non-array aren't simple arrays")
8+
->expect(
9+
is_simple_array(['foo'])
10+
)->toBeFalse();
11+
12+
test("arrays containing a single array are simple arrays")
13+
->expect(
14+
is_simple_array([['foo', 'bar']])
15+
)->toBeTrue();

tests/Pest/UsesTraitTest.php

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
it('checks for the presence of a trait', function () {
4+
expect(uses_trait(Foo::class, FirstTrait::class))->toBeTrue();
5+
expect(uses_trait(Foo::class, SecondTrait::class))->toBeFalse();
6+
});
7+
8+
it('checks recursively', function () {
9+
expect(uses_trait(Bar::class, FirstTrait::class))->toBeTrue(); // inherited
10+
expect(uses_trait(Bar::class, SecondTrait::class))->toBeTrue();
11+
});
12+
13+
it('accepts both objects and classes', function () {
14+
expect(uses_trait(new Foo, FirstTrait::class))->toBeTrue();
15+
expect(uses_trait(new Foo, SecondTrait::class))->toBeFalse();
16+
});
17+
18+
trait FirstTrait {}
19+
trait SecondTrait {}
20+
21+
class Foo
22+
{
23+
use FirstTrait;
24+
}
25+
26+
class Bar extends Foo
27+
{
28+
use SecondTrait;
29+
}

tests/Pest/VariadicArrayTest.php

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
it('returns the inner array if a simple array is supplied')
4+
->expect(
5+
variadic_array([['foo', 'bar']])
6+
)->toBe(['foo', 'bar']);
7+
8+
it('returns the array if a non-simple array is supplied')
9+
->expect(
10+
variadic_array(['foo', 'bar'])
11+
)->toBe(['foo', 'bar']);

tests/TestCase.php

+1-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
11
<?php
22

3-
namespace ArchTech\REPLACE\Tests;
3+
namespace ArchTech\Helpers\Tests;
44

55
use Orchestra\Testbench\TestCase as TestbenchTestCase;
6-
use ArchTech\REPLACE\REPLACEServiceProvider;
76

87
class TestCase extends TestbenchTestCase
98
{
10-
protected function getPackageProviders($app)
11-
{
12-
return [
13-
REPLACEServiceProvider::class,
14-
];
15-
}
169
}

0 commit comments

Comments
 (0)