From d8b17a062e0577510bd43dd90d4f040047ced1a7 Mon Sep 17 00:00:00 2001 From: Alberto Rodriguez Date: Thu, 8 Feb 2024 10:23:20 -0400 Subject: [PATCH 01/11] update readme --- .gitignore | 1 + README.md | 42 +++++++++++++++++++++--------------------- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/.gitignore b/.gitignore index 244d127..2a55d71 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ /config/Migrations/schema-dump-default.lock /vendor/ /.idea/ +/.phpunit.cache \ No newline at end of file diff --git a/README.md b/README.md index a08a980..5975d37 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ -CakePHP Money Plugin -=================== +# CakePHP Money Plugin +

Software License @@ -22,45 +22,45 @@ It covers the following features: * Store Money objects onto database (as strings) * Marshal money objects into CakePHP entities. -Requirements ------------- +## Requirements * CakePHP 4.0.0+ * PHP 7.2+ -Versions and branches ---------------------- +## Versions and branches + +| CakePHP | CakeDC Money Plugin | Notes | +| :-------------: | :------------------------: | :---- | +| ^4.5 | [1.x](https://github.com/cakedc/money/tree/1.next-cake4) | stable | +| ^4.0 | [0.0.1](https://github.com/cakedc/money/tree/0.0.1) | deprecated | + +## Installation -| CakePHP | CakeDC Money Plugin | Tag | Notes | -| :-------------: | :------------------------: | :--: | :---- | -| ^4.0 | [0.1](https://github.com/cakedc/money/tree/1.next-cake4) | 0.0.1 | stable | +You can install this plugin into your CakePHP application using [composer](http://getcomposer.org). +The recommended way to install composer packages is: -Documentation -------------- +``` +composer require cakedc/cakephp-money +``` + +## Documentation For documentation, as well as tutorials, see the [Docs](Docs/Home.md) directory of this repository. -Support -------- +## Support For bugs and feature requests, please use the [issues](https://github.com/CakeDC/money/issues) section of this repository. Commercial support is also available, [contact us](https://www.cakedc.com/contact) for more information. -Contributing ------------- +## Contributing This repository follows the [CakeDC Plugin Standard](https://www.cakedc.com/plugin-standard). If you'd like to contribute new features, enhancements or bug fixes to the plugin, please read our [Contribution Guidelines](https://www.cakedc.com/contribution-guidelines) for detailed instructions. -License -------- +## License Copyright 2021 Cake Development Corporation (CakeDC). All rights reserved. Licensed under the [MIT](http://www.opensource.org/licenses/mit-license.php) License. Redistributions of the source code included in this repository must retain the copyright notice found in each file. - -## To Do - -* Add Unit Tests From c7fe0af43e5c32fad20fdfb3b90701ef27de1423 Mon Sep 17 00:00:00 2001 From: Alberto Rodriguez Date: Thu, 8 Feb 2024 12:45:46 -0400 Subject: [PATCH 02/11] feat: update php versions on gh actions --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3207304..4c636c5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,7 +11,7 @@ jobs: strategy: fail-fast: false matrix: - php-version: ['7.2', '7.3', '7.4', '8.0'] + php-version: ['7.4', '8.0', '8.1', '8.2', '8.3'] db-type: [sqlite, mysql, pgsql] prefer-lowest: [''] From 3e22b3c1f3a8209e49ccdf1fc2f4b932262c3fa8 Mon Sep 17 00:00:00 2001 From: Alberto Rodriguez Date: Thu, 8 Feb 2024 13:11:59 -0400 Subject: [PATCH 03/11] update gb actions --- .github/workflows/ci.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4c636c5..812aa85 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -7,7 +7,7 @@ on: jobs: testsuite: - runs-on: ubuntu-18.04 + runs-on: ubuntu-22.04 strategy: fail-fast: false matrix: @@ -66,7 +66,7 @@ jobs: if [[ ${{ matrix.db-type }} == 'mysql' ]]; then export DB_URL='mysql://root:root@127.0.0.1/cakephp'; fi if [[ ${{ matrix.db-type }} == 'pgsql' ]]; then export DB_URL='postgres://postgres:postgres@127.0.0.1/postgres'; fi if [[ ${{ matrix.php-version }} == '7.4' ]]; then - export CODECOVERAGE=1 && vendor/bin/phpunit --verbose --coverage-clover=coverage.xml + export CODECOVERAGE=1 && vendor/bin/phpunit --coverage-clover=coverage.xml else vendor/bin/phpunit fi @@ -77,7 +77,7 @@ jobs: cs-stan: name: Coding Standard & Static Analysis - runs-on: ubuntu-18.04 + runs-on: ubuntu-22.04 steps: - uses: actions/checkout@v2 From 9ce4bafc411944f48ac88ece44aa6db1d241cbe5 Mon Sep 17 00:00:00 2001 From: Antonio Reveron Date: Thu, 22 Feb 2024 15:38:59 +0000 Subject: [PATCH 04/11] fixed issue where __call method sometimes returned an \money\money object --- .gitignore | 3 ++- CHANGELOG.md | 4 +++- src/Money.php | 7 ++++++- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 2a55d71..56b0dbd 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,5 @@ /config/Migrations/schema-dump-default.lock /vendor/ /.idea/ -/.phpunit.cache \ No newline at end of file +/.phpunit.cache +/.ddev/ diff --git a/CHANGELOG.md b/CHANGELOG.md index d9e0afd..df65918 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +* Fixed issue where `__call()` method sometimes returned an object with class \Money\Money instead of CakeDC\Money\. + ## [0.0.1] - 2021-06-18 -* Initial version \ No newline at end of file +* Initial version diff --git a/src/Money.php b/src/Money.php index 2ebc7cf..cc2a55a 100644 --- a/src/Money.php +++ b/src/Money.php @@ -51,7 +51,12 @@ public function __call($name, $arguments) { $arguments = self::processArguments($arguments); - return call_user_func_array([$this->_money, $name], $arguments); + $result = call_user_func_array([$this->_money, $name], $arguments); + if ($result instanceof MoneyPHP) { + return new self($result); + } + + return $result; } /** From abb1a1183aefd087b0577915e4b49b2b5858540c Mon Sep 17 00:00:00 2001 From: Antonio Reveron Date: Fri, 23 Feb 2024 13:01:06 +0000 Subject: [PATCH 05/11] update changelog description --- CHANGELOG.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index df65918..95755bb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] -* Fixed issue where `__call()` method sometimes returned an object with class \Money\Money instead of CakeDC\Money\. +## [1.0.1] - 2024-02-23 + +### Fixed +- Fixed issue where `__call()` method sometimes returned an object with class `\Money\Money` instead of `\CakeDC\Money\Money`. ## [0.0.1] - 2021-06-18 From e0d86804f5d0b3e7c2dcd96f7ec369f95195df3e Mon Sep 17 00:00:00 2001 From: Adam Rusinowski Date: Thu, 29 Feb 2024 20:42:45 +0100 Subject: [PATCH 06/11] improved code completion for class \CakeDC\Money\Money --- src/Money.php | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/Money.php b/src/Money.php index cc2a55a..4d7a075 100644 --- a/src/Money.php +++ b/src/Money.php @@ -19,10 +19,54 @@ * * This class encapsulates MoneyPHP\Money and extends features * + * @method bool isSameCurrency(\CakeDC\Money\Money $other) See \Money\Money::isSameCurrency() + * @method bool equals(\CakeDC\Money\Money $other) See \Money\Money::equals() + * @method int compare(\CakeDC\Money\Money $other) See \Money\Money::compare() + * @method bool greaterThan(\CakeDC\Money\Money $other) See \Money\Money::greaterThan() + * @method bool greaterThanOrEqual(\CakeDC\Money\Money $other) See \Money\Money::greaterThanOrEqual() + * @method bool lessThan(\CakeDC\Money\Money $other) See \Money\Money::lessThan() + * @method bool lessThanOrEqual(\CakeDC\Money\Money $other) See \Money\Money::lessThanOrEqual() + * @method string getAmount() See \Money\Money::getAmount() + * @method \Money\Currency getCurrency() See \Money\Money::getCurrency() + * @method \CakeDC\Money\Money add(\CakeDC\Money\Money ...$addends) See \Money\Money::add() + * @method \CakeDC\Money\Money subtract(\CakeDC\Money\Money ...$subtrahends) See \Money\Money::subtract() + * @method \CakeDC\Money\Money multiply($multiplier, $roundingMode = \CakeDC\Money\Money::ROUND_HALF_UP) See \Money\Money::multiply() + * @method \CakeDC\Money\Money divide($divisor, $roundingMode = \CakeDC\Money\Money::ROUND_HALF_UP) See \Money\Money::divide() + * @method \CakeDC\Money\Money mod(\CakeDC\Money\Money $divisor) See \Money\Money::mod() + * @method \CakeDC\Money\Money[] allocate(array $ratios) See \Money\Money::allocate() + * @method \CakeDC\Money\Money[] allocateTo($n) See \Money\Money::allocateTo() + * @method string ratioOf(Money $money) See \Money\Money::ratioOf() + * @method \CakeDC\Money\Money absolute() See \Money\Money::absolute() + * @method \CakeDC\Money\Money negative() See \Money\Money::negative() + * @method bool isZero() See \Money\Money::isZero() + * @method bool isPositive() See \Money\Money::isPositive() + * @method bool isNegative() See \Money\Money::isNegative() + * @method bool jsonSerialize() See \Money\Money::jsonSerialize() + * @method static \CakeDC\Money\Money min(\CakeDC\Money\Money $first, \CakeDC\Money\Money ...$collection) See \Money\Money::min() + * @method static \CakeDC\Money\Money max(\CakeDC\Money\Money $first, \CakeDC\Money\Money ...$collection) See \Money\Money::max() + * @method static \CakeDC\Money\Money sum(\CakeDC\Money\Money $first, \CakeDC\Money\Money ...$collection) See \Money\Money::sum() + * @method static \CakeDC\Money\Money avg(\CakeDC\Money\Money $first, \CakeDC\Money\Money ...$collection) See \Money\Money::avg() + * @method static void registerCalculator(string $calculator) See \Money\Money::registerCalculator() + * @see \Money\Money * @package CakeDC\Money */ class Money { + const ROUND_HALF_UP = PHP_ROUND_HALF_UP; + + const ROUND_HALF_DOWN = PHP_ROUND_HALF_DOWN; + + const ROUND_HALF_EVEN = PHP_ROUND_HALF_EVEN; + + const ROUND_HALF_ODD = PHP_ROUND_HALF_ODD; + + const ROUND_UP = 5; + + const ROUND_DOWN = 6; + + const ROUND_HALF_POSITIVE_INFINITY = 7; + + const ROUND_HALF_NEGATIVE_INFINITY = 8; /** * @var MoneyPHP From 0ff1968f0fc1d71791197cac4112ff5a186fd2f9 Mon Sep 17 00:00:00 2001 From: Adam Rusinowski Date: Fri, 1 Mar 2024 11:07:37 +0100 Subject: [PATCH 07/11] fixed issues reported by phpstan --- .phive/phars.xml | 5 +++++ composer.json | 24 +++++++++++++----------- phpstan-baseline.neon | 2 ++ phpstan.neon | 15 +++++++++------ psalm.xml | 3 ++- src/Money.php | 15 ++++++++------- src/Plugin.php | 6 +++--- src/Utility/MoneyUtil.php | 16 +++++++++------- src/View/Helper/MoneyHelper.php | 7 ++++++- tests/bootstrap.php | 2 +- 10 files changed, 58 insertions(+), 37 deletions(-) create mode 100644 .phive/phars.xml create mode 100644 phpstan-baseline.neon diff --git a/.phive/phars.xml b/.phive/phars.xml new file mode 100644 index 0000000..9aa6dfe --- /dev/null +++ b/.phive/phars.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/composer.json b/composer.json index a5b1b16..4783206 100644 --- a/composer.json +++ b/composer.json @@ -24,19 +24,21 @@ }, "scripts": { "check": [ - "@test", - "@cs-check" + "@cs-check", + "@test" ], - "analyse": [ - "@stan", + "cs-check": "phpcs --colors --parallel=16 -p src/ tests/", + "cs-fix": "phpcbf --colors --parallel=16 -p src/ tests/", + "phpstan": "tools/phpstan analyse", + "psalm": "tools/psalm --show-info=false", + "stan": [ + "@phpstan", "@psalm" ], - "cs-check": "phpcs -n -p --standard=vendor/cakephp/cakephp-codesniffer/CakePHP ./src ./tests", - "cs-fix": "phpcbf --standard=vendor/cakephp/cakephp-codesniffer/CakePHP ./src ./tests", - "test": "phpunit --stderr", - "stan": "phpstan analyse src/", - "psalm": "php vendor/psalm/phar/psalm.phar --show-info=false src/ ", - "stan-setup": "cp composer.json composer.backup && composer require --dev phpstan/phpstan:0.12.88 psalm/phar:~4.7.0 && mv composer.backup composer.json", - "coverage-test": "phpunit --stderr --coverage-clover=clover.xml" + "phpstan-tests": "tools/phpstan analyze -c tests/phpstan.neon", + "phpstan-baseline": "tools/phpstan --generate-baseline", + "psalm-baseline": "tools/psalm --set-baseline=psalm-baseline.xml", + "stan-setup": "phive install", + "test": "phpunit" } } diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon new file mode 100644 index 0000000..364905f --- /dev/null +++ b/phpstan-baseline.neon @@ -0,0 +1,2 @@ +parameters: + ignoreErrors: diff --git a/phpstan.neon b/phpstan.neon index 2bc6593..7dc051f 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,7 +1,10 @@ +includes: + - phpstan-baseline.neon parameters: - level: 6 - autoload_files: - - tests/bootstrap.php - ignoreErrors: - - '#Method CakeDC\\Auth\\Rbac\\Rules\\AbstractRule::_getTableFromRequest\(\) should return Cake\\ORM\\Table but returns Cake\\Datasource\\RepositoryInterface.#' -services: + level: 8 + checkMissingIterableValueType: false + checkGenericClassInNonGenericObjectType: false + bootstrapFiles: + - tests/bootstrap.php + paths: + - src/ diff --git a/psalm.xml b/psalm.xml index cc8fde3..3ee6ae7 100644 --- a/psalm.xml +++ b/psalm.xml @@ -6,6 +6,7 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="https://getpsalm.org/schema/config" xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd" + autoloader="tests/bootstrap.php" > @@ -42,7 +43,7 @@ - + diff --git a/src/Money.php b/src/Money.php index 4d7a075..a068dbe 100644 --- a/src/Money.php +++ b/src/Money.php @@ -87,14 +87,14 @@ public function __construct(MoneyPHP $money) } /** - * @param $name - * @param $arguments + * @param string $name + * @param array $arguments * @return false|mixed */ - public function __call($name, $arguments) + public function __call(string $name, array $arguments) { $arguments = self::processArguments($arguments); - + // @phpstan-ignore-next-line $result = call_user_func_array([$this->_money, $name], $arguments); if ($result instanceof MoneyPHP) { return new self($result); @@ -104,14 +104,15 @@ public function __call($name, $arguments) } /** - * @param $name - * @param $arguments + * @param string $name + * @param array $arguments * @return false|mixed */ - public static function __callStatic($name, $arguments) + public static function __callStatic(string $name, array $arguments) { $arguments = self::processArguments($arguments); + // @phpstan-ignore-next-line return new self(forward_static_call_array([MoneyPHP::class, $name], $arguments)); } diff --git a/src/Plugin.php b/src/Plugin.php index 929d838..2541eea 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -14,7 +14,7 @@ use Cake\Core\BasePlugin; use Cake\Core\PluginApplicationInterface; -use Cake\Database\Type; +use Cake\Database\TypeFactory; use Cake\Http\MiddlewareQueue; use Cake\Routing\RouteBuilder; use CakeDC\Money\Database\Type\MoneyType; @@ -35,7 +35,7 @@ class Plugin extends BasePlugin */ public function bootstrap(PluginApplicationInterface $app): void { - Type::map('money', MoneyType::class); + TypeFactory::map('money', MoneyType::class); } /** @@ -64,7 +64,7 @@ function (RouteBuilder $builder) { /** * Add middleware for the plugin. * - * @param \Cake\Http\MiddlewareQueue $middleware The middleware queue to update. + * @param \Cake\Http\MiddlewareQueue $middlewareQueue The middleware queue to update. * @return \Cake\Http\MiddlewareQueue */ public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue diff --git a/src/Utility/MoneyUtil.php b/src/Utility/MoneyUtil.php index 3813bdd..e7182d2 100644 --- a/src/Utility/MoneyUtil.php +++ b/src/Utility/MoneyUtil.php @@ -29,18 +29,18 @@ class MoneyUtil { /** - * @var MoneyFormatter + * @var \Money\MoneyFormatter[] */ protected static $_moneyFormatters = []; /** * Returns a new object of type Money * - * @param int|float|string $value + * @param \CakeDC\Money\Money|int|float|string $value * @param boolean $fromDb * @return Money */ - public static function money($value, $fromDb = false) : ?Money + public static function money($value, bool $fromDb = false) : ?Money { if (!is_numeric($value) && empty($value)) { return null; @@ -50,12 +50,12 @@ public static function money($value, $fromDb = false) : ?Money } if (!$fromDb) { - $parts = explode('.', $value ); + $parts = explode('.', (string)$value); if (!isset($parts[1])) { $parts[1] = '00'; } - $decimalLength = strlen($parts[1] ?? ''); + $decimalLength = strlen($parts[1]); if ($decimalLength == 1) { $parts[1] = $parts[1] . '0'; @@ -65,7 +65,8 @@ public static function money($value, $fromDb = false) : ?Money } $currency = Configure::read('Money.currency', 'USD'); - return Money::{$currency}(!empty($value) ? str_replace(',', '', $value) : 0); + + return Money::{$currency}(!empty($value) ? str_replace(',', '', (string)$value) : 0); } /** @@ -74,7 +75,7 @@ public static function money($value, $fromDb = false) : ?Money */ public static function float(Money $money) : float { - return $money->getAmount() / 100; + return ((float)$money->getAmount()) / 100; } /** @@ -121,6 +122,7 @@ protected static function _loadMoneyFormatter(Currency $currency) : MoneyFormatt */ public static function zero() : Money { + /** @var Money */ return self::money(0); } diff --git a/src/View/Helper/MoneyHelper.php b/src/View/Helper/MoneyHelper.php index 7dbd8e7..861dc08 100644 --- a/src/View/Helper/MoneyHelper.php +++ b/src/View/Helper/MoneyHelper.php @@ -18,9 +18,14 @@ /** * Class MoneyHelper * @package CakeDC\Money\View\Helper + * @property \Cake\View\Helper\HtmlHelper $Html + * @property \Cake\View\Helper\NumberHelper $Number */ class MoneyHelper extends Helper { + /** + * @inheritDoc + */ protected $helpers = ['Html', 'Number']; /** @@ -36,7 +41,7 @@ public function initialize(array $config): void /** * Format number or money as currency. - * @param $value + * @param \CakeDC\Money\Money|float|string $value * @return string */ public function currency($value): string diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 7003609..0913431 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -32,7 +32,7 @@ * and define the data required by your plugin here. */ require_once $root . '/vendor/cakephp/cakephp/tests/bootstrap.php'; - +class_alias(\Cake\Controller\Controller::class, 'App\Controller\AppController'); if (file_exists($root . '/config/bootstrap.php')) { require $root . '/config/bootstrap.php'; From 70d4bf7529ae698ae464c9e2df19309088d1f34b Mon Sep 17 00:00:00 2001 From: Adam Rusinowski Date: Fri, 1 Mar 2024 11:17:40 +0100 Subject: [PATCH 08/11] cc --- .editorconfig | 26 +++++++++ .gitignore | 1 + composer.json | 9 ++- phpcs.xml | 6 ++ src/Database/Type/MoneyType.php | 22 +++----- src/Money.php | 24 ++++---- src/Utility/MoneyUtil.php | 56 +++++++++---------- src/View/Helper/MoneyHelper.php | 8 ++- src/View/Widget/MoneyWidget.php | 4 +- .../TestCase/Database/Type/MoneyTypeTest.php | 7 +-- tests/TestCase/Utility/MoneyUtilTest.php | 26 ++++----- .../TestCase/View/Helper/MoneyHelperTest.php | 6 +- .../TestCase/View/Widget/MoneyWidgetTest.php | 2 +- tests/bootstrap.php | 2 +- webroot/.gitkeep | 0 15 files changed, 113 insertions(+), 86 deletions(-) create mode 100644 .editorconfig create mode 100644 phpcs.xml delete mode 100644 webroot/.gitkeep diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..ae35877 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,26 @@ +; This file is for unifying the coding style for different editors and IDEs. +; More information at http://editorconfig.org + +root = true + +[*] +indent_style = space +indent_size = 4 +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true + +[*.bat] +end_of_line = crlf + +[*.yml] +indent_size = 2 + +[*.xml] +indent_size = 2 + +[Makefile] +indent_style = tab + +[*.neon] +indent_style = tab \ No newline at end of file diff --git a/.gitignore b/.gitignore index 56b0dbd..22e0459 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ /.idea/ /.phpunit.cache /.ddev/ +/tools/ diff --git a/composer.json b/composer.json index 4783206..a7c53bb 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,9 @@ "moneyphp/money": "^3.3" }, "require-dev": { - "phpunit/phpunit": "^8.5 || ^9.3" + "phpunit/phpunit": "^8.5 || ^9.3", + "cakephp/cakephp-codesniffer": "^4.5", + "ext-pdo": "*" }, "autoload": { "psr-4": { @@ -40,5 +42,10 @@ "psalm-baseline": "tools/psalm --set-baseline=psalm-baseline.xml", "stan-setup": "phive install", "test": "phpunit" + }, + "config": { + "allow-plugins": { + "dealerdirect/phpcodesniffer-composer-installer": false + } } } diff --git a/phpcs.xml b/phpcs.xml new file mode 100644 index 0000000..c1ac214 --- /dev/null +++ b/phpcs.xml @@ -0,0 +1,6 @@ + + + + + + diff --git a/src/Database/Type/MoneyType.php b/src/Database/Type/MoneyType.php index ca72e92..111ef54 100644 --- a/src/Database/Type/MoneyType.php +++ b/src/Database/Type/MoneyType.php @@ -11,15 +11,11 @@ namespace CakeDC\Money\Database\Type; use Cake\Core\Configure; - use Cake\Database\DriverInterface; use Cake\Database\Type\BaseType; use Cake\Database\TypeInterface; -use Cake\Error\Debugger; -use CakeDC\Money\Utility\MoneyUtil; use CakeDC\Money\Money; -use http\Exception\RuntimeException; -use Money\Money as MoneyPHP; +use CakeDC\Money\Utility\MoneyUtil; use PDO; /** @@ -29,15 +25,14 @@ */ class MoneyType extends BaseType implements TypeInterface { - /** * Casts given value from a database type to a PHP equivalent. * * @param mixed $value Value to be converted to PHP equivalent * @param \Cake\Database\DriverInterface $driver Object from which database preferences and configuration will be extracted - * @return ?Money Given value casted from a database to a PHP equivalent. + * @return ?\CakeDC\Money\Money Given value casted from a database to a PHP equivalent. */ - public function toPHP($value, DriverInterface $driver) : ?Money + public function toPHP($value, DriverInterface $driver): ?Money { if ($value === null) { return null; @@ -55,9 +50,9 @@ public function toPHP($value, DriverInterface $driver) : ?Money * that make sense for the rest of the ORM/Database layers. * * @param mixed $value The value to convert. - * @return ?Money Converted value. + * @return ?\CakeDC\Money\Money Converted value. */ - public function marshal($value) : ?Money + public function marshal($value): ?Money { if ($value === null) { return null; @@ -70,11 +65,11 @@ public function marshal($value) : ?Money /** * Casts given value from a PHP type to one acceptable by a database. * - * @param ?Money $value Value to be converted to a database equivalent. + * @param ?\CakeDC\Money\Money $value Value to be converted to a database equivalent. * @param \Cake\Database\DriverInterface $driver Object from which database preferences and configuration will be extracted. * @return ?string Given PHP type casted to one acceptable by a database. */ - public function toDatabase($value, DriverInterface $driver) : ?string + public function toDatabase($value, DriverInterface $driver): ?string { if ($value === null) { @@ -94,7 +89,7 @@ public function toDatabase($value, DriverInterface $driver) : ?string * @param \Cake\Database\DriverInterface $driver Object from which database preferences and configuration will be extracted. * @return int Given value casted to its Statement equivalent. */ - public function toStatement($value, DriverInterface $driver) : int + public function toStatement($value, DriverInterface $driver): int { if ($value === null) { return PDO::PARAM_NULL; @@ -102,5 +97,4 @@ public function toStatement($value, DriverInterface $driver) : int return PDO::PARAM_INT; } - } diff --git a/src/Money.php b/src/Money.php index a068dbe..7ff2a26 100644 --- a/src/Money.php +++ b/src/Money.php @@ -35,7 +35,7 @@ * @method \CakeDC\Money\Money mod(\CakeDC\Money\Money $divisor) See \Money\Money::mod() * @method \CakeDC\Money\Money[] allocate(array $ratios) See \Money\Money::allocate() * @method \CakeDC\Money\Money[] allocateTo($n) See \Money\Money::allocateTo() - * @method string ratioOf(Money $money) See \Money\Money::ratioOf() + * @method string ratioOf(\CakeDC\Money\Money $money) See \Money\Money::ratioOf() * @method \CakeDC\Money\Money absolute() See \Money\Money::absolute() * @method \CakeDC\Money\Money negative() See \Money\Money::negative() * @method bool isZero() See \Money\Money::isZero() @@ -52,29 +52,29 @@ */ class Money { - const ROUND_HALF_UP = PHP_ROUND_HALF_UP; + public const ROUND_HALF_UP = PHP_ROUND_HALF_UP; - const ROUND_HALF_DOWN = PHP_ROUND_HALF_DOWN; + public const ROUND_HALF_DOWN = PHP_ROUND_HALF_DOWN; - const ROUND_HALF_EVEN = PHP_ROUND_HALF_EVEN; + public const ROUND_HALF_EVEN = PHP_ROUND_HALF_EVEN; - const ROUND_HALF_ODD = PHP_ROUND_HALF_ODD; + public const ROUND_HALF_ODD = PHP_ROUND_HALF_ODD; - const ROUND_UP = 5; + public const ROUND_UP = 5; - const ROUND_DOWN = 6; + public const ROUND_DOWN = 6; - const ROUND_HALF_POSITIVE_INFINITY = 7; + public const ROUND_HALF_POSITIVE_INFINITY = 7; - const ROUND_HALF_NEGATIVE_INFINITY = 8; + public const ROUND_HALF_NEGATIVE_INFINITY = 8; /** - * @var MoneyPHP + * @var \Money\Money */ protected $_money; /** - * @return MoneyPHP + * @return \Money\Money */ public function getMoney(): MoneyPHP { @@ -130,7 +130,7 @@ public function __toString(): string */ protected static function processArguments($arguments = []) { - for ($i=0; $i < count($arguments); $i++) { + for ($i = 0; $i < count($arguments); $i++) { if ($arguments[$i] instanceof Money) { $arguments[$i] = $arguments[$i]->getMoney(); } diff --git a/src/Utility/MoneyUtil.php b/src/Utility/MoneyUtil.php index e7182d2..797acee 100644 --- a/src/Utility/MoneyUtil.php +++ b/src/Utility/MoneyUtil.php @@ -11,12 +11,12 @@ namespace CakeDC\Money\Utility; use Cake\Core\Configure; +use CakeDC\Money\Money; use Money\Currencies\BitcoinCurrencies; use Money\Currencies\ISOCurrencies; use Money\Currency; use Money\Formatter\BitcoinMoneyFormatter; use Money\Formatter\IntlMoneyFormatter; -use CakeDC\Money\Money; use Money\MoneyFormatter; /** @@ -37,10 +37,10 @@ class MoneyUtil * Returns a new object of type Money * * @param \CakeDC\Money\Money|int|float|string $value - * @param boolean $fromDb - * @return Money + * @param bool $fromDb + * @return \CakeDC\Money\Money */ - public static function money($value, bool $fromDb = false) : ?Money + public static function money($value, bool $fromDb = false): ?Money { if (!is_numeric($value) && empty($value)) { return null; @@ -63,28 +63,28 @@ public static function money($value, bool $fromDb = false) : ?Money $value = ltrim($parts[0] . $parts[1], '0'); } - + $currency = Configure::read('Money.currency', 'USD'); return Money::{$currency}(!empty($value) ? str_replace(',', '', (string)$value) : 0); } /** - * @param Money $money + * @param \CakeDC\Money\Money $money * @return float */ - public static function float(Money $money) : float + public static function float(Money $money): float { - return ((float)$money->getAmount()) / 100; + return (float)$money->getAmount() / 100; } /** - * @param Money $value + * @param \CakeDC\Money\Money $value * @return string */ - public static function format(Money $value) : string + public static function format(Money $value): string { - /** @var Currency $currency */ + /** @var \Money\Currency $currency */ $currency = $value->getCurrency(); return static::_loadMoneyFormatter($currency)->format($value->getMoney()); @@ -93,10 +93,10 @@ public static function format(Money $value) : string /** * Loads proper money formatter or returns if it is already loaded * - * @param Currency $currency - * @return MoneyFormatter + * @param \Money\Currency $currency + * @return \Money\MoneyFormatter */ - protected static function _loadMoneyFormatter(Currency $currency) : MoneyFormatter + protected static function _loadMoneyFormatter(Currency $currency): MoneyFormatter { if (isset(static::$_moneyFormatters[$currency->getCode()])) { return static::$_moneyFormatters[$currency->getCode()]; @@ -112,27 +112,28 @@ protected static function _loadMoneyFormatter(Currency $currency) : MoneyFormatt throw new \RuntimeException(sprintf('Cannot format currency \'%s\'. Only ISO currencies and Bitcoin are allowed.', $currency)); } static::$_moneyFormatters[$currency->getCode()] = $moneyFormatter; + return static::$_moneyFormatters[$currency->getCode()]; } /** * Returns money object with value 0.00, false otherwise. * - * @return Money + * @return \CakeDC\Money\Money */ - public static function zero() : Money + public static function zero(): Money { - /** @var Money */ + /** @var \CakeDC\Money\Money */ return self::money(0); } /** * Returns true if amount value is > 0.00 * - * @param Money $amount + * @param \CakeDC\Money\Money $amount * @return bool */ - public static function greaterThanZero(Money $amount) : bool + public static function greaterThanZero(Money $amount): bool { return $amount->greaterThan(self::zero()); } @@ -140,10 +141,10 @@ public static function greaterThanZero(Money $amount) : bool /** * Returns true if amount value is >= 0.00, false otherwise. * - * @param Money $amount + * @param \CakeDC\Money\Money $amount * @return bool */ - public static function greaterThanOrEqualZero(Money $amount) : bool + public static function greaterThanOrEqualZero(Money $amount): bool { return $amount->greaterThanOrEqual(self::zero()); } @@ -151,10 +152,10 @@ public static function greaterThanOrEqualZero(Money $amount) : bool /** * Returns true if amount value is < 0.00, false otherwise. * - * @param Money $amount + * @param \CakeDC\Money\Money $amount * @return bool */ - public static function lessThanZero(Money $amount) : bool + public static function lessThanZero(Money $amount): bool { return $amount->lessThan(self::zero()); } @@ -162,10 +163,10 @@ public static function lessThanZero(Money $amount) : bool /** * Returns true if amount value is <= 0.00, false otherwise. * - * @param Money $amount + * @param \CakeDC\Money\Money $amount * @return bool */ - public static function lessThanOrEqualZero(Money $amount) : bool + public static function lessThanOrEqualZero(Money $amount): bool { return $amount->lessThanOrEqual(self::zero()); } @@ -173,12 +174,11 @@ public static function lessThanOrEqualZero(Money $amount) : bool /** * Returns true if amount value is = 0.00, false otherwise. * - * @param Money $amount + * @param \CakeDC\Money\Money $amount * @return bool */ - public static function equalZero(Money $amount) : bool + public static function equalZero(Money $amount): bool { return $amount->equals(self::zero()); } - } diff --git a/src/View/Helper/MoneyHelper.php b/src/View/Helper/MoneyHelper.php index 861dc08..abc143e 100644 --- a/src/View/Helper/MoneyHelper.php +++ b/src/View/Helper/MoneyHelper.php @@ -10,13 +10,13 @@ */ namespace CakeDC\Money\View\Helper; - use Cake\View\Helper; -use CakeDC\Money\Utility\MoneyUtil; use CakeDC\Money\Money; +use CakeDC\Money\Utility\MoneyUtil; /** * Class MoneyHelper + * * @package CakeDC\Money\View\Helper * @property \Cake\View\Helper\HtmlHelper $Html * @property \Cake\View\Helper\NumberHelper $Number @@ -41,6 +41,7 @@ public function initialize(array $config): void /** * Format number or money as currency. + * * @param \CakeDC\Money\Money|float|string $value * @return string */ @@ -52,7 +53,8 @@ public function currency($value): string } else { $output = $this->Number->currency($value); } - if ((is_numeric($value) && $value < 0) || + if ( + (is_numeric($value) && $value < 0) || ($value instanceof Money && MoneyUtil::lessThanZero($value)) ) { $class = 'negative-balance'; diff --git a/src/View/Widget/MoneyWidget.php b/src/View/Widget/MoneyWidget.php index f0f29ce..ba7cdab 100644 --- a/src/View/Widget/MoneyWidget.php +++ b/src/View/Widget/MoneyWidget.php @@ -12,11 +12,12 @@ use Cake\View\Form\ContextInterface; use Cake\View\Widget\WidgetInterface; -use CakeDC\Money\Utility\MoneyUtil; use CakeDC\Money\Money; +use CakeDC\Money\Utility\MoneyUtil; /** * Class MoneyWidget + * * @package CakeDC\Money\View\Widget */ class MoneyWidget implements WidgetInterface @@ -59,6 +60,7 @@ public function render(array $data, ContextInterface $context): string } $data['class'] = ($data['class'] ?? '') . ' form-control'; $data['step'] = '.01'; + return $this->_templates->format('input', [ 'name' => $data['name'], 'type' => 'number', diff --git a/tests/TestCase/Database/Type/MoneyTypeTest.php b/tests/TestCase/Database/Type/MoneyTypeTest.php index 43b2053..13e6973 100644 --- a/tests/TestCase/Database/Type/MoneyTypeTest.php +++ b/tests/TestCase/Database/Type/MoneyTypeTest.php @@ -3,13 +3,10 @@ namespace CakeDC\Money\Test\TestCase\Utility; -use Cake\Core\Configure; use Cake\TestSuite\TestCase; use CakeDC\Money\Database\Type\MoneyType; use CakeDC\Money\Utility\MoneyUtil; -use Exception; use PDO; -use RuntimeException; /** * CakeDC\Money\Database\Type\MoneyTypeTest Test Case @@ -67,6 +64,4 @@ public function testToStatement() $this->assertEquals(PDO::PARAM_NULL, $this->moneyType->toStatement(null, $this->driver)); $this->assertEquals(PDO::PARAM_INT, $this->moneyType->toStatement(100, $this->driver)); } - - -} \ No newline at end of file +} diff --git a/tests/TestCase/Utility/MoneyUtilTest.php b/tests/TestCase/Utility/MoneyUtilTest.php index dcf7efb..bab03aa 100644 --- a/tests/TestCase/Utility/MoneyUtilTest.php +++ b/tests/TestCase/Utility/MoneyUtilTest.php @@ -6,7 +6,6 @@ use Cake\Core\Configure; use Cake\TestSuite\TestCase; use CakeDC\Money\Utility\MoneyUtil; -use Exception; use RuntimeException; /** @@ -41,7 +40,6 @@ public function tearDown(): void parent::tearDown(); } - public function testMoneyNumericValue(): void { $this->assertInstanceOf( @@ -120,7 +118,6 @@ public function testMoneyClassValue(): void ); } - public function testFloat(): void { $money = MoneyUtil::money(100.15); @@ -134,7 +131,7 @@ public function testFormat() { $money = MoneyUtil::money(100.15); $this->assertEquals('$100.15', MoneyUtil::format($money)); - + $money = MoneyUtil::money(200); $this->assertEquals('$200.00', MoneyUtil::format($money)); } @@ -142,14 +139,14 @@ public function testFormat() public function testFormatCurrencies() { Configure::write('Money.currency', 'EUR'); - + $money = MoneyUtil::money(100.15); $this->assertEquals('€100.15', MoneyUtil::format($money)); - + $money = MoneyUtil::money('200'); $this->assertEquals('€200.00', MoneyUtil::format($money)); } - + public function testFormatBitcoin() { Configure::write('Money.currency', 'XBT'); @@ -158,14 +155,14 @@ public function testFormatBitcoin() $this->assertInstanceOf(\CakeDC\Money\Money::class, $money); $this->assertEquals('Ƀ0.0000010', MoneyUtil::format($money)); } - + public function testFormatOther() { - try{ + try { Configure::write('Money.currency', 'NotCurrency'); $money = MoneyUtil::money(100.15); MoneyUtil::format($money); - } catch (\Exception $e){ + } catch (\Exception $e) { $this->assertInstanceOf(RuntimeException::class, $e); } } @@ -182,7 +179,6 @@ public function testZero() $this->assertEquals('$0.00', $money->__toString()); } - public function testGreaterThanZero() { $money = MoneyUtil::money(100); @@ -203,7 +199,6 @@ public function testGreaterThanOrEqualZero() $this->assertTrue(MoneyUtil::greaterThanOrEqualZero(MoneyUtil::zero())); } - public function testLessThanZero() { $money = MoneyUtil::money(100); @@ -212,7 +207,7 @@ public function testLessThanZero() $money = MoneyUtil::money(-100); $this->assertTrue(MoneyUtil::lessThanZero($money)); } - + public function testLessThanOrEqualZero() { $money = MoneyUtil::money(100); @@ -228,8 +223,7 @@ public function testEqualZero() { $money = MoneyUtil::money(100); $this->assertFalse(MoneyUtil::equalZero($money)); - + $this->assertTrue(MoneyUtil::equalZero(MoneyUtil::zero())); } - -} \ No newline at end of file +} diff --git a/tests/TestCase/View/Helper/MoneyHelperTest.php b/tests/TestCase/View/Helper/MoneyHelperTest.php index fdcb0d9..92df567 100644 --- a/tests/TestCase/View/Helper/MoneyHelperTest.php +++ b/tests/TestCase/View/Helper/MoneyHelperTest.php @@ -3,10 +3,10 @@ namespace CakeDC\Money\Test\TestCase\View\Helper; -use CakeDC\Money\View\Helper\MoneyHelper; use Cake\TestSuite\TestCase; use Cake\View\View; use CakeDC\Money\Utility\MoneyUtil; +use CakeDC\Money\View\Helper\MoneyHelper; /** * CakeDC\Money\View\Helper\MoneyHelper Test Case @@ -56,7 +56,7 @@ public function testCurrency(): void ); $this->assertEquals( - '$100.00', + '$100.00', $this->MoneyHelper->currency('100') ); @@ -67,7 +67,7 @@ public function testCurrency(): void $this->assertEquals( '-$100.00', - $this->MoneyHelper->currency("-100") + $this->MoneyHelper->currency('-100') ); $this->assertEquals( diff --git a/tests/TestCase/View/Widget/MoneyWidgetTest.php b/tests/TestCase/View/Widget/MoneyWidgetTest.php index 7df1411..ecd3fc1 100644 --- a/tests/TestCase/View/Widget/MoneyWidgetTest.php +++ b/tests/TestCase/View/Widget/MoneyWidgetTest.php @@ -115,4 +115,4 @@ public function testSecureFields(): void $this->assertTrue(in_array('amount', $money->secureFields($data))); } -} \ No newline at end of file +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 0913431..9d5a171 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -17,7 +17,7 @@ } } while ($root !== $lastRoot); - throw new Exception("Cannot find the root of the application, unable to run tests"); + throw new Exception('Cannot find the root of the application, unable to run tests'); }; $root = $findRoot(__FILE__); unset($findRoot); diff --git a/webroot/.gitkeep b/webroot/.gitkeep deleted file mode 100644 index e69de29..0000000 From d22ecb4c40e3a34ffeeea4d3695afdd28aa673e2 Mon Sep 17 00:00:00 2001 From: Adam Rusinowski Date: Fri, 1 Mar 2024 11:33:22 +0100 Subject: [PATCH 09/11] cc --- .gitignore | 48 ++++++++++++++++++++++++++------ CHANGELOG.md | 2 +- composer.json | 6 ++-- src/Controller/AppController.php | 4 +-- src/Database/Type/MoneyType.php | 14 ++++++---- src/Money.php | 2 ++ src/Plugin.php | 4 +-- src/Utility/MoneyUtil.php | 2 ++ src/View/Helper/MoneyHelper.php | 2 ++ src/View/Widget/MoneyWidget.php | 2 ++ 10 files changed, 64 insertions(+), 22 deletions(-) diff --git a/.gitignore b/.gitignore index 22e0459..85db205 100644 --- a/.gitignore +++ b/.gitignore @@ -1,11 +1,41 @@ +/tmp +/vendor +/.idea +composer.lock +.php_cs* +/coverage +/config/Migrations/schema-dump-default.lock +*.pyc +docs/_build +tmp /composer.lock -/composer.phar -/phpunit.xml +*.diff +*.err +*.log +*.orig +*.rej +*.swo +*.swp +*.vi +*~ +.idea/* +nbproject/* +.vscode +.DS_Store +.cache +.phpunit.cache +.project +.settings +.svn +errors.err +tags +node_modules +package-lock.json /.phpunit.result.cache -/phpunit.phar -/config/Migrations/schema-dump-default.lock -/vendor/ -/.idea/ -/.phpunit.cache -/.ddev/ -/tools/ +/nbproject/ +/tools +/phpunit.xml +/webroot/css/style.css.map +/webroot/mix.js.map +/webroot/mix-manifest.json +.ddev/* diff --git a/CHANGELOG.md b/CHANGELOG.md index 63cedae..dedd607 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ## [2.0.1] - 2024-02-23 -- Ported changes applied in tag `1.0.1` +- Ported changes applied in tag `1.0.1` ## [1.0.1] - 2024-02-23 diff --git a/composer.json b/composer.json index 6234aba..2d22ab4 100644 --- a/composer.json +++ b/composer.json @@ -6,10 +6,12 @@ "require": { "php": ">=8.1", "cakephp/cakephp": "^5.0", - "moneyphp/money": "^4.0" + "moneyphp/money": "^4.0", + "ext-pdo": "*" }, "require-dev": { - "phpunit/phpunit": "^10.1.0" + "phpunit/phpunit": "^10.1.0", + "cakephp/cakephp-codesniffer": "^5.1" }, "autoload": { "psr-4": { diff --git a/src/Controller/AppController.php b/src/Controller/AppController.php index 834fd84..0043b60 100644 --- a/src/Controller/AppController.php +++ b/src/Controller/AppController.php @@ -1,4 +1,6 @@ Date: Fri, 1 Mar 2024 11:43:36 +0100 Subject: [PATCH 10/11] cc --- src/Database/Type/MoneyType.php | 12 ++++---- src/Money.php | 20 ++++++++----- src/Plugin.php | 2 +- src/Utility/MoneyUtil.php | 19 ++++++++---- src/View/Helper/MoneyHelper.php | 4 +-- src/View/Widget/MoneyWidget.php | 5 ++-- .../TestCase/Database/Type/MoneyTypeTest.php | 8 +++-- tests/TestCase/Utility/MoneyUtilTest.php | 30 ++++++++++--------- tests/bootstrap.php | 4 ++- 9 files changed, 62 insertions(+), 42 deletions(-) diff --git a/src/Database/Type/MoneyType.php b/src/Database/Type/MoneyType.php index d80c1f2..57c49e2 100644 --- a/src/Database/Type/MoneyType.php +++ b/src/Database/Type/MoneyType.php @@ -16,8 +16,9 @@ use Cake\Database\Driver; use Cake\Database\Type\BaseType; use Cake\Database\TypeInterface; -use CakeDC\Money\Utility\MoneyUtil; use CakeDC\Money\Money; +use CakeDC\Money\Utility\MoneyUtil; +use InvalidArgumentException; use PDO; /** @@ -34,7 +35,7 @@ class MoneyType extends BaseType implements TypeInterface * @param \Cake\Database\Driver $driver Object from which database preferences and configuration will be extracted * @return ?\CakeDC\Money\Money Given value casted from a database to a PHP equivalent. */ - public function toPHP(mixed $value, Driver $driver) : ?Money + public function toPHP(mixed $value, Driver $driver): ?Money { if ($value === null) { return null; @@ -59,7 +60,6 @@ public function marshal(mixed $value): ?Money if ($value === null) { return null; } - $currency = Configure::read('Money.currency', 'USD'); return MoneyUtil::money($value); } @@ -71,13 +71,13 @@ public function marshal(mixed $value): ?Money * @param \Cake\Database\Driver $driver Object from which database preferences and configuration will be extracted. * @return ?string Given PHP type casted to one acceptable by a database. */ - public function toDatabase(mixed $value, Driver $driver) : ?string + public function toDatabase(mixed $value, Driver $driver): ?string { if ($value === null) { return null; } if (!($value instanceof Money)) { - throw new \InvalidArgumentException(__('Value must be instance of Money')); + throw new InvalidArgumentException(__('Value must be instance of Money')); } return $value->getAmount(); @@ -90,7 +90,7 @@ public function toDatabase(mixed $value, Driver $driver) : ?string * @param \Cake\Database\Driver $driver Object from which database preferences and configuration will be extracted. * @return int Given value casted to its Statement equivalent. */ - public function toStatement(mixed $value, Driver $driver) : int + public function toStatement(mixed $value, Driver $driver): int { if ($value === null) { return PDO::PARAM_NULL; diff --git a/src/Money.php b/src/Money.php index 45a47ef..ed7857f 100644 --- a/src/Money.php +++ b/src/Money.php @@ -73,7 +73,7 @@ class Money /** * @var \Money\Money */ - protected $_money; + protected MoneyPHP $_money; /** * @return \Money\Money @@ -83,6 +83,11 @@ public function getMoney(): MoneyPHP return $this->_money; } + /** + * Constructor + * + * @param \Money\Money $money + */ public function __construct(MoneyPHP $money) { $this->_money = $money; @@ -91,9 +96,9 @@ public function __construct(MoneyPHP $money) /** * @param string $name * @param array $arguments - * @return false|mixed + * @return mixed|false */ - public function __call(string $name, array $arguments) + public function __call(string $name, array $arguments): mixed { $arguments = self::processArguments($arguments); // @phpstan-ignore-next-line @@ -108,9 +113,9 @@ public function __call(string $name, array $arguments) /** * @param string $name * @param array $arguments - * @return false|mixed + * @return mixed|false */ - public static function __callStatic(string $name, array $arguments) + public static function __callStatic(string $name, array $arguments): mixed { $arguments = self::processArguments($arguments); @@ -130,9 +135,10 @@ public function __toString(): string * @param array $arguments * @return array */ - protected static function processArguments($arguments = []) + protected static function processArguments(array $arguments = []): array { - for ($i = 0; $i < count($arguments); $i++) { + $count = count($arguments); + for ($i = 0; $i < $count; $i++) { if ($arguments[$i] instanceof Money) { $arguments[$i] = $arguments[$i]->getMoney(); } diff --git a/src/Plugin.php b/src/Plugin.php index e2f20b7..496aefe 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -52,7 +52,7 @@ public function routes(RouteBuilder $routes): void $routes->plugin( 'CakeDC/Money', ['path' => '/money'], - function (RouteBuilder $builder) { + function (RouteBuilder $builder): void { // Add custom routes here $builder->fallbacks(); diff --git a/src/Utility/MoneyUtil.php b/src/Utility/MoneyUtil.php index dd61ce0..8008721 100644 --- a/src/Utility/MoneyUtil.php +++ b/src/Utility/MoneyUtil.php @@ -20,6 +20,8 @@ use Money\Formatter\BitcoinMoneyFormatter; use Money\Formatter\IntlMoneyFormatter; use Money\MoneyFormatter; +use NumberFormatter; +use RuntimeException; /** * Class MoneyUtil @@ -31,18 +33,18 @@ class MoneyUtil { /** - * @var \Money\MoneyFormatter[] + * @var array<\Money\MoneyFormatter> */ - protected static $_moneyFormatters = []; + protected static array $_moneyFormatters = []; /** * Returns a new object of type Money * - * @param \CakeDC\Money\Money|int|float|string $value + * @param \CakeDC\Money\Money|string|float|int $value * @param bool $fromDb * @return \CakeDC\Money\Money */ - public static function money($value, bool $fromDb = false): ?Money + public static function money(Money|int|float|string $value, bool $fromDb = false): ?Money { if (!is_numeric($value) && empty($value)) { return null; @@ -106,12 +108,17 @@ protected static function _loadMoneyFormatter(Currency $currency): MoneyFormatte $iso = new ISOCurrencies(); $bitcoin = new BitcoinCurrencies(); if ($iso->contains($currency)) { - $numberFormatter = new \NumberFormatter('en_US', \NumberFormatter::CURRENCY); + $numberFormatter = new NumberFormatter('en_US', NumberFormatter::CURRENCY); $moneyFormatter = new IntlMoneyFormatter($numberFormatter, $iso); } elseif ($bitcoin->contains($currency)) { $moneyFormatter = new BitcoinMoneyFormatter(7, $bitcoin); } else { - throw new \RuntimeException(sprintf('Cannot format currency \'%s\'. Only ISO currencies and Bitcoin are allowed.', $currency)); + throw new RuntimeException( + sprintf( + 'Cannot format currency \'%s\'. Only ISO currencies and Bitcoin are allowed.', + $currency + ) + ); } static::$_moneyFormatters[$currency->getCode()] = $moneyFormatter; diff --git a/src/View/Helper/MoneyHelper.php b/src/View/Helper/MoneyHelper.php index 5c5bb39..706bde7 100644 --- a/src/View/Helper/MoneyHelper.php +++ b/src/View/Helper/MoneyHelper.php @@ -44,10 +44,10 @@ public function initialize(array $config): void /** * Format number or money as currency. * - * @param \CakeDC\Money\Money|float|string $value + * @param \CakeDC\Money\Money|string|float $value * @return string */ - public function currency($value): string + public function currency(Money|float|string $value): string { $class = ''; if ($value instanceof Money) { diff --git a/src/View/Widget/MoneyWidget.php b/src/View/Widget/MoneyWidget.php index 7f348c5..5e9fda9 100644 --- a/src/View/Widget/MoneyWidget.php +++ b/src/View/Widget/MoneyWidget.php @@ -13,6 +13,7 @@ namespace CakeDC\Money\View\Widget; use Cake\View\Form\ContextInterface; +use Cake\View\StringTemplate; use Cake\View\Widget\WidgetInterface; use CakeDC\Money\Money; use CakeDC\Money\Utility\MoneyUtil; @@ -29,14 +30,14 @@ class MoneyWidget implements WidgetInterface * * @var \Cake\View\StringTemplate */ - protected $_templates; + protected StringTemplate $_templates; /** * Constructor. * * @param \Cake\View\StringTemplate $templates Templates list. */ - public function __construct($templates) + public function __construct(StringTemplate $templates) { $this->_templates = $templates; } diff --git a/tests/TestCase/Database/Type/MoneyTypeTest.php b/tests/TestCase/Database/Type/MoneyTypeTest.php index 13e6973..fc1a5ed 100644 --- a/tests/TestCase/Database/Type/MoneyTypeTest.php +++ b/tests/TestCase/Database/Type/MoneyTypeTest.php @@ -5,7 +5,9 @@ use Cake\TestSuite\TestCase; use CakeDC\Money\Database\Type\MoneyType; +use CakeDC\Money\Money; use CakeDC\Money\Utility\MoneyUtil; +use InvalidArgumentException; use PDO; /** @@ -38,13 +40,13 @@ public function tearDown(): void public function testToPhp() { $this->assertNull($this->moneyType->toPHP(null, $this->driver)); - $this->assertInstanceOf(\CakeDC\Money\Money::class, $this->moneyType->toPHP(100, $this->driver)); + $this->assertInstanceOf(Money::class, $this->moneyType->toPHP(100, $this->driver)); } public function testMarshal() { $this->assertNull($this->moneyType->marshal(null)); - $this->assertInstanceOf(\CakeDC\Money\Money::class, $this->moneyType->marshal(100)); + $this->assertInstanceOf(Money::class, $this->moneyType->marshal(100)); } public function testToDatabase() @@ -55,7 +57,7 @@ public function testToDatabase() public function testToDatabaseInvalidArgument() { - $this->expectException(\InvalidArgumentException::class); + $this->expectException(InvalidArgumentException::class); $this->moneyType->toDatabase(100, $this->driver); } diff --git a/tests/TestCase/Utility/MoneyUtilTest.php b/tests/TestCase/Utility/MoneyUtilTest.php index bab03aa..46d71b8 100644 --- a/tests/TestCase/Utility/MoneyUtilTest.php +++ b/tests/TestCase/Utility/MoneyUtilTest.php @@ -5,7 +5,9 @@ use Cake\Core\Configure; use Cake\TestSuite\TestCase; +use CakeDC\Money\Money; use CakeDC\Money\Utility\MoneyUtil; +use Exception; use RuntimeException; /** @@ -43,27 +45,27 @@ public function tearDown(): void public function testMoneyNumericValue(): void { $this->assertInstanceOf( - \CakeDC\Money\Money::class, + Money::class, MoneyUtil::money(100) ); $this->assertInstanceOf( - \CakeDC\Money\Money::class, + Money::class, MoneyUtil::money(100, true) ); $this->assertInstanceOf( - \CakeDC\Money\Money::class, + Money::class, MoneyUtil::money(-100) ); $this->assertInstanceOf( - \CakeDC\Money\Money::class, + Money::class, MoneyUtil::money(100.15) ); $this->assertInstanceOf( - \CakeDC\Money\Money::class, + Money::class, MoneyUtil::money(100.1) ); } @@ -71,22 +73,22 @@ public function testMoneyNumericValue(): void public function testMoneyStringValue(): void { $this->assertInstanceOf( - \CakeDC\Money\Money::class, + Money::class, MoneyUtil::money('100') ); $this->assertInstanceOf( - \CakeDC\Money\Money::class, + Money::class, MoneyUtil::money('100', true) ); $this->assertInstanceOf( - \CakeDC\Money\Money::class, + Money::class, MoneyUtil::money('-100') ); $this->assertInstanceOf( - \CakeDC\Money\Money::class, + Money::class, MoneyUtil::money('100.15') ); } @@ -98,7 +100,7 @@ public function testMoneyNullValue(): void ); $this->assertInstanceOf( - \CakeDC\Money\Money::class, + Money::class, MoneyUtil::money(0) ); } @@ -108,7 +110,7 @@ public function testMoneyClassValue(): void $money = MoneyUtil::money(10); $this->assertInstanceOf( - \CakeDC\Money\Money::class, + Money::class, MoneyUtil::money($money) ); @@ -152,7 +154,7 @@ public function testFormatBitcoin() Configure::write('Money.currency', 'XBT'); $money = MoneyUtil::money(1); - $this->assertInstanceOf(\CakeDC\Money\Money::class, $money); + $this->assertInstanceOf(Money::class, $money); $this->assertEquals('Ƀ0.0000010', MoneyUtil::format($money)); } @@ -162,7 +164,7 @@ public function testFormatOther() Configure::write('Money.currency', 'NotCurrency'); $money = MoneyUtil::money(100.15); MoneyUtil::format($money); - } catch (\Exception $e) { + } catch (Exception $e) { $this->assertInstanceOf(RuntimeException::class, $e); } } @@ -172,7 +174,7 @@ public function testZero() $money = MoneyUtil::zero(); $this->assertInstanceOf( - \CakeDC\Money\Money::class, + Money::class, $money ); diff --git a/tests/bootstrap.php b/tests/bootstrap.php index c6ff548..3711278 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,6 +1,8 @@ Date: Fri, 1 Mar 2024 11:49:36 +0100 Subject: [PATCH 11/11] cc --- CHANGELOG.md | 4 ++++ README.md | 10 +++++----- composer.json | 3 ++- src/Utility/MoneyUtil.php | 14 +++++++------- src/View/Widget/MoneyWidget.php | 8 ++++---- 5 files changed, 22 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dedd607..459e015 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [2.0.2] 2024-03-01 +- improved conde completion in `\CakeDC\Money\Money` class +- fixed issues reported by stan tools + ## [2.0.1] - 2024-02-23 - Ported changes applied in tag `1.0.1` diff --git a/README.md b/README.md index a76268e..43ef560 100644 --- a/README.md +++ b/README.md @@ -29,11 +29,11 @@ It covers the following features: ## Versions and branches -| CakePHP | CakeDC Money Plugin | Notes | -| :-------------: | :------------------------: | :---- | -| ^5.0 | [2.x](https://github.com/cakedc/money/tree/2.next-cake5) | stable | -| ^4.5 | [1.x](https://github.com/cakedc/money/tree/1.next-cake4) | stable | -| ^4.0 | [0.0.1](https://github.com/cakedc/money/tree/0.0.1) | deprecated | +| CakePHP | CakeDC Money Plugin | Notes | +|:-------:|:--------------------------------------------------------:|:-----------| +| ^5.0 | [2.x](https://github.com/cakedc/money/tree/2.next-cake5) | stable | +| ^4.5 | [1.x](https://github.com/cakedc/money/tree/1.next-cake4) | stable | +| ^4.0 | [0.0.1](https://github.com/cakedc/money/tree/0.0.1) | deprecated | ## Installation diff --git a/composer.json b/composer.json index 2d22ab4..54e0c9c 100644 --- a/composer.json +++ b/composer.json @@ -7,7 +7,8 @@ "php": ">=8.1", "cakephp/cakephp": "^5.0", "moneyphp/money": "^4.0", - "ext-pdo": "*" + "ext-pdo": "*", + "ext-intl": "*" }, "require-dev": { "phpunit/phpunit": "^10.1.0", diff --git a/src/Utility/MoneyUtil.php b/src/Utility/MoneyUtil.php index 8008721..eca7137 100644 --- a/src/Utility/MoneyUtil.php +++ b/src/Utility/MoneyUtil.php @@ -35,7 +35,7 @@ class MoneyUtil /** * @var array<\Money\MoneyFormatter> */ - protected static array $_moneyFormatters = []; + protected static array $moneyFormatters = []; /** * Returns a new object of type Money @@ -91,7 +91,7 @@ public static function format(Money $value): string /** @var \Money\Currency $currency */ $currency = $value->getCurrency(); - return static::_loadMoneyFormatter($currency)->format($value->getMoney()); + return static::loadMoneyFormatter($currency)->format($value->getMoney()); } /** @@ -100,10 +100,10 @@ public static function format(Money $value): string * @param \Money\Currency $currency * @return \Money\MoneyFormatter */ - protected static function _loadMoneyFormatter(Currency $currency): MoneyFormatter + protected static function loadMoneyFormatter(Currency $currency): MoneyFormatter { - if (isset(static::$_moneyFormatters[$currency->getCode()])) { - return static::$_moneyFormatters[$currency->getCode()]; + if (isset(static::$moneyFormatters[$currency->getCode()])) { + return static::$moneyFormatters[$currency->getCode()]; } $iso = new ISOCurrencies(); $bitcoin = new BitcoinCurrencies(); @@ -120,9 +120,9 @@ protected static function _loadMoneyFormatter(Currency $currency): MoneyFormatte ) ); } - static::$_moneyFormatters[$currency->getCode()] = $moneyFormatter; + static::$moneyFormatters[$currency->getCode()] = $moneyFormatter; - return static::$_moneyFormatters[$currency->getCode()]; + return static::$moneyFormatters[$currency->getCode()]; } /** diff --git a/src/View/Widget/MoneyWidget.php b/src/View/Widget/MoneyWidget.php index 5e9fda9..033abb5 100644 --- a/src/View/Widget/MoneyWidget.php +++ b/src/View/Widget/MoneyWidget.php @@ -30,7 +30,7 @@ class MoneyWidget implements WidgetInterface * * @var \Cake\View\StringTemplate */ - protected StringTemplate $_templates; + protected StringTemplate $templates; /** * Constructor. @@ -39,7 +39,7 @@ class MoneyWidget implements WidgetInterface */ public function __construct(StringTemplate $templates) { - $this->_templates = $templates; + $this->templates = $templates; } /** @@ -64,11 +64,11 @@ public function render(array $data, ContextInterface $context): string $data['class'] = ($data['class'] ?? '') . ' form-control'; $data['step'] = '.01'; - return $this->_templates->format('input', [ + return $this->templates->format('input', [ 'name' => $data['name'], 'type' => 'number', 'templateVars' => $data['templateVars'], - 'attrs' => $this->_templates->formatAttributes( + 'attrs' => $this->templates->formatAttributes( $data, ['name', 'type'] ),