mirror of
https://github.com/archtechx/helpers.git
synced 2025-12-12 04:04:04 +00:00
add code
This commit is contained in:
parent
2945c59e92
commit
0d63a5b79c
12 changed files with 97 additions and 109 deletions
36
README.md
36
README.md
|
|
@ -1,35 +1,7 @@
|
||||||
# REPLACE
|
# archtechx/helpers
|
||||||
|
|
||||||
Simple and flexible package template.
|
A collection of helpers we commonly use in packages or applications.
|
||||||
|
|
||||||
# Usage
|
Documentation will come later, currently not intended for public use.
|
||||||
|
|
||||||
- Replace all occurances of `REPLACE` (case sensitive) with the name of the package namespace. E.g. the `Foo` in `ArchTech\Foo`.
|
Breaking changes may occur, and renamings are likely.
|
||||||
- Also do this for file names, e.g. `REPLACEServiceProvider.php`.
|
|
||||||
- Replace all occurances of `replace` with the name of the package on composer, e.g. the `bar` in `archtechx/bar`.
|
|
||||||
- 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.
|
|
||||||
- If SQLite is wanted, change `DB_CONNECTION` in `phpunit.xml` to `sqlite`, and `DB_DATABASE` to `:memory:`.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Installation
|
|
||||||
|
|
||||||
```sh
|
|
||||||
composer require archtechx/replace
|
|
||||||
```
|
|
||||||
|
|
||||||
## Usage
|
|
||||||
|
|
||||||
```php
|
|
||||||
// ...
|
|
||||||
```
|
|
||||||
|
|
||||||
## Development
|
|
||||||
|
|
||||||
Run all checks locally:
|
|
||||||
|
|
||||||
```sh
|
|
||||||
./check
|
|
||||||
```
|
|
||||||
|
|
||||||
Code style will be automatically fixed by php-cs-fixer.
|
|
||||||
|
|
|
||||||
2
check
2
check
|
|
@ -43,8 +43,6 @@ else
|
||||||
offer_run './vendor/bin/phpstan analyse'
|
offer_run './vendor/bin/phpstan analyse'
|
||||||
fi
|
fi
|
||||||
|
|
||||||
(MYSQL_PORT=3307 docker-compose up -d > /dev/null 2>/dev/null) || true
|
|
||||||
|
|
||||||
if (./vendor/bin/pest > /dev/null 2>/dev/null); then
|
if (./vendor/bin/pest > /dev/null 2>/dev/null); then
|
||||||
echo '✅ PEST OK'
|
echo '✅ PEST OK'
|
||||||
else
|
else
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "archtechx/replace",
|
"name": "archtechx/helpers",
|
||||||
"description": "",
|
"description": "A collection of helpers we commonly use.",
|
||||||
"type": "library",
|
"type": "library",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"authors": [
|
"authors": [
|
||||||
|
|
@ -10,13 +10,11 @@
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"files": ["src/helpers.php"]
|
||||||
"ArchTech\\REPLACE\\": "src/"
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
"autoload-dev": {
|
"autoload-dev": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
"ArchTech\\REPLACE\\Tests\\": "tests/"
|
"ArchTech\\Helpers\\Tests\\": "tests/"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"require": {
|
"require": {
|
||||||
|
|
@ -28,12 +26,5 @@
|
||||||
"nunomaduro/larastan": "^0.6.10",
|
"nunomaduro/larastan": "^0.6.10",
|
||||||
"pestphp/pest": "^1.2",
|
"pestphp/pest": "^1.2",
|
||||||
"pestphp/pest-plugin-laravel": "^1.0"
|
"pestphp/pest-plugin-laravel": "^1.0"
|
||||||
},
|
|
||||||
"extra": {
|
|
||||||
"laravel": {
|
|
||||||
"providers": [
|
|
||||||
"ArchTech\\REPLACE\\PackageServiceProvider"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,12 +0,0 @@
|
||||||
version: '3'
|
|
||||||
services:
|
|
||||||
mysql:
|
|
||||||
image: mysql:5.7
|
|
||||||
environment:
|
|
||||||
MYSQL_ROOT_PASSWORD: password
|
|
||||||
MYSQL_DATABASE: main
|
|
||||||
MYSQL_USER: user
|
|
||||||
MYSQL_PASSWORD: password
|
|
||||||
MYSQL_TCP_PORT: ${MYSQL_PORT}
|
|
||||||
ports:
|
|
||||||
- "${MYSQL_PORT}:${MYSQL_PORT}"
|
|
||||||
|
|
@ -1,32 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace ArchTech\REPLACE;
|
|
||||||
|
|
||||||
use Illuminate\Support\ServiceProvider;
|
|
||||||
|
|
||||||
class REPLACEServiceProvider extends ServiceProvider
|
|
||||||
{
|
|
||||||
public function register(): void
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public function boot(): void
|
|
||||||
{
|
|
||||||
// $this->loadViewsFrom(__DIR__ . '/../assets/views', 'replace');
|
|
||||||
|
|
||||||
// $this->publishes([
|
|
||||||
// __DIR__ . '/../assets/views' => resource_path('views/vendor/replace'),
|
|
||||||
// ], 'replace-views');
|
|
||||||
|
|
||||||
// $this->mergeConfigFrom(
|
|
||||||
// __DIR__ . '/../assets/replace.php',
|
|
||||||
// 'replace'
|
|
||||||
// );
|
|
||||||
|
|
||||||
// $this->publishes([
|
|
||||||
// __DIR__ . '/../assets/replace.php' => config_path('replace.php'),
|
|
||||||
// ], 'replace-config');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
32
src/helpers.php
Normal file
32
src/helpers.php
Normal file
|
|
@ -0,0 +1,32 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
if (! function_exists('uses_trait')) {
|
||||||
|
/** Check if a class or an object uses the specified trait. */
|
||||||
|
function uses_trait(object|string $class, string $trait): bool
|
||||||
|
{
|
||||||
|
return in_array($trait, class_uses_recursive($class), true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! function_exists('is_simple_array')) {
|
||||||
|
/** Checks if a variadic array only has one element that's an array.
|
||||||
|
* @example [['foo', 'bar']] is a simple array
|
||||||
|
* @example [['foo'], ['bar']] is not a simple array
|
||||||
|
*/
|
||||||
|
function is_simple_array(array $variadicArray): bool
|
||||||
|
{
|
||||||
|
return count($variadicArray) === 1 && isset($variadicArray[0]) && is_array($variadicArray[0]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! function_exists('variadic_array')) {
|
||||||
|
/** Converts [[a]] into [a] and [a, b] into [a, b] */
|
||||||
|
function variadic_array(array $items): array
|
||||||
|
{
|
||||||
|
if (is_simple_array($items)) {
|
||||||
|
$items = $items[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $items;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -11,7 +11,7 @@
|
||||||
|
|
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
uses(ArchTech\REPLACE\Tests\TestCase::class)->in('Pest');
|
uses(ArchTech\Helpers\Tests\TestCase::class)->in('Pest');
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|--------------------------------------------------------------------------
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
|
@ -1,9 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
it('succeeds', function () {
|
|
||||||
expect(true)->toBeTrue();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('fails', function () {
|
|
||||||
expect(false)->toBeTrue();
|
|
||||||
});
|
|
||||||
15
tests/Pest/SimpleArrayTest.php
Normal file
15
tests/Pest/SimpleArrayTest.php
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
test("arrays containing multiple items aren't simple arrays")
|
||||||
|
->expect(is_simple_array(['foo', 'bar']))->toBeFalse()
|
||||||
|
->expect(is_simple_array([['foo'], ['bar']]))->toBeFalse();
|
||||||
|
|
||||||
|
test("arrays containing a non-array aren't simple arrays")
|
||||||
|
->expect(
|
||||||
|
is_simple_array(['foo'])
|
||||||
|
)->toBeFalse();
|
||||||
|
|
||||||
|
test("arrays containing a single array are simple arrays")
|
||||||
|
->expect(
|
||||||
|
is_simple_array([['foo', 'bar']])
|
||||||
|
)->toBeTrue();
|
||||||
29
tests/Pest/UsesTraitTest.php
Normal file
29
tests/Pest/UsesTraitTest.php
Normal file
|
|
@ -0,0 +1,29 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
it('checks for the presence of a trait', function () {
|
||||||
|
expect(uses_trait(Foo::class, FirstTrait::class))->toBeTrue();
|
||||||
|
expect(uses_trait(Foo::class, SecondTrait::class))->toBeFalse();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('checks recursively', function () {
|
||||||
|
expect(uses_trait(Bar::class, FirstTrait::class))->toBeTrue(); // inherited
|
||||||
|
expect(uses_trait(Bar::class, SecondTrait::class))->toBeTrue();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('accepts both objects and classes', function () {
|
||||||
|
expect(uses_trait(new Foo, FirstTrait::class))->toBeTrue();
|
||||||
|
expect(uses_trait(new Foo, SecondTrait::class))->toBeFalse();
|
||||||
|
});
|
||||||
|
|
||||||
|
trait FirstTrait {}
|
||||||
|
trait SecondTrait {}
|
||||||
|
|
||||||
|
class Foo
|
||||||
|
{
|
||||||
|
use FirstTrait;
|
||||||
|
}
|
||||||
|
|
||||||
|
class Bar extends Foo
|
||||||
|
{
|
||||||
|
use SecondTrait;
|
||||||
|
}
|
||||||
11
tests/Pest/VariadicArrayTest.php
Normal file
11
tests/Pest/VariadicArrayTest.php
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
it('returns the inner array if a simple array is supplied')
|
||||||
|
->expect(
|
||||||
|
variadic_array([['foo', 'bar']])
|
||||||
|
)->toBe(['foo', 'bar']);
|
||||||
|
|
||||||
|
it('returns the array if a non-simple array is supplied')
|
||||||
|
->expect(
|
||||||
|
variadic_array(['foo', 'bar'])
|
||||||
|
)->toBe(['foo', 'bar']);
|
||||||
|
|
@ -1,16 +1,9 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace ArchTech\REPLACE\Tests;
|
namespace ArchTech\Helpers\Tests;
|
||||||
|
|
||||||
use Orchestra\Testbench\TestCase as TestbenchTestCase;
|
use Orchestra\Testbench\TestCase as TestbenchTestCase;
|
||||||
use ArchTech\REPLACE\REPLACEServiceProvider;
|
|
||||||
|
|
||||||
class TestCase extends TestbenchTestCase
|
class TestCase extends TestbenchTestCase
|
||||||
{
|
{
|
||||||
protected function getPackageProviders($app)
|
|
||||||
{
|
|
||||||
return [
|
|
||||||
REPLACEServiceProvider::class,
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue