Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function create_function() is deprecated in PHP 7.2 #55

Open
AlexanderMatveev opened this issue Nov 14, 2017 · 3 comments
Open

Function create_function() is deprecated in PHP 7.2 #55

AlexanderMatveev opened this issue Nov 14, 2017 · 3 comments

Comments

@AlexanderMatveev
Copy link

See http://php.net/manual/ru/function.create-function.php

@AlexanderMatveev AlexanderMatveev changed the title Function create_function() is deprecated in 7.2 Function create_function() is deprecated in PHP 7.2 Nov 14, 2017
@AlexanderMatveev
Copy link
Author

Deprecated: Function create_function() is deprecated
"exception" => ErrorException {
#message: "Deprecated: Function create_function() is deprecated"
#code: 0
#file: "(...)/vendor/emuravjev/mdash/src/Lib.php"
#line: 205
#severity: E_DEPRECATED
}

@Boorj
Copy link

Boorj commented May 30, 2018

All create_function instances should be replaced with lambda functions, this way:

create_function( '$m', 'return EMT_Lib::decrypt_tag($m[1]);');

//  ↓↓↓

function ($m){
    return EMT_Lib::decrypt_tag($m[1]);
}

And, for BC it could be used with wrapper:

if (version_compare(PHP_VERSION, '7.2.0', '<')) { ... }

But looks like all pull requests are merged quite rarely. So for own usage i just changed some things inside my local fork.

@TomasVotruba
Copy link

If you need to upgrade more than one create_function to anonymous function (official recommended upgrade), you can use a CLI tool for that.

It's tested on 30 various (and really weird :)) cases.

-$callback = create_function('$a', 'return "<cas:proxy>$a</cas:proxy>";');    
+$callback = function ($a) {
+    return "<cas:proxy>{$a}</cas:proxy>";
+};

Includes concat (.), string quotes and inclined function calls:

-$func = create_function('$atts, $content = null','return "<div class=\"' . $class_list . '\">" . do_shortcode($content) . "</div>";' );
+$func = function ($atts, $content = null) use ($class_list) {
+    return "<div class=\"{$class_list}\">" . do_shortcode($content) . "</div>";
+};

Do you want to automate the hard work?

1. Instal Rector

composer require rector/rector --dev

2. Create config

# rector.yml
services:
    Rector\Php\Rector\FuncCall\CreateFunctionToAnonymousFunctionRector: ~

3. Upgrade your Code

vendor/bin/rector process src --config rector.yml --dry-run
vendor/bin/rector process src --config rector.yml

Enjoy!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants