Skip to content

Commit

Permalink
Apply fixes from StyleCI (#53)
Browse files Browse the repository at this point in the history
Co-authored-by: StyleCI Bot <bot@styleci.io>
  • Loading branch information
jaredtking and StyleCIBot committed Jul 26, 2023
1 parent cad63e7 commit b45261c
Show file tree
Hide file tree
Showing 18 changed files with 81 additions and 88 deletions.
14 changes: 7 additions & 7 deletions src/DefinitionBuilder.php
Expand Up @@ -28,7 +28,7 @@ public static function get(string $modelClass): Definition
/**
* Builds a model definition given certain parameters.
*
* @param string[] $ids
* @param string[] $ids
* @param Property[] $properties
*/
public static function build(array $ids, array $properties, string $modelClass): Definition
Expand All @@ -54,13 +54,13 @@ public static function build(array $ids, array $properties, string $modelClass):
$relationType = $property['relation_type'] ?? null;
if (isset($property['relation']) && !$relationType) {
self::buildBelongsToLegacy($k, $property);
} elseif (isset($property['belongs_to']) || $relationType == Relationship::BELONGS_TO) {
} elseif (isset($property['belongs_to']) || Relationship::BELONGS_TO == $relationType) {
self::buildBelongsTo($k, $property, $result);
} elseif (isset($property['has_one']) || $relationType == Relationship::HAS_ONE) {
} elseif (isset($property['has_one']) || Relationship::HAS_ONE == $relationType) {
self::buildHasOne($property, $modelClass);
} elseif (isset($property['belongs_to_many']) || $relationType == Relationship::BELONGS_TO_MANY) {
} elseif (isset($property['belongs_to_many']) || Relationship::BELONGS_TO_MANY == $relationType) {
self::buildBelongsToMany($property, $modelClass);
} elseif (isset($property['has_many']) || $relationType == Relationship::HAS_MANY) {
} elseif (isset($property['has_many']) || Relationship::HAS_MANY == $relationType) {
self::buildHasMany($property, $modelClass);
} elseif (isset($property['morphs_to'])) {
self::buildPolymorphic($property, $k);
Expand All @@ -82,9 +82,9 @@ public static function build(array $ids, array $properties, string $modelClass):
return new Definition($ids, $result);
}

/////////////////////////////////
// ///////////////////////////////
// Relationship Shortcuts
/////////////////////////////////
// ///////////////////////////////

/**
* This is added for BC with older versions of pulsar
Expand Down
4 changes: 2 additions & 2 deletions src/Driver/AbstractDriver.php
Expand Up @@ -76,11 +76,11 @@ protected function prefixWhere(array $where, string $tablename): array
// handles $where[property] = value
if (!is_numeric($key)) {
$return[] = [$this->prefixColumn($key, $tablename), $condition];
// handles $where[] = [property, value, '=']
// handles $where[] = [property, value, '=']
} elseif (is_array($condition)) {
$condition[0] = $this->prefixColumn($condition[0], $tablename);
$return[] = $condition;
// handles raw SQL - do nothing
// handles raw SQL - do nothing
} else {
$return[] = [$condition];
}
Expand Down
1 change: 0 additions & 1 deletion src/Driver/DatabaseDriver.php
Expand Up @@ -13,7 +13,6 @@

use JAQB\ConnectionManager;
use JAQB\Exception\JAQBException;
use JAQB\Query\SelectQuery;
use JAQB\QueryBuilder;
use PDOException;
use PDOStatement;
Expand Down
6 changes: 3 additions & 3 deletions src/Driver/DbalDriver.php
Expand Up @@ -222,9 +222,9 @@ public function min(Query $query, string $field): float
return (float) $this->executeScalar($dbQuery, $model, $field);
}

//////////////////////////
/// Helpers
//////////////////////////
// ////////////////////////
// / Helpers
// ////////////////////////

/**
* Builds a new select query.
Expand Down
16 changes: 8 additions & 8 deletions src/Errors.php
Expand Up @@ -138,9 +138,9 @@ public function __toString(): string
return implode("\n", $this->all());
}

//////////////////////////
// ////////////////////////
// Helpers
//////////////////////////
// ////////////////////////

/**
* Formats an incoming error message.
Expand All @@ -162,18 +162,18 @@ private function parse(string $error, array $context): string
return $this->getTranslator()->translate($error, $context, null);
}

//////////////////////////
// ////////////////////////
// IteratorAggregate Interface
//////////////////////////
// ////////////////////////

public function getIterator(): Traversable
{
return new ArrayIterator($this->stack);
}

//////////////////////////
// ////////////////////////
// Countable Interface
//////////////////////////
// ////////////////////////

/**
* Get total number of models matching query.
Expand All @@ -183,9 +183,9 @@ public function count(): int
return count($this->stack);
}

/////////////////////////////
// ///////////////////////////
// ArrayAccess Interface
/////////////////////////////
// ///////////////////////////

public function offsetExists($offset): bool
{
Expand Down
1 change: 0 additions & 1 deletion src/EventManager.php
Expand Up @@ -11,7 +11,6 @@
*/
class EventManager
{
/** @var array */
private static array $dispatchers = [];

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Hydrator.php
Expand Up @@ -54,7 +54,7 @@ public static function hydrate(array $result, string|Model $modelClass, array $e
if (isset($relationships[$id])) {
if ($property->persisted) {
$models[$j]->setRelation($k, $relationships[$id]);
// older style properties do not support this type of hydration
// older style properties do not support this type of hydration
} else {
$models[$j]->hydrateValue($k, $relationships[$id]);
}
Expand All @@ -67,7 +67,7 @@ public static function hydrate(array $result, string|Model $modelClass, array $e
if (isset($relationships[$id])) {
if ($property->persisted) {
$models[$j]->setRelation($k, $relationships[$id]);
// older style properties do not support this type of hydration
// older style properties do not support this type of hydration
} else {
$models[$j]->hydrateValue($k, $relationships[$id]);
}
Expand Down Expand Up @@ -111,7 +111,7 @@ public static function hydrate(array $result, string|Model $modelClass, array $e
/**
* Hydrates the eager-loaded relationships for a given set of IDs.
*
* @param bool $multiple when true will condense
* @param bool $multiple when true will condense
*
* @return Model[]
*/
Expand Down
16 changes: 8 additions & 8 deletions src/Iterator.php
Expand Up @@ -56,9 +56,9 @@ public function getQuery(): Query
return $this->query;
}

//////////////////////////
// ////////////////////////
// Iterator Interface
//////////////////////////
// ////////////////////////

/**
* Rewind the Iterator to the first element.
Expand Down Expand Up @@ -122,9 +122,9 @@ public function valid(): bool
return null !== $this->current();
}

//////////////////////////
// ////////////////////////
// Countable Interface
//////////////////////////
// ////////////////////////

/**
* Get total number of models matching query.
Expand All @@ -136,9 +136,9 @@ public function count(): int
return $this->count;
}

//////////////////////////
// ////////////////////////
// ArrayAccess Interface
//////////////////////////
// ////////////////////////

public function offsetExists($offset): bool
{
Expand Down Expand Up @@ -173,9 +173,9 @@ public function offsetUnset($offset)
throw new \Exception('Cannot perform unset on immutable Iterator');
}

//////////////////////////
// ////////////////////////
// Private Methods
//////////////////////////
// ////////////////////////

/**
* Load the next round of models.
Expand Down
42 changes: 20 additions & 22 deletions src/Model.php
Expand Up @@ -36,9 +36,9 @@ abstract class Model implements ArrayAccess
{
const DEFAULT_ID_NAME = 'id';

/////////////////////////////
// ///////////////////////////
// Model visible variables
/////////////////////////////
// ///////////////////////////

protected array $_values = [];
private array $_unsaved = [];
Expand All @@ -47,9 +47,9 @@ abstract class Model implements ArrayAccess
/** @var AbstractRelation[] */
private array $relationships = [];

/////////////////////////////
// ///////////////////////////
// Base model variables
/////////////////////////////
// ///////////////////////////

private static array $initialized = [];
private static ?DriverInterface $driver = null;
Expand Down Expand Up @@ -210,9 +210,9 @@ public function hasId(): bool
return $this->hasId;
}

/////////////////////////////
// ///////////////////////////
// Magic Methods
/////////////////////////////
// ///////////////////////////

/**
* Converts the model into a string.
Expand Down Expand Up @@ -297,9 +297,9 @@ public function __unset(string $name): void
}
}

/////////////////////////////
// ///////////////////////////
// ArrayAccess Interface
/////////////////////////////
// ///////////////////////////

public function offsetExists($offset): bool
{
Expand Down Expand Up @@ -331,9 +331,9 @@ public static function __callStatic($name, $parameters)
return call_user_func_array([static::query(), $name], $parameters);
}

/////////////////////////////
// ///////////////////////////
// Property Definitions
/////////////////////////////
// ///////////////////////////

/**
* Gets the model definition.
Expand Down Expand Up @@ -442,9 +442,9 @@ public static function getAccessor(string $property): ?string
return self::$accessors[$k];
}

/////////////////////////////
// ///////////////////////////
// CRUD Operations
/////////////////////////////
// ///////////////////////////

/**
* Gets the table name for storing this model.
Expand Down Expand Up @@ -1104,9 +1104,9 @@ public function isDeleted(): bool
return !$this->_persisted;
}

/////////////////////////////
// ///////////////////////////
// Queries
/////////////////////////////
// ///////////////////////////

/**
* Generates a new query instance.
Expand Down Expand Up @@ -1227,9 +1227,9 @@ public function clearCache(): static
return $this;
}

/////////////////////////////
// ///////////////////////////
// Relationships
/////////////////////////////
// ///////////////////////////

/**
* Gets the relationship manager for a property.
Expand Down Expand Up @@ -1290,8 +1290,6 @@ private function saveRelationships(bool $usesTransactions): bool
* scalar value or relationship.
*
* @internal
*
* @param $value
*/
public function hydrateValue(string $name, $value): void
{
Expand Down Expand Up @@ -1364,9 +1362,9 @@ public function clearRelation(string $k): static
return $this;
}

/////////////////////////////
// ///////////////////////////
// Events
/////////////////////////////
// ///////////////////////////

/**
* Adds a listener to the model.creating and model.updating events.
Expand Down Expand Up @@ -1454,9 +1452,9 @@ public static function deleted(callable $listener, int $priority = 0): void
EventManager::listen(static::class, ModelDeleted::NAME, $listener, $priority);
}

/////////////////////////////
// ///////////////////////////
// Validation
/////////////////////////////
// ///////////////////////////

/**
* Gets the error stack for this model.
Expand Down
5 changes: 2 additions & 3 deletions src/Property.php
Expand Up @@ -43,9 +43,8 @@ public function __construct(
?string $has_many = null,
public readonly ?string $enum_class = null,
public readonly ?string $date_format = null,
)
{
$this->hasDefault = $default !== self::MISSING_DEFAULT;
) {
$this->hasDefault = self::MISSING_DEFAULT !== $default;
$this->default = $this->hasDefault ? $default : null;

// Relationship shortcuts
Expand Down
7 changes: 3 additions & 4 deletions src/Query.php
Expand Up @@ -133,9 +133,8 @@ public function getSort(): array
* iii) where('balance', 100, '>')
* iv) where('balance > 100').
*
* @param array|string $where
* @param mixed $value optional value
* @param string|null $condition optional condition
* @param mixed $value optional value
* @param string|null $condition optional condition
*
* @return $this
*/
Expand Down Expand Up @@ -181,7 +180,7 @@ public function getWhere(): array
* to the ID of the model we are joining.
*
* @param Model|string $model model being joined
* @param string $column name of local property
* @param string $column name of local property
*
* @return $this
*/
Expand Down
6 changes: 3 additions & 3 deletions src/Type.php
Expand Up @@ -61,15 +61,15 @@ public static function cast(Property $property, mixed $value): mixed
return $value;
}

if ($type == self::ENUM) {
if (self::ENUM == $type) {
return self::to_enum($value, (string) $property->enum_class);
}

if ($type == self::DATE) {
if (self::DATE == $type) {
return self::to_date($value, $property->date_format);
}

if ($type == self::DATETIME) {
if (self::DATETIME == $type) {
return self::to_datetime($value, $property->date_format);
}

Expand Down
3 changes: 1 addition & 2 deletions tests/Driver/SerializeValueTestTrait.php
Expand Up @@ -3,7 +3,6 @@
namespace Pulsar\Tests\Driver;

use DateTimeImmutable;
use Pulsar\Driver\DatabaseDriver;
use Pulsar\Property;
use Pulsar\Tests\Enums\TestEnumInteger;
use Pulsar\Tests\Enums\TestEnumString;
Expand Down Expand Up @@ -49,4 +48,4 @@ public function testSerializeValueDateTime(): void
$this->assertEquals('2023-01-08 01:02:03', $driver->serializeValue(new DateTimeImmutable('2023-01-08 01:02:03'), new Property(type: Type::DATETIME)));
$this->assertEquals('1673139723', $driver->serializeValue(new DateTimeImmutable('2023-01-08 01:02:03'), new Property(date_format: 'U')));
}
}
}

0 comments on commit b45261c

Please sign in to comment.