Skip to content

Commit

Permalink
Remove func_num_args() checks in favor of phpstan only (#2115)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek authored Oct 1, 2023
1 parent 08a63e0 commit 4a71a77
Show file tree
Hide file tree
Showing 8 changed files with 2 additions and 100 deletions.
4 changes: 1 addition & 3 deletions src/AbstractView.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand Down
2 changes: 0 additions & 2 deletions src/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 0 additions & 4 deletions src/Modal.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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;
Expand Down
4 changes: 1 addition & 3 deletions src/Popup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down
4 changes: 0 additions & 4 deletions src/Table/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
12 changes: 0 additions & 12 deletions src/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
18 changes: 0 additions & 18 deletions tests/TableColumnTest.php

This file was deleted.

54 changes: 0 additions & 54 deletions tests/ViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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<View> $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<list{class-string<View>}>
*/
public function provideSetNotOneArgumentExceptionCases(): iterable
{
yield [View::class];
yield [Loader::class];
yield [Modal::class];
yield [Popup::class];
}
}

0 comments on commit 4a71a77

Please sign in to comment.