2017 © Pedro Peláez
 

library callback-parser

PHP parser for arrow functions

image

mf/callback-parser

PHP parser for arrow functions

  • Monday, July 16, 2018
  • by MortalFlesh
  • Repository
  • 1 Watchers
  • 0 Stars
  • 3,719 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 7 Versions
  • 53 % Grown

The README.md

CallbackParser

Latest Stable Version Build Status Coverage Status Total Downloads License, (*1)

PHP parser for arrow functions, (*2)

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, (*3)

Table of Contents

Requirements

Installation:

composer require mf/callback-parser

Arrow Functions

Usage:

$callbackParser = new CallbackParser();
$callback = $callbackParser->parseArrowFunction('($a, $b) => $a + $b');

var_dump($callback(2, 3));  // int 5

With Custom Exception

$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., (*4)

Namespaces of parameters

For example namespace of class for given parameter type., (*5)

(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 evaled elsewhere of scope, where you give such a callback - so CallbackParser does not know SimpleEntity full class name, (*6)

There 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, (*7)

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, you have other ways to know and verify a object type in parameter, so you can simply use those methods right away., (*8)

$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., (*9)

The Versions

16/07 2018

dev-master

9999999-dev

PHP parser for arrow functions

  Sources   Download

MIT

The Requires

 

The Development Requires

by Petr Chromec

16/07 2018

2.0.2

2.0.2.0

PHP parser for arrow functions

  Sources   Download

MIT

The Requires

 

The Development Requires

by Petr Chromec

16/07 2018

dev-feature/update-dependencies

dev-feature/update-dependencies

PHP parser for arrow functions

  Sources   Download

MIT

The Requires

 

The Development Requires

by Petr Chromec

11/06 2018

2.0.1

2.0.1.0

PHP parser for arrow functions

  Sources   Download

MIT

The Requires

 

The Development Requires

by Petr Chromec

16/06 2017

2.0.0

2.0.0.0

PHP parser for arrow functions

  Sources   Download

MIT

The Requires

 

The Development Requires

by Petr Chromec

05/09 2016

1.0.1

1.0.1.0

PHP parser for arrow functions

  Sources   Download

MIT

The Requires

  • php >=5.5 || >=7.0

 

The Development Requires

by Petr Chromec

05/09 2016

1.0.0

1.0.0.0

PHP parser for arrow functions

  Sources   Download

MIT

The Requires

  • php >=5.5 || >=7.0

 

The Development Requires

by Petr Chromec