Skip to content

Commit

Permalink
fixed issues reported by phpstan
Browse files Browse the repository at this point in the history
  • Loading branch information
arusinowski committed Mar 1, 2024
1 parent e0d8680 commit 0ff1968
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 37 deletions.
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>
24 changes: 13 additions & 11 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
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
15 changes: 8 additions & 7 deletions src/Money.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 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
16 changes: 9 additions & 7 deletions src/Utility/MoneyUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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';
Expand All @@ -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);
}

/**
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -121,6 +122,7 @@ protected static function _loadMoneyFormatter(Currency $currency) : MoneyFormatt
*/
public static function zero() : Money
{
/** @var Money */
return self::money(0);
}

Expand Down
7 changes: 6 additions & 1 deletion src/View/Helper/MoneyHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];

/**
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down

0 comments on commit 0ff1968

Please sign in to comment.