Skip to content

Commit

Permalink
Increase PHPStan level to 2
Browse files Browse the repository at this point in the history
  • Loading branch information
staudenmeir committed Sep 25, 2024
1 parent 0d65b49 commit 4d36bb8
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 22 deletions.
4 changes: 1 addition & 3 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
includes:
- ./vendor/larastan/larastan/extension.neon
parameters:
level: 1
level: 2
paths:
- src
- tests
ignoreErrors:
- "#Called 'first' on Laravel collection, but could have been retrieved as a query.#"
- '#Call to private method take\(\) of parent class Illuminate\\Database\\Eloquent\\Relations\\BelongsTo#'
- '#Unsafe usage of new static\(\).#'
8 changes: 8 additions & 0 deletions src/Grammars/JsonGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,12 @@ public function compileMemberOf(string $column, ?string $objectKey, mixed $value
* @return array
*/
public function prepareBindingsForMemberOf(mixed $value): array;

/**
* Wrap a value in keyword identifiers.
*
* @param \Illuminate\Contracts\Database\Query\Expression|string $value
* @return string
*/
public function wrap($value);
}
2 changes: 2 additions & 0 deletions src/Grammars/Traits/CompilesMySqlJsonQueries.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public function compileJsonValueSelect(string $column): string
*/
public function supportsMemberOf(ConnectionInterface $connection): bool
{
/** @var \Illuminate\Database\MySqlConnection $connection */

if ($connection->isMaria()) {
return false;
}
Expand Down
39 changes: 30 additions & 9 deletions src/HasJsonRelationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ public function getAttributeValue($key)
*/
protected function newHasOne(Builder $query, Model $parent, $foreignKey, $localKey)
{
if ($query->getConnection()->getDriverName() === 'pgsql') {
/** @var \Illuminate\Database\Connection $connection */
$connection = $query->getConnection();

if ($connection->getDriverName() === 'pgsql') {
return new HasOnePostgres($query, $parent, $foreignKey, $localKey);
}

Expand All @@ -115,7 +118,10 @@ protected function newHasOne(Builder $query, Model $parent, $foreignKey, $localK
*/
protected function newHasOneThrough(Builder $query, Model $farParent, Model $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey)
{
if ($query->getConnection()->getDriverName() === 'pgsql') {
/** @var \Illuminate\Database\Connection $connection */
$connection = $query->getConnection();

if ($connection->getDriverName() === 'pgsql') {
return new HasOneThroughPostgres($query, $farParent, $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey);
}

Expand All @@ -134,7 +140,10 @@ protected function newHasOneThrough(Builder $query, Model $farParent, Model $thr
*/
protected function newMorphOne(Builder $query, Model $parent, $type, $id, $localKey)
{
if ($query->getConnection()->getDriverName() === 'pgsql') {
/** @var \Illuminate\Database\Connection $connection */
$connection = $query->getConnection();

if ($connection->getDriverName() === 'pgsql') {
return new MorphOnePostgres($query, $parent, $type, $id, $localKey);
}

Expand All @@ -153,7 +162,10 @@ protected function newMorphOne(Builder $query, Model $parent, $type, $id, $local
*/
protected function newBelongsTo(Builder $query, Model $child, $foreignKey, $ownerKey, $relation)
{
if ($query->getConnection()->getDriverName() === 'pgsql') {
/** @var \Illuminate\Database\Connection $connection */
$connection = $query->getConnection();

if ($connection->getDriverName() === 'pgsql') {
return new BelongsToPostgres($query, $child, $foreignKey, $ownerKey, $relation);
}

Expand All @@ -171,7 +183,10 @@ protected function newBelongsTo(Builder $query, Model $child, $foreignKey, $owne
*/
protected function newHasMany(Builder $query, Model $parent, $foreignKey, $localKey)
{
if ($query->getConnection()->getDriverName() === 'pgsql') {
/** @var \Illuminate\Database\Connection $connection */
$connection = $query->getConnection();

if ($connection->getDriverName() === 'pgsql') {
return new HasManyPostgres($query, $parent, $foreignKey, $localKey);
}

Expand All @@ -192,7 +207,10 @@ protected function newHasMany(Builder $query, Model $parent, $foreignKey, $local
*/
protected function newHasManyThrough(Builder $query, Model $farParent, Model $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey)
{
if ($query->getConnection()->getDriverName() === 'pgsql') {
/** @var \Illuminate\Database\Connection $connection */
$connection = $query->getConnection();

if ($connection->getDriverName() === 'pgsql') {
return new HasManyThroughPostgres($query, $farParent, $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey);
}

Expand All @@ -211,7 +229,10 @@ protected function newHasManyThrough(Builder $query, Model $farParent, Model $th
*/
protected function newMorphMany(Builder $query, Model $parent, $type, $id, $localKey)
{
if ($query->getConnection()->getDriverName() === 'pgsql') {
/** @var \Illuminate\Database\Connection $connection */
$connection = $query->getConnection();

if ($connection->getDriverName() === 'pgsql') {
return new MorphManyPostgres($query, $parent, $type, $id, $localKey);
}

Expand Down Expand Up @@ -428,7 +449,7 @@ protected function addHasManyThroughJsonPivotRelationship(
$hasManyJson->hydratePivotRelation(
$models,
$this,
fn (Model $model) => json_decode($model->laravel_through_key, true)
fn (Model $model) => json_decode($model->laravel_through_key ?? '[]', true)
);
}
};
Expand All @@ -445,7 +466,7 @@ function (array $models, Collection $results, string $relation) use ($hasManyJso
$hasManyJson->hydratePivotRelation(
$model->$relation,
$model,
fn (Model $model) => json_decode($model->laravel_through_key, true)
fn (Model $model) => json_decode($model->laravel_through_key ?? '[]', true)
);
}

Expand Down
8 changes: 5 additions & 3 deletions src/Relations/HasManyJson.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,12 @@ protected function setForeignAttributesForCreate(Model $model)
{
$foreignKey = explode('.', $this->foreignKey)[1];

/** @var \Staudenmeir\EloquentJsonRelations\Relations\BelongsToJson $relation */
$relation = $model->belongsToJson(get_class($this->parent), $foreignKey, $this->localKey);
if (method_exists($model, 'belongsToJson')) {
/** @var \Staudenmeir\EloquentJsonRelations\Relations\BelongsToJson $relation */
$relation = $model->belongsToJson(get_class($this->parent), $foreignKey, $this->localKey);

$relation->attach($this->getParentKey());
$relation->attach($this->getParentKey());
}
}

/**
Expand Down
6 changes: 4 additions & 2 deletions src/Relations/Postgres/HasOneOrManyThrough.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ protected function performJoin(?Builder $query = null)

$query->join($this->throughParent->getTable(), $this->getQualifiedParentKeyName(), '=', $farKey);

if ($this->throughParentSoftDeletes()) {
if ($this->throughParentSoftDeletes()
&& method_exists($this->throughParent, 'getQualifiedDeletedAtColumn')) {
$query->whereNull($this->throughParent->getQualifiedDeletedAtColumn());
}
}
Expand Down Expand Up @@ -99,7 +100,8 @@ public function getRelationExistenceQueryForThroughSelfRelation(Builder $query,

$query->join($table, $hash.'.'.$this->secondLocalKey, '=', $farKey);

if ($this->throughParentSoftDeletes()) {
if ($this->throughParentSoftDeletes()
&& method_exists($this->throughParent, 'getDeletedAtColumn')) {
$query->whereNull($hash.'.'.$this->throughParent->getDeletedAtColumn());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ protected function buildDictionaryForDeepRelationship(Collection $results): arra
$dictionary = [];

foreach ($results as $result) {
$dictionary[$result->laravel_through_key][] = $result;
$dictionary[$result->laravel_through_key ?? null][] = $result;
}

return $dictionary;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ protected function buildDictionaryForDeepRelationship(Collection $results): arra
$key = $this->key ? str_replace('->', '.', $this->key) : null;

foreach ($results as $result) {
$values = json_decode($result->laravel_through_key, true);
$values = json_decode($result->laravel_through_key ?? '[]', true);

if ($key) {
$values = array_filter(
Expand Down
7 changes: 4 additions & 3 deletions src/Relations/Traits/IsJsonRelation.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,11 @@ protected function whereJsonContainsOrMemberOf(
*/
protected function getJsonGrammar(Builder $query): JsonGrammar
{
$driver = $query->getConnection()->getDriverName();
/** @var \Illuminate\Database\Connection $connection */
$connection = $query->getConnection();

return $query->getConnection()->withTablePrefix(
match ($driver) {
return $connection->withTablePrefix(
match ($connection->getDriverName()) {
'mysql' => new MySqlGrammar(),
'mariadb' => new MariaDbGrammar(),
'pgsql' => new PostgresGrammar(),
Expand Down

0 comments on commit 4d36bb8

Please sign in to comment.