Skip to content

Commit

Permalink
Force nullable data to be always nulled
Browse files Browse the repository at this point in the history
  • Loading branch information
nilmerg committed Jul 24, 2024
1 parent e9d6117 commit 46b2c03
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Hydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ public function hydrate(array $data, Model $model)
} else {
$subjectClass = $relation->getTargetClass();
$subject = new $subjectClass();
$subject->setRelation($relation);

Check failure on line 143 in src/Hydrator.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.2 on ubuntu-latest

Call to an undefined method object::setRelation().

Check failure on line 143 in src/Hydrator.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

Call to an undefined method object::setRelation().

Check failure on line 143 in src/Hydrator.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

Call to an undefined method object::setRelation().

Check failure on line 143 in src/Hydrator.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.4 on ubuntu-latest

Call to an undefined method object::setRelation().

Check failure on line 143 in src/Hydrator.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.1 on ubuntu-latest

Call to an undefined method object::setRelation().

Check failure on line 143 in src/Hydrator.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.0 on ubuntu-latest

Call to an undefined method object::setRelation().

Check failure on line 143 in src/Hydrator.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.3 on ubuntu-latest

Call to an undefined method object::setRelation().
$parent->$relationName = $subject;
}
}
Expand Down Expand Up @@ -187,6 +188,7 @@ public function hydrate(array $data, Model $model)
if (! $subject->hasProperty($step)) {
$stepClass = $relation->getTargetClass();
$subject->$step = new $stepClass();
$subject->$step->setRelation($relation);

Check failure on line 191 in src/Hydrator.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.2 on ubuntu-latest

Call to an undefined method object::setRelation().

Check failure on line 191 in src/Hydrator.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

Call to an undefined method object::setRelation().

Check failure on line 191 in src/Hydrator.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

Call to an undefined method object::setRelation().

Check failure on line 191 in src/Hydrator.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.4 on ubuntu-latest

Call to an undefined method object::setRelation().

Check failure on line 191 in src/Hydrator.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.1 on ubuntu-latest

Call to an undefined method object::setRelation().

Check failure on line 191 in src/Hydrator.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.0 on ubuntu-latest

Call to an undefined method object::setRelation().

Check failure on line 191 in src/Hydrator.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.3 on ubuntu-latest

Call to an undefined method object::setRelation().
}

$subject = $subject->$step;
Expand Down
56 changes: 56 additions & 0 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ abstract class Model implements \ArrayAccess, \IteratorAggregate
{
use PropertiesWithDefaults;

private $relation;

Check failure on line 17 in src/Model.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.2 on ubuntu-latest

Property ipl\Orm\Model::$relation has no type specified.

Check failure on line 17 in src/Model.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

Property ipl\Orm\Model::$relation has no type specified.

Check failure on line 17 in src/Model.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

Property ipl\Orm\Model::$relation has no type specified.

Check failure on line 17 in src/Model.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.4 on ubuntu-latest

Property ipl\Orm\Model::$relation has no type specified.

Check failure on line 17 in src/Model.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.1 on ubuntu-latest

Property ipl\Orm\Model::$relation has no type specified.

Check failure on line 17 in src/Model.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.0 on ubuntu-latest

Property ipl\Orm\Model::$relation has no type specified.

Check failure on line 17 in src/Model.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.3 on ubuntu-latest

Property ipl\Orm\Model::$relation has no type specified.

public function setRelation(Relation $relation)

Check failure on line 19 in src/Model.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.2 on ubuntu-latest

Method ipl\Orm\Model::setRelation() has no return type specified.

Check failure on line 19 in src/Model.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

Method ipl\Orm\Model::setRelation() has no return type specified.

Check failure on line 19 in src/Model.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

Method ipl\Orm\Model::setRelation() has no return type specified.

Check failure on line 19 in src/Model.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.4 on ubuntu-latest

Method ipl\Orm\Model::setRelation() has no return type specified.

Check failure on line 19 in src/Model.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.1 on ubuntu-latest

Method ipl\Orm\Model::setRelation() has no return type specified.

Check failure on line 19 in src/Model.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.0 on ubuntu-latest

Method ipl\Orm\Model::setRelation() has no return type specified.

Check failure on line 19 in src/Model.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.3 on ubuntu-latest

Method ipl\Orm\Model::setRelation() has no return type specified.
{
$this->relation = $relation;
}

private $nullableProperties;

Check failure on line 24 in src/Model.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.2 on ubuntu-latest

Property ipl\Orm\Model::$nullableProperties has no type specified.

Check failure on line 24 in src/Model.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

Property ipl\Orm\Model::$nullableProperties has no type specified.

Check failure on line 24 in src/Model.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

Property ipl\Orm\Model::$nullableProperties has no type specified.

Check failure on line 24 in src/Model.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.4 on ubuntu-latest

Property ipl\Orm\Model::$nullableProperties has no type specified.

Check failure on line 24 in src/Model.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.1 on ubuntu-latest

Property ipl\Orm\Model::$nullableProperties has no type specified.

Check failure on line 24 in src/Model.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.0 on ubuntu-latest

Property ipl\Orm\Model::$nullableProperties has no type specified.

Check failure on line 24 in src/Model.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.3 on ubuntu-latest

Property ipl\Orm\Model::$nullableProperties has no type specified.

final public function __construct(array $properties = null)
{
if ($this->hasProperties()) {
Expand Down Expand Up @@ -140,4 +149,51 @@ public function createRelations(Relations $relations)
protected function init()
{
}

private function nullableColumns()

Check failure on line 153 in src/Model.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.2 on ubuntu-latest

Method ipl\Orm\Model::nullableColumns() has no return type specified.

Check failure on line 153 in src/Model.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

Method ipl\Orm\Model::nullableColumns() has no return type specified.

Check failure on line 153 in src/Model.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

Method ipl\Orm\Model::nullableColumns() has no return type specified.

Check failure on line 153 in src/Model.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.4 on ubuntu-latest

Method ipl\Orm\Model::nullableColumns() has no return type specified.

Check failure on line 153 in src/Model.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.1 on ubuntu-latest

Method ipl\Orm\Model::nullableColumns() has no return type specified.

Check failure on line 153 in src/Model.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.0 on ubuntu-latest

Method ipl\Orm\Model::nullableColumns() has no return type specified.

Check failure on line 153 in src/Model.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.3 on ubuntu-latest

Method ipl\Orm\Model::nullableColumns() has no return type specified.
{
if ($this->nullableProperties !== null) {
return $this->nullableProperties;
}

if ($this->relation === null) {
$this->nullableProperties = (function (): array {
$ref = new \ReflectionClass($this);
$doc = $ref->getDocComment();
preg_match_all('/@property\s+([^\s]+)\s+\$([^\s]+)/', $doc, $matches);

Check failure on line 163 in src/Model.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.2 on ubuntu-latest

Parameter #2 $subject of function preg_match_all expects string, string|false given.

Check failure on line 163 in src/Model.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

Parameter #2 $subject of function preg_match_all expects string, string|false given.

Check failure on line 163 in src/Model.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

Parameter #2 $subject of function preg_match_all expects string, string|false given.

Check failure on line 163 in src/Model.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.4 on ubuntu-latest

Parameter #2 $subject of function preg_match_all expects string, string|false given.

Check failure on line 163 in src/Model.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.1 on ubuntu-latest

Parameter #2 $subject of function preg_match_all expects string, string|false given.

Check failure on line 163 in src/Model.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.0 on ubuntu-latest

Parameter #2 $subject of function preg_match_all expects string, string|false given.

Check failure on line 163 in src/Model.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.3 on ubuntu-latest

Parameter #2 $subject of function preg_match_all expects string, string|false given.
$cols = [];
foreach ($matches[1] as $i => $type) {
if (strpos($type, '?') === 0 || preg_match('~\|?null\|?~', $type)) {
$cols[] = $matches[2][$i];
}
}

return $cols;
})();
} elseif ($this->relation->getJoinType() === 'LEFT') {
$this->nullableProperties = array_merge($this->getColumns(), (array) $this->getKeyName());
} else {
$this->nullableProperties = [];
}

return $this->nullableProperties;
}

public function __get($key)

Check failure on line 182 in src/Model.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.2 on ubuntu-latest

Method ipl\Orm\Model::__get() has no return type specified.

Check failure on line 182 in src/Model.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.2 on ubuntu-latest

Method ipl\Orm\Model::__get() has parameter $key with no type specified.

Check failure on line 182 in src/Model.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.1 on ubuntu-latest

Method ipl\Orm\Model::__get() has no return type specified.

Check failure on line 182 in src/Model.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.1 on ubuntu-latest

Method ipl\Orm\Model::__get() has parameter $key with no type specified.

Check failure on line 182 in src/Model.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.0 on ubuntu-latest

Method ipl\Orm\Model::__get() has no return type specified.

Check failure on line 182 in src/Model.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.0 on ubuntu-latest

Method ipl\Orm\Model::__get() has parameter $key with no type specified.

Check failure on line 182 in src/Model.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.3 on ubuntu-latest

Method ipl\Orm\Model::__get() has no return type specified.

Check failure on line 182 in src/Model.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.3 on ubuntu-latest

Method ipl\Orm\Model::__get() has parameter $key with no type specified.
{
if (in_array($key, $this->nullableColumns(), true)) {
return null;
}

return $this->getProperty($key);
}

public function __isset($key)

Check failure on line 191 in src/Model.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.2 on ubuntu-latest

Method ipl\Orm\Model::__isset() has parameter $key with no type specified.

Check failure on line 191 in src/Model.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.1 on ubuntu-latest

Method ipl\Orm\Model::__isset() has parameter $key with no type specified.

Check failure on line 191 in src/Model.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.0 on ubuntu-latest

Method ipl\Orm\Model::__isset() has parameter $key with no type specified.

Check failure on line 191 in src/Model.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.3 on ubuntu-latest

Method ipl\Orm\Model::__isset() has parameter $key with no type specified.
{
if (in_array($key, $this->nullableColumns(), true)) {
return false;
}

return $this->hasProperty($key);
}
}

0 comments on commit 46b2c03

Please sign in to comment.