$30 off During Our Annual Pro Sale. View Details »

Tackling the ElePHPant in the Room

Tackling the ElePHPant in the Room

Presented on June 22nd 2023 at the Brussels PHP meetup, Brussels, Belgium.
https://www.meetup.com/brusselsphp/events/293335070/
---------------------------------------------------------------
Preparing for compatibility with a new PHP version... for some of us it's something to spend some time on every few years or so, for others, like those of us managing open source projects, it's a yearly chore which can take months of work.
PHPCompatibility is one of the tools which can help you find PHP cross-version incompatibilities.
In this talk, Juliette will give an introduction on how PHPCompatibility can help you, but more importantly, will take you behind the scenes and give you some insight into the enormous impact the plethora of syntax enhancements in PHP itself has on tooling like PHPCompatibility.
Fasten your seatbelt and prepare for a wild ride!

Juliette Reinders Folmer

June 22, 2023
Tweet

More Decks by Juliette Reinders Folmer

Other Decks in Programming

Transcript

  1. Tackling the ElePHPant
    in the Room
    Juliette Reinders Folmer Tweet about it: @jrf_nl #BrusselsPHP
    Rolf Dobberstein

    View Slide

  2. Update vs Upgrade

    View Slide

  3. Making Your Application Compatible
    Tests

    View Slide

  4. Your Tests
    Are
    Not Good Enough

    View Slide

  5. Making Your Application Compatible
    Tests
    PHPCS /
    CS-Fixer
    PHPStan /
    Psalm
    Rector
    PHP
    Compatibility

    View Slide

  6. PHPCompatibility ▪ External standard for
    PHP_CodeSniffer
    ▪ Started in 2010
    by Wim Godden
    ▪ Currently 155 sniffs

    View Slide

  7. PHPCompatibility ▪ Deprecated: warning
    ▪ Removed: error
    ▪ New: error
    Modular error codes for
    optimal configurability

    View Slide

  8. Installing PHPCompatibility
    composer config
    allow-plugins.dealerdirect/phpcodesniffer-composer-installer true
    composer require –dev
    phpcompatibility/php-compatibility:"^10.0"

    View Slide

  9. Running PHPCompatibility
    [vendor/bin/]phpcs –ps .
    --standard=PHPCompatibility
    --runtime-set testVersion 7.3-
    [--extensions=php]
    [--report=full,source]

    View Slide

  10. TestVersion ?
    Fixed testVersion 7.3
    Range testVersion 7.3-8.1
    Up to testVersion -8.1
    Minimum
    and up
    testVersion 7.3-
    Barbary Dougherty

    View Slide

  11. Demo ?

    View Slide

  12. PHPCompatibilityAll
    PHPCompatibility
    PHPCompatibility
    Symfony
    PHPCompatibility
    Paragonie
    PHPCompatibility
    PasswordCompat
    PHPCompatibility
    Joomla
    PHPCompatibility
    WP

    View Slide

  13. PHP
    and
    PHPCompatibility
    luxstorm

    View Slide

  14. PHP PHPCompatibility

    View Slide

  15. PHP 8.2 Partially Supported Callables Deprecation
    "self::method"
    "parent::method"
    "static::method"
    ["self", "method"]
    ["parent", "method"]
    ["static", "method"]
    ["Foo", "Bar::method"]
    [new Foo, "Bar::method"]
    call_user_func($callback, $param);
    $callback()

    View Slide

  16. Fixed of not ?
    class Foo {
    public function bar() {
    // Do something conditionally if a child class
    // has implemented a certain method.
    if (is_callable('static::methodName')) {
    call_user_func('static::methodName');
    static::methodName();
    }
    }
    }

    View Slide

  17. PHP PHPCompatibility

    View Slide

  18. A Look Behind
    The Curtain
    Marina Bridger

    View Slide

  19. PHPCompatibility
    PHP Tokenizer
    PHP_CodeSniffer
    PHPCompatibility
    Composer PHPCS Plugin

    View Slide

  20. PHPCompatibility 10.0+
    PHP Tokenizer
    PHP_CodeSniffer
    PHPCSUtils
    PHPCompatibility
    Composer PHPCS Plugin

    View Slide

  21. Limitations
    ▪ Context: current file
    only
    ▪ No access to runtime
    values (variables)
    Stefan Parnet

    View Slide

  22. Getting Ready: "Normal" Project
    Ensure
    dependencies are
    compatible
    Make own code
    compatible

    View Slide

  23. Getting Ready: PHP_CodeSniffer
    Adjust existing
    sniffs to allow for
    new PHP version
    Backfill/Polyfill
    new syntaxes
    Ensure
    dependencies are
    compatible
    Make own code
    compatible

    View Slide

  24. Getting Ready: PHPCompatibility
    Detect new
    incompatibilities
    Adjust existing
    sniffs to allow for
    new PHP version
    Ensure
    dependencies are
    compatible
    Make own code
    compatible

    View Slide

  25. New syntaxes/tokenizer changes
    PHP 5.5 5.6 7.0 7.1 7.2 7.3 7.4 8.0 8.1 8.2
    New
    operators
    2 3 1 1 1
    New
    constructs
    4 1 6 1 1 1 9 4 2
    Other
    relevant
    changes
    1 1 1 4 2 2 2 5 4 3

    View Slide

  26. Impact of New Syntaxes [1]
    // Deprecated PHP 7.4, removed PHP 8.0.
    is_real($var);
    // False positive prevention:
    $this->is_real($var);
    MyClass::is_real($var);
    MyNamespace\is_real($var);
    // PHP 8.0+ nullsafe object operator…
    $obj?->is_real($var);

    View Slide

  27. Impact of New Syntaxes [2]
    class Foo {
    // PHP 7.4+ Typed property.
    public string $propA;
    // PHP 7.0+ Scalar type declaration,
    // PHP 7.1+ nullable type.
    public function bar(?int $param) {}
    // Oops...
    public function __construct(public ?int $propB) {}
    }

    View Slide

  28. Impact of New Syntaxes [3]
    // PHP 8.1+ new hash algorithm.
    hash_init('xxh64');
    // Argh...!
    hash_init(
    flags: HASH_HMAC,
    key: 'mysecret',
    algo: 'xxh64',
    );

    View Slide

  29. What's Next ?
    ▪ PHPCompatibility 10.0
    expected soon(ish)
    ▪ New contributors
    (and sponsors!)
    welcome!
    Ben Kerckx

    View Slide

  30. Thanks!
    Any
    questions ?
    Slides: https://speakerdeck.com/jrf
    Feedback: https://joind.in/talk/dafa1
    @jrf_nl @jrfnl
    @[email protected]

    View Slide