-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
65743b7
commit 3aec108
Showing
10 changed files
with
431 additions
and
26 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
First line of the first file | ||
Unique string | ||
non-unique string |
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,2 @@ | ||
First line of the second file | ||
non-unique string |
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,2 @@ | ||
First line of the third file | ||
Second line with non-unique string in the middle |
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,2 @@ | ||
First line of the fourth file | ||
End of file. |
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,2 @@ | ||
First line of the fifth file | ||
Second line with non-unique string in the middle |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace AlexSkrypnyk\Customizer\Tests\Traits; | ||
|
||
/** | ||
* Trait ReflectionTrait. | ||
* | ||
* Provides methods to work with class reflection. | ||
*/ | ||
trait ReflectionTrait { | ||
|
||
/** | ||
* Call protected methods on the class. | ||
* | ||
* @param object|string $object | ||
* Object or class name to use for a method call. | ||
* @param string $name | ||
* Method name. Method can be static. | ||
* @param array $args | ||
* Array of arguments to pass to the method. To pass arguments by reference, | ||
* pass them by reference as an element of this array. | ||
* | ||
* @return mixed | ||
* Method result. | ||
*/ | ||
protected static function callProtectedMethod(object|string $object, string $name, array $args = []) { | ||
$object_or_class = is_object($object) ? $object::class : $object; | ||
|
||
if (!class_exists($object_or_class)) { | ||
throw new \InvalidArgumentException(sprintf('Class %s does not exist', $object_or_class)); | ||
} | ||
|
||
$class = new \ReflectionClass($object_or_class); | ||
|
||
if (!$class->hasMethod($name)) { | ||
throw new \InvalidArgumentException(sprintf('Method %s does not exist', $name)); | ||
} | ||
|
||
$method = $class->getMethod($name); | ||
|
||
$original_accessibility = $method->isPublic(); | ||
|
||
// Set method accessibility to true, so it can be invoked. | ||
$method->setAccessible(TRUE); | ||
|
||
// If the method is static, we won't pass an object instance to invokeArgs() | ||
// Otherwise, we ensure to pass the object instance. | ||
$invoke_object = $method->isStatic() ? NULL : (is_object($object) ? $object : NULL); | ||
|
||
// Ensure we have an object for non-static methods. | ||
if (!$method->isStatic() && $invoke_object === NULL) { | ||
throw new \InvalidArgumentException("An object instance is required for non-static methods"); | ||
} | ||
|
||
$result = $method->invokeArgs($invoke_object, $args); | ||
|
||
// Reset the method's accessibility to its original state. | ||
$method->setAccessible($original_accessibility); | ||
|
||
return $result; | ||
} | ||
|
||
/** | ||
* Set protected property value. | ||
* | ||
* @param object $object | ||
* Object to set the value on. | ||
* @param string $property | ||
* Property name to set the value. Property should exists in the object. | ||
* @param mixed $value | ||
* Value to set to the property. | ||
*/ | ||
protected static function setProtectedValue($object, $property, mixed $value): void { | ||
$class = new \ReflectionClass($object::class); | ||
$property = $class->getProperty($property); | ||
$property->setAccessible(TRUE); | ||
|
||
$property->setValue($object, $value); | ||
} | ||
|
||
/** | ||
* Get protected value from the object. | ||
* | ||
* @param object $object | ||
* Object to set the value on. | ||
* @param string $property | ||
* Property name to get the value. Property should exists in the object. | ||
* | ||
* @return mixed | ||
* Protected property value. | ||
*/ | ||
protected static function getProtectedValue($object, $property) { | ||
$class = new \ReflectionClass($object::class); | ||
$property = $class->getProperty($property); | ||
$property->setAccessible(TRUE); | ||
|
||
return $property->getValue($class); | ||
} | ||
|
||
} |
Oops, something went wrong.