https://github.com/mortalflesh/callbackparser
PHP parser for arrow functions
https://github.com/mortalflesh/callbackparser
arrow-functions callback php-parser wtf
Last synced: 3 months ago
JSON representation
PHP parser for arrow functions
- Host: GitHub
- URL: https://github.com/mortalflesh/callbackparser
- Owner: MortalFlesh
- License: mit
- Created: 2016-09-04T16:58:59.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2021-03-30T14:29:38.000Z (over 4 years ago)
- Last Synced: 2025-04-09T15:12:34.543Z (3 months ago)
- Topics: arrow-functions, callback, php-parser, wtf
- Language: PHP
- Size: 30.3 KB
- Stars: 0
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
CallbackParser
==============[](https://packagist.org/packages/mf/callback-parser)
[](https://travis-ci.org/MortalFlesh/CallbackParser)
[](https://coveralls.io/github/MortalFlesh/CallbackParser?branch=master)
[](https://packagist.org/packages/mf/callback-parser)
[](https://packagist.org/packages/mf/callback-parser)PHP parser for arrow functions
> This library is no longer supported, since the arrow functions are natively in PHP 7.4 - https://www.php.net/manual/en/functions.arrow.php## Table of Contents
- [Requirements](#requirements)
- [Installation](#installation)
- [Arrow Functions](#arrow-functions)
- [How does it work](#how-does-it-work)
- [Possibly WTF?](#wtf)
- [80:20 - Simple](#80-20)## Requirements
- PHP 7.1
- `eval()` function for parsing [Arrow Functions](#arrow-functions)## Installation:
```
composer require mf/callback-parser
```### Usage:
```php
$callbackParser = new CallbackParser();
$callback = $callbackParser->parseArrowFunction('($a, $b) => $a + $b');var_dump($callback(2, 3)); // int 5
```### With Custom Exception
```php
$callbackParser = new CallbackParser(App\MyCustomException::class);$callbackParser->parseArrowFunction('invalid'); // throws App\MyCustomException
```## How does it work?
- it parses function from string and evaluate it with `eval()`## Possibly WTF?
This parser can parse an arrow function into PHP to be execute as usual.
But this process could be a little bit more complex than just `eval` it.
You can check `CallbackParserTest::provideInvalidFunction()` for examples.### Namespaces of parameters
For example namespace of class for given parameter type.
```php
(SimpleEntity $entity) => $entity->getId()
```
This example above shows an `INVALID` arrow function to be parsed (yet?).
Theres more reasons for this is an `invalid` one:
- callback is parsed and `eval`ed elsewhere of scope, where you give such a callback
- so `CallbackParser` does not know `SimpleEntity` full class nameThere is more ways to 'fix' it, like:
- you can register a class map of allowed parameter types and parser could find a relevant one and
use a full class name from the map, but IMHO this could be more complex than it should be
- parser could also find a relevant class in you entire project and magically use one of the most relevant,
but it's a dark magic and I'd rather avoid it#### Question is - is it really necessary?
I dont think so. Because PHP is quite powerful (`weak`) and allows you
to use class methods of an object even if you don't know what they are.
But since the original purpose of this parser was to parse a callbacks on [Collections](https://github.com/MortalFlesh/MFCollectionsPHP),
you have other ways to know and verify a object type in parameter, so you can simply use those methods right away.```php
$list = new Generic\ListCollection(SimpleEntity::class);
$list->add(new SimpleEntity(1));
$list->add(new SimpleEntity(2));$ids = $list->map('($entity) => $entity->getId()');
var_dump($ids);
//array (size=2)
// 0 => int 1
// 1 => int 2
```## Simple simpler and complex still simply - 80:20
IMHO this parser allows you to parse simple functions simply, and you can still write a complex functions like PHP allows you.