-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update Hook::safe_offset() to handle closures (#183)
## Summary Reimplements #174 after implementing PhpStan and code styles in WP_Mock. Tests have been reworked in PhpUnit. Quote from OP: > WP_Mock\Functions::type('callable') was causing a mismatch between the safe_offset values returned for the Mockery matcher and the actual closure being passed to add_action. The matcher was getting spl_object_hash'd, while the Closure was getting `"__CLOSURE__"`. Now, safe_offset() treats Mockery matchers with type closure as `"__CLOSURE__"` too. ### Closes: #89 ## Test - [x] Unit tests pass ## Open questions I think we could rename `safe_offset()` into something more meaningful to make it easier to understand what it does. Seems this is used solely within the `Hook` abstract and Filter/Action implementations to get a string representations of the hook arguments. We could rename it using camel case and soft deprecate the former method. As predicted, PhpStan will start spitting errors as tests, return types and such start to change. I covered some of the issues in this PR, but had to update the baseline to fix several unrelated positives which we can address in subsequent PRs. Co-authored-by: Ashley Gibson <[email protected]>
- Loading branch information
1 parent
9d16132
commit 4ea29a9
Showing
10 changed files
with
318 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
php/WP_Mock/Traits/AccessInaccessibleClassMembersTrait.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
namespace WP_Mock\Traits; | ||
|
||
use ReflectionClass; | ||
use ReflectionException; | ||
use ReflectionMethod; | ||
use ReflectionProperty; | ||
|
||
/** | ||
* Trait for accessing inaccessible class members through reflection. | ||
*/ | ||
trait AccessInaccessibleClassMembersTrait | ||
{ | ||
/** | ||
* Gets the given inaccessible method for the given class. | ||
* | ||
* Allows for calling protected and private methods on a class. | ||
* | ||
* @param class-string|object $class the class name or instance | ||
* @param string $methodName the method name | ||
* @return ReflectionMethod | ||
* @throws ReflectionException | ||
*/ | ||
public function getInaccessibleMethod($class, string $methodName): ReflectionMethod | ||
{ | ||
$class = new ReflectionClass($class); | ||
|
||
$method = $class->getMethod($methodName); | ||
$method->setAccessible(true); | ||
|
||
return $method; | ||
} | ||
|
||
/** | ||
* Gets the given inaccessible property for the given class. | ||
* | ||
* Allows for calling protected and private properties on a class. | ||
* | ||
* @param class-string|object $class the class name or instance | ||
* @param string $propertyName the property name | ||
* @return ReflectionProperty | ||
* @throws ReflectionException | ||
*/ | ||
public function getInaccessibleProperty($class, string $propertyName): ReflectionProperty | ||
{ | ||
$class = new ReflectionClass($class); | ||
|
||
$property = $class->getProperty($propertyName); | ||
$property->setAccessible(true); | ||
|
||
return $property; | ||
} | ||
|
||
/** | ||
* Allows for setting private or protected properties in a class. | ||
* | ||
* @param object|null $instance class instance or null for static classes | ||
* @param class-string $class | ||
* @param string $property | ||
* @param mixed $value | ||
* @return ReflectionProperty | ||
* @throws ReflectionException | ||
*/ | ||
public function setInaccessibleProperty($instance, string $class, string $property, $value): ReflectionProperty | ||
{ | ||
$class = new ReflectionClass($class); | ||
|
||
$property = $class->getProperty($property); | ||
$property->setAccessible(true); | ||
$property->setValue($instance, $value); | ||
|
||
return $property; | ||
} | ||
} |
Oops, something went wrong.