2021-10-14 15:57:58 -05:00
< h1 align = "center" > Object Spy< / h1 >
2021-10-14 15:41:02 -05:00
< p align = "center" >
< strong > Debugging library used to spy on objects' private values/methods< / strong >
< / p >
<!--
TODO: Make sure the following URLs are correct and working for your project.
Then, remove these comments to display the badges, giving users a quick
overview of your package.
< p align = "center" >
< a href = "https://packagist.org/packages/danjones000/object-spy" > < img src = "https://img.shields.io/packagist/v/danjones000/object-spy.svg?style=flat-square&label=release" alt = "Download Package" > < / a >
< a href = "https://php.net" > < img src = "https://img.shields.io/packagist/php-v/danjones000/object-spy.svg?style=flat-square&colorB=%238892BF" alt = "PHP Programming Language" > < / a >
2021-10-14 15:57:58 -05:00
< a href = "https://gitlab.com/danjones000/object-spy/blob/main/LICENSE" > < img src = "https://img.shields.io/packagist/l/danjones000/object-spy.svg?style=flat-square&colorB=darkcyan" alt = "Read License" > < / a >
2021-10-14 15:41:02 -05:00
< a href = "https://github.com/danjones000/object-spy/actions/workflows/continuous-integration.yml" > < img src = "https://img.shields.io/github/workflow/status/danjones000/object-spy/build/main?style=flat-square&logo=github" alt = "Build Status" > < / a >
< a href = "https://codecov.io/gh/danjones000/object-spy" > < img src = "https://img.shields.io/codecov/c/gh/danjones000/object-spy?label=codecov&logo=codecov&style=flat-square" alt = "Codecov Code Coverage" > < / a >
2021-10-14 15:57:58 -05:00
< a href = "https://shepherd.dev/gitlab/danjones000/object-spy" > < img src = "https://img.shields.io/endpoint?style=flat-square&url=https%3A%2F%2Fshepherd.dev%2Fgitlab%2Fdanjones000%2Fobject-spy%2Fcoverage" alt = "Psalm Type Coverage" > < / a >
2021-10-14 15:41:02 -05:00
< / p >
-->
## About
2021-10-15 10:43:30 -05:00
A debugging/testing tool which allows access to an object's or class's private
and protected properties and methods without the use of Reflection.
2021-10-14 15:41:02 -05:00
This project adheres to a [code of conduct ](CODE_OF_CONDUCT.md ).
By participating in this project and its community, you are expected to
uphold this code.
## Installation
Install this package as a dependency using [Composer ](https://getcomposer.org ).
``` bash
composer require danjones000/object-spy
```
## Usage
``` php
2021-10-15 10:43:30 -05:00
use Danjones000\Spy\ObjectProxy;
2021-10-14 15:41:02 -05:00
2021-10-15 10:43:30 -05:00
$object = new Object(); // Can be any object.
2021-10-14 15:41:02 -05:00
2021-10-15 10:43:30 -05:00
$spy = new ObjectProxy($object);
2021-10-14 15:41:02 -05:00
2021-10-15 10:43:30 -05:00
// Access private property
echo $spy->privateProperty;
2021-10-14 15:41:02 -05:00
2021-10-15 10:43:30 -05:00
// Set private property
$spy->privateProperty = 42;
2021-10-14 15:41:02 -05:00
2021-10-15 10:43:30 -05:00
// Call private method
$spy->privateMethod();
2021-10-14 15:41:02 -05:00
2021-10-15 10:43:30 -05:00
// Call private method with parameters
$spy->protectedMethod(42, false);
2021-10-14 15:41:02 -05:00
2021-10-15 10:43:30 -05:00
// Run arbitry code on object, allows passing in parameters
// All arguments after the closure are passed as arguments to the closure itself
$spy->call(fn ($abc) => $this->someProp = $this->someMethod($abc) * static::MULTIPLIER, 500);
2021-10-14 15:41:02 -05:00
2021-10-15 10:43:30 -05:00
// PHP allows static methods to be called non-statically, so, this works as well
$spy->staticPrivateMethod();
2021-10-14 15:41:02 -05:00
2021-10-15 10:43:30 -05:00
// Can also access static properties/methods without an object instance
$staticSpy = new ObjectProxy(Object::class);
2021-10-14 15:41:02 -05:00
2021-10-15 10:43:30 -05:00
// If there is an Object::$privateStaticProperty
echo $staticSpy->privateStaticProperty;
$staticSpy->privateStaticProperty = 42;
$staticSpy->privateStaticMethod();
// No direct access to contstants, but we can do this
$staticSpy->call(fn () => static::PRIVATE_CONSTANT); // self also works, of course
```
## Implementation notes
This class does not use reflections in its operation. Instead, if uses closures
that are bound to the instance or class.
## Contributing
Contributions are welcome! To contribute, please familiarize yourself with
[CONTRIBUTING.md ](CONTRIBUTING.md ).
2021-10-14 15:41:02 -05:00
## Copyright and License
The danjones000/object-spy library is copyright © [Dan Jones ](https://danielrayjones.com/ )
and licensed for use under the terms of the
MIT License (MIT). Please see [LICENSE ](LICENSE ) for more information.