From 7f34b8ea7454054a5efc74f4178efa91b187c051 Mon Sep 17 00:00:00 2001 From: JonGardiner Date: Fri, 10 May 2024 10:39:33 -0600 Subject: [PATCH] Improvements for null assertions. - isset($a->b->c) now asserts that $a is not null, $a->b is not null, and $a->b->c is not null - Consistent with php, !is_null($a->b->c) asserts the same, but does report null dereferences in the parameter before it asserts. - To avoid errors use !is_null($a?->b?->c) or isset($a->b->c). - Don't assert when "$this instanceof" is used. Ie. $this instanceOf Foo from inside of Bar does not change $this to be a Foo. --- tests/units/Checks/TestData/TestComplexTypes.5.inc | 13 +++++++++++++ tests/units/Checks/TestData/TestComplexTypes.6.inc | 14 ++++++++++++++ .../TestData/TestNullablePropertiesCheck.4.inc | 12 ++++++++++++ .../TestData/TestNullablePropertiesCheck.5.inc | 14 ++++++++++++++ 4 files changed, 53 insertions(+) create mode 100644 tests/units/Checks/TestData/TestComplexTypes.5.inc create mode 100644 tests/units/Checks/TestData/TestComplexTypes.6.inc create mode 100644 tests/units/Checks/TestData/TestNullablePropertiesCheck.4.inc create mode 100644 tests/units/Checks/TestData/TestNullablePropertiesCheck.5.inc diff --git a/tests/units/Checks/TestData/TestComplexTypes.5.inc b/tests/units/Checks/TestData/TestComplexTypes.5.inc new file mode 100644 index 0000000..2063345 --- /dev/null +++ b/tests/units/Checks/TestData/TestComplexTypes.5.inc @@ -0,0 +1,13 @@ +testArray)) { + $this->testArray = explode(',', $this->testArray); + } + return $this->testArray; + } +} diff --git a/tests/units/Checks/TestData/TestComplexTypes.6.inc b/tests/units/Checks/TestData/TestComplexTypes.6.inc new file mode 100644 index 0000000..c4d609d --- /dev/null +++ b/tests/units/Checks/TestData/TestComplexTypes.6.inc @@ -0,0 +1,14 @@ +testArray->foo = true ? 'bar' : []; + if (!is_array($this->testArray->foo)) { + $this->testArray->foo = explode(',', $this->testArray->foo); + } + return $this->testArray; + } +} diff --git a/tests/units/Checks/TestData/TestNullablePropertiesCheck.4.inc b/tests/units/Checks/TestData/TestNullablePropertiesCheck.4.inc new file mode 100644 index 0000000..d26df22 --- /dev/null +++ b/tests/units/Checks/TestData/TestNullablePropertiesCheck.4.inc @@ -0,0 +1,12 @@ +a)) { + $this->a = 0; + } + return $this->a; + } +}