Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Better PHP - Keep your code up to date

wernerkrauss
September 15, 2023

Better PHP - Keep your code up to date

Talk given at https://2023.stripecon.eu

* What changed in PHP in the past few years
* Automatic code upgrades using Rector

wernerkrauss

September 15, 2023
Tweet

More Decks by wernerkrauss

Other Decks in Technology

Transcript

  1. Who Am I? • Werner „wmk“ Krauß • Freelancer •

    netwerkstatt, Bad Ischl, AT • PHP since 1998 (PHP3) • Silverstripe CMS since 2009 (SS 2.3)
  2. The „Problem“ • New PHP version every year • Language

    evolves, becomes even more mature • Cool and useful features are added • We‘re still stuck with older PHP versions • Modules should also run with older installations • No knowledge of new features
  3. What Type To You Prefer? • Return types (7.0) •

    Typed parameters • Object types, nullable types (7.1) • Typed properties (7.4) • … • Union types • Intersection types
  4. Sugar For The Baby: New, Smooth Operators • Null coalesce

    ?? (7.0) • Null coalesce asignment operator ??= (7.4)
  5. Sort Of Star Trek • Spaceship operator <=> (7.0) •

    Can even sort more params at once
  6. New Strings (Not Only For My Guitar) • str_contains() •

    str_starts_with() • str_ends_with() • More efficient and easier to use than strpos()
  7. It‘s A Match! • Match • Replaces switch or if-else

    chains • Typesafe • Kind of lookup table
  8. Named Arguments • Pass input to a method by name

    instead of argument‘s order • More flexible code • Better readability
  9. New Tools On The Block • Automation FTW • Silverstripe-ideannotator

    • PHPCS • PHPUnit • PHPStan • RectorPHP
  10. Rector: Your Friend For Updating Code • Much better than

    search/replace • Uses Abstract Syntax Tree • Lots of rules (rectors) for • upgrading language • code quality • upgrading PHPUnit … • Possible to define own rectors for special tasks
  11. Rector: Installation • Via composer as dev dependency • Big,

    monolithic bundle • Totally dependency free
  12. Create rector.php • vendor/bin/rector init • Configure it for your

    needs • Your code folders • Rules to be applied • Easier config: SetLists or LevelSetLists
  13. Rector And Silverstripe CMS • silverstripe-rector • Rectors and configurations

    for handling Silverstripe upgrades • Work in progress
  14. silverstripe-rector: A Possible Future • Silverstripe deprecations • Module deprecations

    • Updated Silverstripe code style • E.g. use arrays for sorting or filtering DataLists • Help needed to keep the rules up to date
  15. Add Rule To silverstripe-rector • Have an idea 💡 •

    Write a test ✅ • Configure rule 😊 • Add to Set 👍 • Party 🥳
  16. Links, Sources, etc… • Links: • Rector: https://getrector.com/ • silverstripe-rector:

    https://github.com/wernerkrauss/silverstripe-rector/ • Screenshots / Code Examples: • Rector Documentation • PHP Documentation • Images: • Werner Krauß, Caminho Portoguese 2023