From 70d4bf7529ae698ae464c9e2df19309088d1f34b Mon Sep 17 00:00:00 2001 From: Adam Rusinowski Date: Fri, 1 Mar 2024 11:17:40 +0100 Subject: [PATCH] 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