PHP Debugging library used to spy on objects' private values/methods https://packagist.org/packages/danjones000/object-spy
Find a file
Dan Jones 0b800a74c9
Some checks failed
build / Coding standards (push) Has been cancelled
build / Static analysis (push) Has been cancelled
build / Security analysis (push) Has been cancelled
build / Code coverage (push) Has been cancelled
build / Unit tests (--ignore-platform-req=php, highest, ubuntu-latest, 8.1) (push) Has been cancelled
build / Unit tests (highest, macos-latest, 7.4) (push) Has been cancelled
build / Unit tests (highest, macos-latest, 8.0) (push) Has been cancelled
build / Unit tests (highest, ubuntu-latest, 7.4) (push) Has been cancelled
build / Unit tests (highest, ubuntu-latest, 8.0) (push) Has been cancelled
build / Unit tests (highest, windows-latest, 7.4) (push) Has been cancelled
build / Unit tests (highest, windows-latest, 8.0) (push) Has been cancelled
build / Unit tests (lowest, macos-latest, 7.4) (push) Has been cancelled
build / Unit tests (lowest, macos-latest, 8.0) (push) Has been cancelled
build / Unit tests (lowest, ubuntu-latest, 7.4) (push) Has been cancelled
build / Unit tests (lowest, ubuntu-latest, 8.0) (push) Has been cancelled
build / Unit tests (lowest, windows-latest, 7.4) (push) Has been cancelled
build / Unit tests (lowest, windows-latest, 8.0) (push) Has been cancelled
Merge tag 'v0.1.0' into develop
🔖 Initial version
2021-10-15 11:28:50 -05:00
.github chore: initialize project using ramsey/php-library-starter-kit 2021-10-14 15:41:02 -05:00
bin chore: initialize project using ramsey/php-library-starter-kit 2021-10-14 15:41:02 -05:00
build chore: initialize project using ramsey/php-library-starter-kit 2021-10-14 15:41:02 -05:00
docs chore: initialize project using ramsey/php-library-starter-kit 2021-10-14 15:41:02 -05:00
resources chore: initialize project using ramsey/php-library-starter-kit 2021-10-14 15:41:02 -05:00
src Add spy function 2021-10-15 11:15:40 -05:00
tests Add spy function 2021-10-15 11:15:40 -05:00
.ac-php-conf.json Implement ObjectProxy class 2021-10-15 10:43:30 -05:00
.editorconfig chore: initialize project using ramsey/php-library-starter-kit 2021-10-14 15:41:02 -05:00
.gitattributes chore: initialize project using ramsey/php-library-starter-kit 2021-10-14 15:41:02 -05:00
.gitignore chore: initialize project using ramsey/php-library-starter-kit 2021-10-14 15:41:02 -05:00
captainhook.json 💬 Rename GitHub to GitLab 2021-10-14 15:57:58 -05:00
CHANGELOG.md chore: initialize project using ramsey/php-library-starter-kit 2021-10-14 15:41:02 -05:00
CODE_OF_CONDUCT.md chore: initialize project using ramsey/php-library-starter-kit 2021-10-14 15:41:02 -05:00
codecov.yml chore: initialize project using ramsey/php-library-starter-kit 2021-10-14 15:41:02 -05:00
composer.json Add spy function 2021-10-15 11:15:40 -05:00
CONTRIBUTING.md 💬 Rename GitHub to GitLab 2021-10-14 15:57:58 -05:00
conventional-commits.json chore: initialize project using ramsey/php-library-starter-kit 2021-10-14 15:41:02 -05:00
LICENSE chore: initialize project using ramsey/php-library-starter-kit 2021-10-14 15:41:02 -05:00
phpcs.xml.dist Implement ObjectProxy class 2021-10-15 10:43:30 -05:00
phpstan.neon.dist Implement ObjectProxy class 2021-10-15 10:43:30 -05:00
phpunit.xml.dist chore: initialize project using ramsey/php-library-starter-kit 2021-10-14 15:41:02 -05:00
psalm-baseline.xml chore: initialize project using ramsey/php-library-starter-kit 2021-10-14 15:41:02 -05:00
psalm.xml Implement ObjectProxy class 2021-10-15 10:43:30 -05:00
README.md Implement ObjectProxy class 2021-10-15 10:43:30 -05:00

Object Spy

Debugging library used to spy on objects' private values/methods

About

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.

This project adheres to a code of conduct. By participating in this project and its community, you are expected to uphold this code.

Installation

Install this package as a dependency using Composer.

composer require danjones000/object-spy

Usage

use Danjones000\Spy\ObjectProxy;

$object = new Object(); // Can be any object.

$spy = new ObjectProxy($object);

// Access private property
echo $spy->privateProperty;

// Set private property
$spy->privateProperty = 42;

// Call private method
$spy->privateMethod();

// Call private method with parameters
$spy->protectedMethod(42, false);

// 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);

// PHP allows static methods to be called non-statically, so, this works as well
$spy->staticPrivateMethod();

// Can also access static properties/methods without an object instance
$staticSpy = new ObjectProxy(Object::class);

// 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.

The danjones000/object-spy library is copyright © Dan Jones and licensed for use under the terms of the MIT License (MIT). Please see LICENSE for more information.