https://github.com/linuscash/with-global-state
simple utility that will wrap any function in order to make its global state requirements clear
https://github.com/linuscash/with-global-state
global-state refactoring state-management testability
Last synced: 2 months ago
JSON representation
simple utility that will wrap any function in order to make its global state requirements clear
- Host: GitHub
- URL: https://github.com/linuscash/with-global-state
- Owner: linuscash
- Created: 2018-08-27T08:19:27.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-08-27T09:07:24.000Z (over 7 years ago)
- Last Synced: 2024-11-08T12:07:38.584Z (over 1 year ago)
- Topics: global-state, refactoring, state-management, testability
- Language: PHP
- Size: 2.93 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# with-global-state
withGlobalState is a simple utility that will wrap any function in order to make its global state requirements clear.
## motivation
Working on an application recently I found that large chunks of it shared global state. I wanted to focus my efforts untangling modules that shared state but didn't want to go all big bang on it and run down the rabbit hole. Thus this little function was born. I used it to get a grip on each modules global state while I refactored it out without interfering with the other modules.
## example
```php
function greetMe (String $moniker): String
{
return "Hello {$moniker} my name is {$GLOBALS['name']}";
}
greetMe('stranger'); // ??
$linusGreetMe = withGlobalState('greetMe', ['name' => 'Linus']);
$linusGreetMe('stranger'); // "Hello stranger my name is Linus"
```
The above is a contrived example but it does demonstrate the functions purpose. We can see a poorly implemented function that depends on global state instantly become more readable, manageable and testable.
Essentially we can write tests against this legacy code. Once we have a handle on it, we can refactor it down to something more meanful and maintainable without treading on the toes of other modules.