Skip to content

Commit

Permalink
To avoid users from having to deal with conflict resolution, do away …
Browse files Browse the repository at this point in the history
…with the Runkit trait in favor of more static methods on the Runkit support class
  • Loading branch information
stevegrunwell committed Oct 31, 2020
1 parent b38ecb3 commit 6453c48
Show file tree
Hide file tree
Showing 11 changed files with 188 additions and 220 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"Tests\\": "tests/"
},
"files": [
"tests/Support/functions.php"
"tests/stubs/functions.php"
]
},
"config": {
Expand Down
4 changes: 4 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@ parameters:
paths:
- tests/FixtureTest.php
- tests/FunctionsTest.php

-
message: '#Call to an undefined static method \S+#'
path: tests/Support/RunkitTest.php
79 changes: 0 additions & 79 deletions src/Concerns/Runkit.php

This file was deleted.

12 changes: 8 additions & 4 deletions src/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

trait Constants
{
use Concerns\Runkit;

/**
* All constants being handled by this trait.
*
Expand Down Expand Up @@ -43,6 +41,8 @@ protected function restoreConstants()

unset($this->constants['created'][$key]);
}

Runkit::reset();
}

/**
Expand All @@ -59,7 +59,9 @@ protected function restoreConstants()
*/
protected function setConstant($name, $value = null)
{
$this->requiresRunkit('setConstant() requires Runkit be available, skipping.');
if (! Runkit::isAvailable()) {
$this->markTestSkipped('setConstant() requires Runkit be available, skipping.');
}

if (defined($name)) {
if (! isset($this->constants['updated'][$name])) {
Expand Down Expand Up @@ -96,7 +98,9 @@ protected function deleteConstant($name)
return $this;
}

$this->requiresRunkit('deleteConstant() requires Runkit be available, skipping.');
if (! Runkit::isAvailable()) {
$this->markTestSkipped('deleteConstant() requires Runkit be available, skipping.');
}

if (! isset($this->constants[$name])) {
$this->constants['updated'][$name] = constant($name);
Expand Down
16 changes: 10 additions & 6 deletions src/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

trait Functions
{
use Concerns\Runkit;

/**
* All functions being handled by this trait.
*
Expand Down Expand Up @@ -41,6 +39,8 @@ protected function restoreFunctions()

array_map([Runkit::class, 'function_remove'], $this->functions['defined']);
$this->functions['defined'] = [];

Runkit::reset();
}

/**
Expand All @@ -64,7 +64,9 @@ protected function defineFunction($name, \Closure $closure)
));
}

$this->requiresRunkit('defineFunction() requires Runkit be available, skipping.');
if (! Runkit::isAvailable()) {
$this->markTestSkipped('defineFunction() requires Runkit be available, skipping.');
}

if (! Runkit::function_add($name, $closure)) {
throw new RunkitException(sprintf('Unable to define function %1$s().', $name));
Expand All @@ -91,11 +93,13 @@ protected function redefineFunction($name, \Closure $closure)
return $this->defineFunction($name, $closure);
}

$this->requiresRunkit('redefineFunction() requires Runkit be available, skipping.');
if (! Runkit::isAvailable()) {
$this->markTestSkipped('redefineFunction() requires Runkit be available, skipping.');
}

// Back up the original version of the function.
if (! isset($this->functions['redefined'][$name])) {
$namespaced = $this->runkitNamespace($name);
$namespaced = Runkit::makeNamespaced($name);

if (! Runkit::function_rename($name, $namespaced)) {
throw new RunkitException(sprintf('Unable to back up %1$s(), aborting.', $name));
Expand Down Expand Up @@ -126,7 +130,7 @@ protected function deleteFunction($name)
return $this;
}

$namespaced = $this->runkitNamespace($name);
$namespaced = Runkit::makeNamespaced($name);

if (! Runkit::function_rename($name, $namespaced)) {
throw new RunkitException(sprintf('Unable to back up %1$s(), aborting.', $name));
Expand Down
64 changes: 64 additions & 0 deletions src/Support/Runkit.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@
*/
class Runkit
{
/**
* A namespace used to move things out of the way for the duration of a test.
*
* @var string
*/
private static $namespace;

/**
* Dynamically alias methods to the underlying Runkit functions.
*
Expand All @@ -57,4 +64,61 @@ public static function __callStatic($name, array $args = [])
$name
));
}

/**
* Determine whether or not Runkit is available in the current environment.
*
* @return bool
*/
public static function isAvailable()
{
return function_exists('runkit7_constant_redefine')
|| function_exists('runkit_constant_redefine');
}

/**
* Get the current runkit namespace.
*
* If the property is currently empty, one will be created.
*
* @return string The namespace (with trailing backslash) where we're moving functions,
* constants, etc. during tests.
*/
public static function getNamespace()
{
if (empty(self::$namespace)) {
self::$namespace = uniqid(__NAMESPACE__ . '\\runkit_') . '\\';
}

return self::$namespace;
}

/**
* Namespace the given reference.
*
* @param string $var The item to be moved into the temporary test namespace.
*
* @return string The newly-namespaced item.
*/
public static function makeNamespaced($var)
{
// Strip leading backslashes.
if (0 === mb_strpos($var, '\\')) {
$var = mb_substr($var, 1);
}

return self::getNamespace() . $var;
}

/**
* Reset static properties.
*
* This is helpful to run before tests in case self::$namespace gets polluted.
*
* @return void
*/
public static function reset()
{
self::$namespace = '';
}
}
98 changes: 0 additions & 98 deletions tests/Concerns/RunkitTest.php

This file was deleted.

Loading

0 comments on commit 6453c48

Please sign in to comment.