Skip to content

Commit

Permalink
Merge pull request #6 from CakeDC/feature/add-phpdocs
Browse files Browse the repository at this point in the history
Feature/add phpdocs
  • Loading branch information
ajibarra authored Mar 1, 2024
2 parents 6c415bf + 70d4bf7 commit 6c239c4
Show file tree
Hide file tree
Showing 20 changed files with 168 additions and 120 deletions.
26 changes: 26 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
/.idea/
/.phpunit.cache
/.ddev/
/tools/
5 changes: 5 additions & 0 deletions .phive/phars.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<phive xmlns="https://phar.io/phive">
<phar name="phpstan" version="1.10.32" installed="1.10.32" location="./tools/phpstan" copy="false"/>
<phar name="psalm" version="5.15.0" installed="5.15.0" location="./tools/psalm" copy="false"/>
</phive>
33 changes: 21 additions & 12 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -24,19 +26,26 @@
},
"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"
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": false
}
}
}
6 changes: 6 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0"?>
<ruleset name="CakeDC/Money">
<config name="installed_paths" value="../../cakephp/cakephp-codesniffer" />

<rule ref="CakePHP" />
</ruleset>
2 changes: 2 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
parameters:
ignoreErrors:
15 changes: 9 additions & 6 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -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/
3 changes: 2 additions & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
>
<projectFiles>
<directory name="src" />
Expand Down Expand Up @@ -42,7 +43,7 @@

<MoreSpecificReturnType errorLevel="info" />
<LessSpecificReturnStatement errorLevel="info" />
<TypeCoercion errorLevel="info" />
<!-- <TypeCoercion errorLevel="info" />-->

<PossiblyInvalidArrayAccess errorLevel="info" />
<PossiblyInvalidArrayOffset errorLevel="info" />
Expand Down
22 changes: 8 additions & 14 deletions src/Database/Type/MoneyType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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) {
Expand All @@ -94,13 +89,12 @@ 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;
}

return PDO::PARAM_INT;
}

}
39 changes: 20 additions & 19 deletions src/Money.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
{
Expand All @@ -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);
Expand All @@ -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));
}

Expand All @@ -129,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();
}
Expand Down
6 changes: 3 additions & 3 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -35,7 +35,7 @@ class Plugin extends BasePlugin
*/
public function bootstrap(PluginApplicationInterface $app): void
{
Type::map('money', MoneyType::class);
TypeFactory::map('money', MoneyType::class);
}

/**
Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit 6c239c4

Please sign in to comment.