From 4a71a772b3706fc6785fb8b7442e09b536ea3583 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Sun, 1 Oct 2023 15:04:57 +0200 Subject: [PATCH] Remove func_num_args() checks in favor of phpstan only (#2115) --- src/AbstractView.php | 4 +-- src/Loader.php | 2 -- src/Modal.php | 4 --- src/Popup.php | 4 +-- src/Table/Column.php | 4 --- src/View.php | 12 --------- tests/TableColumnTest.php | 18 ------------- tests/ViewTest.php | 54 --------------------------------------- 8 files changed, 2 insertions(+), 100 deletions(-) delete mode 100644 tests/TableColumnTest.php diff --git a/src/AbstractView.php b/src/AbstractView.php index 87ed04dd11..4633d20fbf 100644 --- a/src/AbstractView.php +++ b/src/AbstractView.php @@ -69,9 +69,7 @@ protected function init(): void */ public function add(self $object, array $args = []): self { - if ('func_num_args'() > 2) { // prevent bad usage - throw new \Error('Too many method arguments'); - } elseif ($this->_rendered) { + if ($this->_rendered) { throw new Exception('You cannot add anything into the view after it was rendered'); } diff --git a/src/Loader.php b/src/Loader.php index e8494599d0..3fff55472f 100644 --- a/src/Loader.php +++ b/src/Loader.php @@ -74,8 +74,6 @@ public function set($fx = null) { if (!$fx instanceof \Closure) { throw new \TypeError('$fx must be of type Closure'); - } elseif ('func_num_args'() > 1) { - throw new Exception('Only one argument is needed by Loader::set()'); } $this->cb->set(function () use ($fx) { diff --git a/src/Modal.php b/src/Modal.php index fa176f4125..39dd363b40 100644 --- a/src/Modal.php +++ b/src/Modal.php @@ -66,8 +66,6 @@ class Modal extends View /** * Set callback function for this modal. - * $fx is set as an array in order to comply with View::set(). - * TODO Rename this function and break BC? * * @param \Closure(View): void $fx * @@ -77,8 +75,6 @@ public function set($fx = null) { if (!$fx instanceof \Closure) { throw new \TypeError('$fx must be of type Closure'); - } elseif ('func_num_args'() > 1) { - throw new Exception('Only one argument is needed by Modal::set()'); } $this->fx = $fx; diff --git a/src/Popup.php b/src/Popup.php index af23261abf..70109f4d45 100644 --- a/src/Popup.php +++ b/src/Popup.php @@ -122,7 +122,7 @@ protected function init(): void /** * Set callback for loading content dynamically. - * Callback will receive a view attach to this popup + * Callback will receive a view attached to this popup * for adding content to it. * * @param \Closure(View): void $fx @@ -133,8 +133,6 @@ public function set($fx = null) { if (!$fx instanceof \Closure) { throw new \TypeError('$fx must be of type Closure'); - } elseif ('func_num_args'() > 1) { - throw new Exception('Only one argument is needed by Popup::set()'); } $this->cb = Callback::addTo($this); diff --git a/src/Table/Column.php b/src/Table/Column.php index c1ed102022..157b623dc9 100644 --- a/src/Table/Column.php +++ b/src/Table/Column.php @@ -53,10 +53,6 @@ class Column public function __construct(array $defaults = []) { - if ('func_num_args'() > 1) { // prevent bad usage - throw new \Error('Too many method arguments'); - } - $this->setDefaults($defaults); } diff --git a/src/View.php b/src/View.php index 8dd2b40fa3..56d51844d9 100644 --- a/src/View.php +++ b/src/View.php @@ -93,10 +93,6 @@ class View extends AbstractView */ public function __construct($label = []) { - if ('func_num_args'() > 1) { // prevent bad usage - throw new \Error('Too many method arguments'); - } - $defaults = is_array($label) ? $label : [$label]; if (array_key_exists(0, $defaults)) { @@ -270,10 +266,6 @@ public function getExecutorFactory(): ExecutorFactory */ public function add($object, $region = null): AbstractView { - if ('func_num_args'() > 2) { // prevent bad usage - throw new \Error('Too many method arguments'); - } - if (!is_object($object)) { // @phpstan-ignore-line // for BC do not throw // later consider to accept strictly objects only @@ -340,10 +332,6 @@ public function getClosestOwner(string $class): ?self */ public function set($content) { - if ('func_num_args'() > 1) { // prevent bad usage - throw new Exception('Only one argument is needed by View::set()'); - } - if (!is_string($content) && $content !== null) { // @phpstan-ignore-line throw (new Exception('Not sure what to do with argument')) ->addMoreInfo('this', $this) diff --git a/tests/TableColumnTest.php b/tests/TableColumnTest.php deleted file mode 100644 index 190161b5df..0000000000 --- a/tests/TableColumnTest.php +++ /dev/null @@ -1,18 +0,0 @@ -expectException(\Error::class); - $this->expectExceptionMessage('Too many method arguments'); - new Column([], []); // @phpstan-ignore-line - } -} diff --git a/tests/ViewTest.php b/tests/ViewTest.php index 81d2a2e3a8..3dc3959b89 100644 --- a/tests/ViewTest.php +++ b/tests/ViewTest.php @@ -91,33 +91,6 @@ public function testAddDelayedAbstractViewInit(): void self::assertTrue($vInner->isInitialized()); } - public function testTooManyArgumentsConstructorError(): void - { - $this->expectException(\Error::class); - $this->expectExceptionMessage('Too many method arguments'); - new View([], []); // @phpstan-ignore-line - } - - public function testTooManyArgumentsAddError(): void - { - $v = new View(); - $vInner = new View(); - - $this->expectException(\Error::class); - $this->expectExceptionMessage('Too many method arguments'); - $v->add($vInner, [], []); // @phpstan-ignore-line - } - - public function testTooManyArgumentsAbstractViewAddError(): void - { - $v = new class() extends AbstractView {}; - $vInner = new View(); - - $this->expectException(\Error::class); - $this->expectExceptionMessage('Too many method arguments'); - $v->add($vInner, [], []); // @phpstan-ignore-line - } - public function testSetModelTwiceException(): void { $v = new View(); @@ -178,31 +151,4 @@ public function provideSetNotClosureErrorCases(): iterable yield [Popup::class]; yield [VirtualPage::class]; } - - /** - * TODO remove the explicit exceptions and this test/provider once release 5.0 is made. - * - * @param class-string $class - * - * @dataProvider provideSetNotOneArgumentExceptionCases - */ - public function testSetNotOneArgumentException(string $class): void - { - $v = new $class(); - - $this->expectException(Exception::class); - $this->expectExceptionMessage('Only one argument is needed by ' . preg_replace('~.+\\\\~', '', $class) . '::set()'); - $v->set(static function () {}, null); // @phpstan-ignore-line - } - - /** - * @return iterable}> - */ - public function provideSetNotOneArgumentExceptionCases(): iterable - { - yield [View::class]; - yield [Loader::class]; - yield [Modal::class]; - yield [Popup::class]; - } }