Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force nullable data to be always nulled #138

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Hydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
}
}

$this->updateColumnToTargetMap($path, $columnToPropertyMap);

Check failure on line 76 in src/Hydrator.php

View workflow job for this annotation

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

Parameter #2 $columnToPropertyMap of method ipl\Orm\Hydrator::updateColumnToTargetMap() expects array<string, string>, array|false given.

Check failure on line 76 in src/Hydrator.php

View workflow job for this annotation

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

Parameter #2 $columnToPropertyMap of method ipl\Orm\Hydrator::updateColumnToTargetMap() expects array<string, string>, array|false given.

Check failure on line 76 in src/Hydrator.php

View workflow job for this annotation

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

Parameter #2 $columnToPropertyMap of method ipl\Orm\Hydrator::updateColumnToTargetMap() expects array<string, string>, array|false given.
$this->hydrators[$path] = [$target, $relation, $columnToPropertyMap, $defaults];

return $this;
Expand Down Expand Up @@ -140,6 +140,7 @@
} 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 @@
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 @@
{
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 @@
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);
}
}
Loading