Skip to content

Commit

Permalink
refactor: phpstan code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
khalwat committed Jan 19, 2025
1 parent 1980a53 commit 264cce9
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 61 deletions.
66 changes: 30 additions & 36 deletions src/fields/Units.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,22 @@
namespace nystudio107\units\fields;

use Craft;

use craft\base\ElementInterface;
use craft\base\Field;
use craft\base\PreviewableFieldInterface;
use craft\helpers\Json;
use craft\i18n\Locale;

use nystudio107\units\assetbundles\unitsfield\UnitsFieldAsset;
use nystudio107\units\helpers\ClassHelper;
use nystudio107\units\models\Settings;
use nystudio107\units\models\UnitsData;
use nystudio107\units\Units as UnitsPlugin;
use nystudio107\units\validators\EmbeddedUnitsDataValidator;

use PhpUnitsOfMeasure\PhysicalQuantity\Length;

use yii\base\InvalidConfigException;
use function is_array;
use function is_numeric;
use function is_string;

/**
* @author nystudio107
Expand All @@ -37,59 +36,55 @@
*/
class Units extends Field implements PreviewableFieldInterface
{
// Static Methods
// Public Properties
// =========================================================================

/**
* @inheritdoc
* @var ?string The default fully qualified class name of the unit of measure
*/
public static function displayName(): string
{
return Craft::t('units', 'Units');
}

// Public Properties
// =========================================================================
public $defaultUnitsClass = null;

/**
* @var string The default fully qualified class name of the unit of measure
* @var ?float The default value of the unit of measure
*/
public $defaultUnitsClass;
public $defaultValue = null;

/**
* @var float The default value of the unit of measure
* @var ?string The default units that the unit of measure is in
*/
public $defaultValue;
public $defaultUnits = null;

/**
* @var string The default units that the unit of measure is in
* @var ?bool Whether the units the field can be changed
*/
public $defaultUnits;
public $changeableUnits = null;

/**
* @var bool Whether the units the field can be changed
* @var int|float|null The minimum allowed number
*/
public $changeableUnits;
public $min = null;

/**
* @var int|float The minimum allowed number
* @var int|float|null The maximum allowed number
*/
public $min;
public $max = null;

/**
* @var int|float|null The maximum allowed number
* @var ?int The number of digits allowed after the decimal point
*/
public $max;
public $decimals = null;

/**
* @var int The number of digits allowed after the decimal point
* @var ?int The size of the field
*/
public $decimals;
public $size = null;

/**
* @var int|null The size of the field
* @inheritdoc
*/
public $size;
public static function displayName(): string
{
return Craft::t('units', 'Units');
}

// Public Methods
// =========================================================================
Expand All @@ -100,10 +95,10 @@ public static function displayName(): string
public function init()
{
parent::init();
/** @var Settings $settings */
if (UnitsPlugin::$plugin !== null) {
/** @var ?Settings $settings */
$settings = UnitsPlugin::$plugin->getSettings();
if (!empty($settings)) {
if ($settings !== null) {
$this->defaultUnitsClass = $this->defaultUnitsClass ?? $settings->defaultUnitsClass;
$this->defaultValue = $this->defaultValue ?? $settings->defaultValue;
$this->defaultUnits = $this->defaultUnits ?? $settings->defaultUnits;
Expand Down Expand Up @@ -161,12 +156,12 @@ public function normalizeValue($value, ElementInterface $element = null)
// Handle incoming values potentially being JSON or an array
if (!empty($value)) {
// Handle a numeric value coming in (perhaps from a Number field)
if (\is_numeric($value)) {
if (is_numeric($value)) {
$config['value'] = (float)$value;
} elseif (\is_string($value)) {
} elseif (is_string($value)) {
$config = Json::decodeIfJson($value);
}
if (\is_array($value)) {
if (is_array($value)) {
$config = array_merge($config, array_filter($value));
}
}
Expand Down Expand Up @@ -246,7 +241,6 @@ public function getInputHtml($value, ElementInterface $element = null): string
'namespacedId' => $namespacedId,
'value' => $value,
'model' => $model,
'field' => $this,
]
);
}
Expand Down
37 changes: 19 additions & 18 deletions src/models/UnitsData.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,24 @@
namespace nystudio107\units\models;

use craft\base\Model;

use nystudio107\units\Units;

use PhpUnitsOfMeasure\AbstractPhysicalQuantity;

use PhpUnitsOfMeasure\Exception\NonNumericValue;
use PhpUnitsOfMeasure\Exception\NonStringUnitName;
use PhpUnitsOfMeasure\PhysicalQuantityInterface;
use PhpUnitsOfMeasure\UnitOfMeasure;
use PhpUnitsOfMeasure\UnitOfMeasureInterface;
use yii\base\InvalidArgumentException;
use function call_user_func_array;
use function get_class;

/**
* @author nystudio107
* @package Units
* @since 1.0.0
*
* @property string $valueFraction
* @property array|float[] $valueParts
* @property string $valueFraction
* @property array|float[] $valueParts
* @property array|string[] $valuePartsFraction
*/
class UnitsData extends Model implements PhysicalQuantityInterface
Expand All @@ -37,19 +38,19 @@ class UnitsData extends Model implements PhysicalQuantityInterface
// =========================================================================

/**
* @var string The fully qualified class name of the unit of measure
* @var ?string The fully qualified class name of the unit of measure
*/
public $unitsClass;
public $unitsClass = null;

/**
* @var float The value of the unit of measure
* @var ?float The value of the unit of measure
*/
public $value;
public $value = null;

/**
* @var string The units that the unit of measure is in
* @var ?string The units that the unit of measure is in
*/
public $units;
public $units = null;

/**
* @var AbstractPhysicalQuantity
Expand All @@ -64,18 +65,18 @@ class UnitsData extends Model implements PhysicalQuantityInterface
* class in $unitsInstance, if it exists
*
* @param string $method
* @param array $args
* @param array $args
*
* @return mixed
* @throws InvalidArgumentException
* @throws \PhpUnitsOfMeasure\Exception\NonNumericValue
* @throws \PhpUnitsOfMeasure\Exception\NonStringUnitName
* @throws NonNumericValue
* @throws NonStringUnitName
*/
public function __call($method, $args)
{
$unitsInstance = $this->unitsInstance;
if (method_exists($unitsInstance, $method)) {
return \call_user_func_array([$unitsInstance, $method], $args);
return call_user_func_array([$unitsInstance, $method], $args);
}

throw new InvalidArgumentException("Method {$method} doesn't exist");
Expand Down Expand Up @@ -186,7 +187,6 @@ public function isEquivalentQuantity(PhysicalQuantityInterface $testQuantity)
public function availableUnits(bool $includeAliases = true)
{
$availableUnits = [];
/** @var AbstractPhysicalQuantity $unitsClass */
$units = $this->unitsInstance::getUnitDefinitions();
/** @var UnitOfMeasure $unit */
foreach ($units as $unit) {
Expand All @@ -207,10 +207,11 @@ public function toFraction(): string
{
return trim(Units::$variable->fraction($this->value) . ' ' . $this->units);
}

/**
* Return the measurement as a fraction, in the given unit of measure
*
* @param UnitOfMeasureInterface|string $unit The desired unit of measure,
* @param UnitOfMeasureInterface|string $unit The desired unit of measure,
* or a string name of one
*
* @return string The measurement cast in the requested units, as a
Expand Down Expand Up @@ -272,7 +273,7 @@ public function getValuePartsFraction(): array
*/
protected function physicalQuantityToUnitsData(PhysicalQuantityInterface $quantity): UnitsData
{
$unitsClass = \get_class($quantity);
$unitsClass = get_class($quantity);
list($value, $units) = explode(' ', (string)$quantity);
$config = [
'unitsClass' => $unitsClass,
Expand Down
13 changes: 6 additions & 7 deletions src/validators/EmbeddedUnitsDataValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@
namespace nystudio107\units\validators;

use Craft;

use nystudio107\units\models\UnitsData;

use yii\base\Model;
use yii\validators\NumberValidator;
use yii\validators\Validator;
use function is_object;

/**
* @author nystudio107
Expand All @@ -40,18 +39,18 @@ class EmbeddedUnitsDataValidator extends Validator
*/
public $integerOnly = false;
/**
* @var int|float upper limit of the number. Defaults to null, meaning no
* @var int|float|null upper limit of the number. Defaults to null, meaning no
* upper limit.
* @see tooBig for the customized message used when the number is too big.
*/
public $max;
public $max = null;
/**
* @var int|float lower limit of the number. Defaults to null, meaning no
* @var int|float|null lower limit of the number. Defaults to null, meaning no
* lower limit.
* @see tooSmall for the customized message used when the number is too
* small.
*/
public $min;
public $min = null;

// Public Methods
// =========================================================================
Expand All @@ -64,7 +63,7 @@ public function validateAttribute($model, $attribute)
/** @var Model $model */
$value = $model->$attribute;

if ($value !== null && \is_object($value) && $value instanceof UnitsData) {
if ($value !== null && is_object($value) && $value instanceof UnitsData) {
// Validate the model
$value->validate();
// Normalize the min/max value
Expand Down

0 comments on commit 264cce9

Please sign in to comment.