From 9de7d3e266013f9ff531b79d736d30c504235cfc Mon Sep 17 00:00:00 2001 From: Mark Story Date: Fri, 19 Jan 2024 22:50:56 -0500 Subject: [PATCH] Fix fatal errors when calling get methods Replacing fatal errors with runtime exceptions. Fatal errors are tricky to catch and exceptions are much simpler to work with. --- src/Phinx/Db/Table/Column.php | 4 ++++ src/Phinx/Db/Table/ForeignKey.php | 4 ++++ tests/Phinx/Db/Table/ColumnTest.php | 7 +++++++ tests/Phinx/Db/Table/ForeignKeyTest.php | 6 ++++++ 4 files changed, 21 insertions(+) diff --git a/src/Phinx/Db/Table/Column.php b/src/Phinx/Db/Table/Column.php index dde4cf2d2..f677c7760 100644 --- a/src/Phinx/Db/Table/Column.php +++ b/src/Phinx/Db/Table/Column.php @@ -212,6 +212,10 @@ public function setType(string|Literal $type) */ public function getType(): string|Literal { + if (!isset($this->type)) { + throw new RuntimeException('Cannot access `type` it has not been set'); + } + return $this->type; } diff --git a/src/Phinx/Db/Table/ForeignKey.php b/src/Phinx/Db/Table/ForeignKey.php index a734d6125..54a61c670 100644 --- a/src/Phinx/Db/Table/ForeignKey.php +++ b/src/Phinx/Db/Table/ForeignKey.php @@ -96,6 +96,10 @@ public function setReferencedTable(Table $table) */ public function getReferencedTable(): Table { + if (!isset($this->referencedTable)) { + throw new RuntimeException('Cannot access `referencedTable` it has not been set'); + } + return $this->referencedTable; } diff --git a/tests/Phinx/Db/Table/ColumnTest.php b/tests/Phinx/Db/Table/ColumnTest.php index 38481488d..6489e4e59 100644 --- a/tests/Phinx/Db/Table/ColumnTest.php +++ b/tests/Phinx/Db/Table/ColumnTest.php @@ -20,6 +20,13 @@ public function testSetOptionThrowsExceptionIfOptionIsNotString() $column->setOptions(['identity']); } + public function testGetType() + { + $column = new Column(); + $this->expectException(RuntimeException::class); + $column->getType(); + } + public function testSetOptionsIdentity() { $column = new Column(); diff --git a/tests/Phinx/Db/Table/ForeignKeyTest.php b/tests/Phinx/Db/Table/ForeignKeyTest.php index f3776310a..7d03a55aa 100644 --- a/tests/Phinx/Db/Table/ForeignKeyTest.php +++ b/tests/Phinx/Db/Table/ForeignKeyTest.php @@ -34,6 +34,12 @@ public function testInitiallyActionsEmpty() $this->assertNull($this->fk->getOnUpdate()); } + public function testRefrencedTableUndefined() + { + $this->expectException(RuntimeException::class); + $this->fk->getReferencedTable(); + } + /** * @param string $dirtyValue * @param string $valueOfConstant