Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Dec 28, 2020
1 parent 297a7d3 commit bfabe17
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 35 deletions.
8 changes: 5 additions & 3 deletions .psalm/baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,6 @@
<code>$expected</code>
<code>$expected</code>
</PossiblyInvalidArgument>
<RedundantCondition occurrences="1">
<code>assert($step['object'] instanceof TestCase)</code>
</RedundantCondition>
</file>
<file src="src/Framework/Assert/Functions.php">
<MissingParamType occurrences="85">
Expand Down Expand Up @@ -1386,6 +1383,11 @@
<code>ensureOperatorIsValid</code>
</MissingThrowsDocblock>
</file>
<file src="src/Util/Warning.php">
<RedundantCondition occurrences="1">
<code>assert($step['object'] instanceof TestCase)</code>
</RedundantCondition>
</file>
<file src="src/Util/Xml.php">
<ArgumentTypeCoercion occurrences="1">
<code>$item</code>
Expand Down
21 changes: 4 additions & 17 deletions src/Framework/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
use PHPUnit\Framework\Constraint\TraversableContainsIdentical;
use PHPUnit\Framework\Constraint\TraversableContainsOnly;
use PHPUnit\Util\Type;
use PHPUnit\Util\Warning as WarningUtil;
use PHPUnit\Util\Xml;
use PHPUnit\Util\Xml\Loader as XmlLoader;

Expand Down Expand Up @@ -277,7 +278,9 @@ public static function assertCount(int $expectedCount, $haystack, string $messag
}

if ($haystack instanceof Generator) {
self::createWarning('Passing an argument of type Generator for the $haystack parameter is deprecated. Support for this will be removed in PHPUnit 11.');
(new WarningUtil)->createForTestCaseObjectOnCallStack(
'Passing an argument of type Generator for the $haystack parameter is deprecated. Support for this will be removed in PHPUnit 11.'
);
}

static::assertThat(
Expand Down Expand Up @@ -2516,20 +2519,4 @@ private static function isValidClassAttributeName(string $attributeName): bool
{
return (bool) preg_match('/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $attributeName);
}

/**
* @codeCoverageIgnore
*/
private static function createWarning(string $warning): void
{
foreach (debug_backtrace() as $step) {
if (isset($step['object']) && $step['object'] instanceof TestCase) {
assert($step['object'] instanceof TestCase);

$step['object']->addWarning($warning);

break;
}
}
}
}
19 changes: 4 additions & 15 deletions src/Framework/MockObject/Builder/InvocationMocker.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use function array_map;
use function array_merge;
use function count;
use function debug_backtrace;
use function in_array;
use function is_string;
use function strtolower;
Expand All @@ -36,7 +35,7 @@
use PHPUnit\Framework\MockObject\Stub\ReturnStub;
use PHPUnit\Framework\MockObject\Stub\ReturnValueMap;
use PHPUnit\Framework\MockObject\Stub\Stub;
use PHPUnit\Framework\TestCase;
use PHPUnit\Util\Warning as WarningUtil;
use Throwable;

/**
Expand Down Expand Up @@ -197,19 +196,9 @@ public function withConsecutive(...$arguments): self
{
$this->ensureParametersCanBeConfigured();

$stack = debug_backtrace();

while (!empty($stack)) {
$frame = array_pop($stack);

if (isset($frame['object']) && $frame['object'] instanceof TestCase) {
$frame['object']->addWarning(
'The withConsecutive() method has been deprecated. It will be removed in PHPUnit 11.'
);

break;
}
}
(new WarningUtil)->createForTestCaseObjectOnCallStack(
'The withConsecutive() method has been deprecated. It will be removed in PHPUnit 11.'
);

$this->matcher->setParametersRule(new Rule\ConsecutiveParameters($arguments));

Expand Down
33 changes: 33 additions & 0 deletions src/Util/Warning.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\Util;

use PHPUnit\Framework\TestCase;

/**
* @internal This class is not covered by the backward compatibility promise for PHPUnit
*
* @codeCoverageIgnore
*/
final class Warning
{
public function createForTestCaseObjectOnCallStack(string $warning): void
{
foreach (debug_backtrace() as $step) {
if (isset($step['object']) && $step['object'] instanceof TestCase) {
assert($step['object'] instanceof TestCase);

$step['object']->addWarning($warning);

break;
}
}
}
}

0 comments on commit bfabe17

Please sign in to comment.