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

create_function() is deprecated in php 7.2 #2335

Open
Shelob9 opened this issue Mar 1, 2018 · 4 comments

Comments

@Shelob9
Copy link
Collaborator

commented Mar 1, 2018

We use this for calculation fields because we can't use closures and support PHP 5.2.

I am not maintaining backwards compat with PHP 5.2 at the expense of users on PHP 7.2. You've had a decade to update, I'm sorry. Caldera Forms 1.7.0 -- IE the next major release after the one we're working on now will require PHP 5.3 or later. PHP7 for 2.0 is obviously going to be the case.

http://php.net/manual/migration72.deprecated.php

@TomasVotruba

This comment has been minimized.

Copy link

commented Dec 16, 2018

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!

@Shelob9

This comment has been minimized.

Copy link
Collaborator Author

commented Jan 18, 2019

PR #2902 should resolve this.

@Shelob9 Shelob9 modified the milestones: 1.8.1, 1.8.2, 1.9.0 Feb 27, 2019

@Shelob9

This comment has been minimized.

Copy link
Collaborator Author

commented Apr 24, 2019

Steps:
1)Unit tests for Caldera_Forms::run_calculation() with manual and non-manual formulas. We can provide data by presetting it in global $processed_data; - https://github.com/CalderaWP/Caldera-Forms/blob/master/classes/core.php#L2238 or something like this:

$fieldOneValue = 3;
		$fieldTwoValue = 4;
		$fieldThreeValue = 5;
		
		
		Caldera_Forms::set_field_data('fld1', $fieldOneValue,$form );
		Caldera_Forms::set_field_data('fld2', $fieldOneValue,$form );
		Caldera_Forms::set_field_data('fld3', $fieldOneValue,$form );
		
		$this->assertSame( $fieldOneValue + $fieldTwoValue / $fieldThreeValue, Caldera_Forms::run_calculation( null, $field, $form );)
  1. Replace create_function() with a math parser. https://github.com/denissimon/formula-parser

Ideally this:

$total_function = create_function(null, 'return ' . $formula . ';');
		$total = $total_function();

becomes:

                 $parser = new FormulaParser($formula);
		$result = $parser->getResult(); // [0 => 'done', 1 => 3.0001]
		$total = $result[1];
@Shelob9

This comment has been minimized.

Copy link
Collaborator Author

commented May 3, 2019

@New0 With tests we have now, good start, but not covering all of the functions you can use in manual formulas.

Basic Math functions

Trig

  • 1 with some of these to prove those are predictable.
  • Probably do the math for expected result with a calculator and hard-code value.
    • 'cos','sin','tan'

Anti-trig

  • 1 test with some of these functions in forumula
    • 'acos', 'asin','atan','atan2',
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.