From 3a054c5ec728b9e76d765cbf899059174127ac81 Mon Sep 17 00:00:00 2001 From: Iman Ghafoori Date: Mon, 6 May 2024 12:18:46 +0330 Subject: [PATCH] drop support for old version of php and laravel --- .github/workflows/check_imports.yml | 2 +- composer.json | 12 +++---- src/Decorator.php | 53 +++++++++++++++++++++++------ src/helpers.php | 38 --------------------- tests/CacheResultDecoratorTest.php | 5 +-- tests/DecoratableFacadeTest.php | 13 +++---- tests/DecoratorTest.php | 32 +++++++---------- 7 files changed, 71 insertions(+), 84 deletions(-) delete mode 100644 src/helpers.php diff --git a/.github/workflows/check_imports.yml b/.github/workflows/check_imports.yml index fe11457..b0e6960 100644 --- a/.github/workflows/check_imports.yml +++ b/.github/workflows/check_imports.yml @@ -27,7 +27,7 @@ jobs: ${{ runner.os }}-php- - name: Install dependencies - run: composer install --prefer-dist --no-progress + run: composer install --prefer-dist --no-progress && composer require imanghafoori/php-imports-analyzer --dev - name: Check Imports run: ./vendor/bin/check_imports diff --git a/composer.json b/composer.json index c85015c..6ab996e 100644 --- a/composer.json +++ b/composer.json @@ -16,21 +16,17 @@ } ], "require": { - "php": "7.2.*|7.3.*|7.4.*|8.0.*|8.1.*|8.2.*|8.3.*|8.4.*", - "laravel/framework": "~5.1|6.*|7.*|8.*|9.*|10.*|^11.0|^12.0" + "php": "7.4.*|8.0.*|8.1.*|8.2.*|8.3.*|8.4.*", + "laravel/framework": "~5.5|6.*|7.*|8.*|9.*|10.*|^11.0|^12.0" }, "require-dev": { "orchestra/testbench": "~6.0|^9.0", - "squizlabs/php_codesniffer": "3.*", - "imanghafoori/php-imports-analyzer": "^1.0.6" + "squizlabs/php_codesniffer": "3.*" }, "autoload": { "psr-4": { "Imanghafoori\\Decorator\\": "src" - }, - "files": [ - "src/helpers.php" - ] + } }, "autoload-dev": { "classmap": [ diff --git a/src/Decorator.php b/src/Decorator.php index 3e41bd5..0284468 100644 --- a/src/Decorator.php +++ b/src/Decorator.php @@ -2,19 +2,21 @@ namespace Imanghafoori\Decorator; -use Illuminate\Support\Str; +use Illuminate\Container\Container; +use ReflectionFunction; +use ReflectionMethod; class Decorator { /** - * All of the decorators for method calls. + * All the decorators for method calls. * * @var array */ protected $globalDecorators = []; /** - * All of the decorator names and definitions. + * All the decorator names and definitions. * * @var array */ @@ -76,7 +78,7 @@ public function call($callback, array $parameters = [], $defaultMethod = null) $callback = $this->decorateWith($callback, $decorators); $parameters = $this->getCallParams($callback, $parameters); - return app()->call($callback, $parameters, $defaultMethod); + return Container::getInstance()->call($callback, $parameters, $defaultMethod); } public function unDecorate($decorated, $decorator = null) @@ -97,13 +99,14 @@ private function normalizeMethod($callback) /** * @param $callable - * @param $decorators + * @param array $decorators * @return mixed + * @throws \ReflectionException */ public function decorateWith($callable, array $decorators) { foreach ($decorators as $decorator) { - if (is_string($decorator) and ! Str::contains($decorator, '@')) { + if (is_string($decorator) and ! self::contains($decorator, '@')) { $decorator = $this->globalDecorators[$decorator]; } @@ -111,7 +114,7 @@ public function decorateWith($callable, array $decorators) ? $params = $this->getCallParams($this->normalizeMethod($decorator), [$callable]) : $params = $this->getCallParams($decorator, [$callable]); - $callable = app()->call($decorator, $params); + $callable = Container::getInstance()->call($decorator, $params); } return $callable; @@ -127,7 +130,7 @@ public function callOnlyWith($callable, $decorators, $params) { $callable = $this->decorateWith($callable, $decorators); - return \App::make($callable, $params); + return Container::getInstance()->make($callable, $params); } /** @@ -142,10 +145,10 @@ public function callOnlyWith($callable, $decorators, $params) public function getCallParams($callable, array $params): array { if (is_callable($callable)) { - $argName = get_func_argNames($callable); + $argName = $this->getFunctionArgNames($callable); } else { $class = explode('@', $callable); - $argName = get_method_argNames($class[0], $class[1]); + $argName = $this->getMethodArgNames($class[0], $class[1]); } $parameters = array_map(function ($MArgName, $Parameters) use ($argName) { return [$MArgName ?? $argName[count($argName) - 1] => $Parameters]; @@ -157,4 +160,34 @@ public function getCallParams($callable, array $params): array return $parameters; } + + /** + * @throws \ReflectionException + * @throws \ReflectionException + */ + private function getMethodArgNames($className, $methodName) + { + return $this->getParameterNames(new ReflectionMethod($className, $methodName)); + } + + /** + * @throws \ReflectionException + */ + private function getFunctionArgNames($funcName) + { + return $this->getParameterNames(new ReflectionFunction($funcName)); + } + + private static function contains($haystack, $needle) + { + return mb_strpos($haystack, $needle) !== false; + + } + + private function getParameterNames($reflection): array + { + return array_map(function ($param) { + return $param->name; + }, $reflection->getParameters()); + } } diff --git a/src/helpers.php b/src/helpers.php deleted file mode 100644 index fd0f5ff..0000000 --- a/src/helpers.php +++ /dev/null @@ -1,38 +0,0 @@ -getParameters() as $param) { - $result[] = $param->name; - } - - return $result; - } -} - -if (! function_exists('get_method_argNames')) { - /** - * @throws ReflectionException - */ - function get_method_argNames($className, $methodName) - { - $f = new ReflectionMethod($className, $methodName); - $result = []; - foreach ($f->getParameters() as $param) { - $result[] = $param->name; - } - - return $result; - } -} diff --git a/tests/CacheResultDecoratorTest.php b/tests/CacheResultDecoratorTest.php index 9892780..c8c0197 100644 --- a/tests/CacheResultDecoratorTest.php +++ b/tests/CacheResultDecoratorTest.php @@ -1,5 +1,6 @@ singleton('abc', abc::class); + Container::getInstance()->singleton('abc', abc::class); \MyFacade::forgetDecorations('getGiven'); \MyFacade::decorateMethod('getGiven', DecoratorFactory::cache('hello', 2)); @@ -22,7 +23,7 @@ public function testCacheResultDecorator() public function testPermanentCacheResultDecorator() { - app()->singleton('abc', abc::class); + Container::getInstance()->singleton('abc', abc::class); \MyFacade::forgetDecorations('getGiven'); \MyFacade::decorateMethod('getGiven', DecoratorFactory::foreverCache(function ($a) { return 'cache_key_'.$a; diff --git a/tests/DecoratableFacadeTest.php b/tests/DecoratableFacadeTest.php index 7705d3b..f95b3b1 100644 --- a/tests/DecoratableFacadeTest.php +++ b/tests/DecoratableFacadeTest.php @@ -1,5 +1,6 @@ singleton('abc', abc::class); + Container::getInstance()->singleton('abc', abc::class); \MyFacade::forgetDecorations('getGiven'); \MyFacade::decorateMethod('getGiven', function ($f) { return function () { @@ -29,8 +30,8 @@ public function testDecoratableFacade() public function testDecoratableFacade2() { - app()->singleton('abc', abc::class); - app(Decorator::class)->define('stringifyResult', [ResultCasterDecorator::class, 'toStringStaticDecorator']); + Container::getInstance()->singleton('abc', abc::class); + Container::getInstance()->make(Decorator::class)->define('stringifyResult', [ResultCasterDecorator::class, 'toStringStaticDecorator']); \MyFacade::forgetDecorations(); \MyFacade::decorateMethod('getGiven', 'stringifyResult'); @@ -42,7 +43,7 @@ public function testDecoratableFacade2() public function testStaticMethodsAsDecoratorsOnFacades() { - app()->singleton('abc', abc::class); + Container::getInstance()->singleton('abc', abc::class); \MyFacade::decorateMethod('getGiven', ResultCasterDecorator::class.'@toStringDecorator'); $this->assertIsString(\MyFacade::getGiven(1)); @@ -52,7 +53,7 @@ public function testStaticMethodsAsDecoratorsOnFacades() public function testFacadeClassDecorators() { - app()->singleton('abc', abc::class); + Container::getInstance()->singleton('abc', abc::class); \MyFacade::decorateAll(ResultCasterDecorator::class.'@minimumParamZero'); \MyFacade::decorateAll(ResultCasterDecorator::class.'@toStringDecorator'); @@ -65,7 +66,7 @@ public function testFacadeClassDecorators() public function testFacadeClassDecoratorsCombination() { - app()->singleton('abc', abc::class); + Container::getInstance()->singleton('abc', abc::class); \MyFacade::decorateMethod('getGiven', ResultCasterDecorator::class.'@minimumParamZero'); \MyFacade::decorateAll(ResultCasterDecorator::class.'@toStringDecorator'); diff --git a/tests/DecoratorTest.php b/tests/DecoratorTest.php index 0c812e1..bbffd14 100644 --- a/tests/DecoratorTest.php +++ b/tests/DecoratorTest.php @@ -1,5 +1,6 @@ define('stringifyResult', $this->getResultCasterDecorator()); $decorator->decorate(ICalculator::class.'@add', 'stringifyResult'); - app()->bind(ICalculator::class, Calculator::class); + Container::getInstance()->bind(ICalculator::class, Calculator::class); $result = $decorator->call(ICalculator::class.'@add', [10, 10]); $this->assertIsString($result); @@ -91,13 +92,13 @@ public function testTwoDecorators() $stringifyDecorator = function ($decorated) { return function (...$params) use ($decorated) { - return (string) app()->call($decorated, app('decorator')->getCallParams($decorated, $params[0])); + return (string) Container::getInstance()->call($decorated, Container::getInstance()->make('decorator')->getCallParams($decorated, $params[0])); }; }; $intifyParamsDecorator = function ($decorated) { return function ($x, $y) use ($decorated) { - return app()->call($decorated, app('decorator')->getCallParams($decorated, [(int) $x, (int) $y])); + return Container::getInstance()->call($decorated, Container::getInstance()->make('decorator')->getCallParams($decorated, [(int) $x, (int) $y])); }; }; @@ -122,7 +123,7 @@ public function testMultipleDecorators() $x = ($x < -20) ? -20 : $x; $y = ($y < -20) ? -20 : $y; - return app()->call($callback, app('decorator')->getCallParams($callback, [$x, $y])); + return Container::getInstance()->call($callback, Container::getInstance()->make('decorator')->getCallParams($callback, [$x, $y])); }; }); @@ -159,7 +160,7 @@ public function getResultCasterDecorator() { return function ($callable) { return function (...$params) use ($callable) { - return (string) app()->call($callable, app('decorator')->getCallParams($callable, $params[0])); + return (string) Container::getInstance()->call($callable, Container::getInstance()->make('decorator')->getCallParams($callable, $params[0])); }; }; } @@ -168,11 +169,11 @@ public function getResultCasterDecorator() * @param int $max * @return \Closure */ - private function maxResult(int $max): \Closure + private function maxResult(int $max): Closure { return function ($callable) use ($max) { return function (...$params) use ($callable, $max) { - $result = app()->call($callable, app('decorator')->getCallParams($callable, $params[0])); + $result = Container::getInstance()->call($callable, Container::getInstance()->make('decorator')->getCallParams($callable, $params[0])); return ($result > $max) ? $max : $result; }; @@ -192,13 +193,6 @@ public function add(int $x, int $y): int return $x + $y; } - public function arraySum(...$x) - { - app()->call($this->addToStr($x, $y)); - - return array_sum($x); - } - public function addToStr(int $x, int $y): string { return (string) ($x + $y); @@ -210,7 +204,7 @@ class ResultCasterDecorator public function toStringDecorator($callable) { return function (...$params) use ($callable) { - return (string) app()->call($callable, app('decorator')->getCallParams($callable, is_array($params[0]) ? $params[0] : $params)); + return (string) Container::getInstance()->call($callable, Container::getInstance()->make('decorator')->getCallParams($callable, is_array($params[0]) ? $params[0] : $params)); }; } @@ -223,28 +217,28 @@ public function minimumParamZero($callable) } } - return app()->call($callable, $params); + return Container::getInstance()->call($callable, $params); }; } public static function toStringStaticDecorator($callable) { return function (...$params) use ($callable) { - return (string) app()->call($callable, $params); + return (string) Container::getInstance()->call($callable, $params); }; } public static function toInt($callable) { return function (...$params) use ($callable) { - return (int) app()->call($callable, app('decorator')->getCallParams($callable, is_array($params[0]) ? $params[0] : $params)); + return (int) Container::getInstance()->call($callable, Container::getInstance()->make('decorator')->getCallParams($callable, is_array($params[0]) ? $params[0] : $params)); }; } public function _toString($callable) { return function (...$params) use ($callable) { - return (string) app()->call($callable, ['x' => $params[0][0], 'y' => $params[0][1]]); + return (string) Container::getInstance()->call($callable, ['x' => $params[0][0], 'y' => $params[0][1]]); }; } }