Slide 1

Slide 1 text

Better PHP Keep your code up to date

Slide 2

Slide 2 text

Who Am I? • Werner „wmk“ Krauß • Freelancer • netwerkstatt, Bad Ischl, AT • PHP since 1998 (PHP3) • Silverstripe CMS since 2009 (SS 2.3)

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

What‘s New In PHP since 5.6

Slide 5

Slide 5 text

Let‘s Dive Into It Look at a practical example

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

Sugar For The Baby: New, Smooth Operators • Null coalesce ?? (7.0) • Null coalesce asignment operator ??= (7.4)

Slide 8

Slide 8 text

Sort Of Star Trek • Spaceship operator <=> (7.0) • Can even sort more params at once

Slide 9

Slide 9 text

New, Safe Operators • Nullsafe operator ?-> (8.0)

Slide 10

Slide 10 text

New Strings (Not Only For My Guitar) • str_contains() • str_starts_with() • str_ends_with() • More efficient and easier to use than strpos()

Slide 11

Slide 11 text

Romantic PHP

Slide 12

Slide 12 text

It‘s A Match! • Match • Replaces switch or if-else chains • Typesafe • Kind of lookup table

Slide 13

Slide 13 text

Arrow Functions • More functional coding style • Somewhat similar to JavaScript

Slide 14

Slide 14 text

Built In Interfaces • Stringable • Iterable

Slide 15

Slide 15 text

Named Arguments • Pass input to a method by name instead of argument‘s order • More flexible code • Better readability

Slide 16

Slide 16 text

Constructor Property Promotion

Slide 17

Slide 17 text

Attributes • Structured, machine readable metadata on declarations • Can be inspected by ReflectionAPI

Slide 18

Slide 18 text

Faul Fauler EDV-ler The tale of the lazy IT expert

Slide 19

Slide 19 text

New Tools On The Block • Automation FTW • Silverstripe-ideannotator • PHPCS • PHPUnit • PHPStan • RectorPHP

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

Rector: Installation • Via composer as dev dependency • Big, monolithic bundle • Totally dependency free

Slide 22

Slide 22 text

Create rector.php • vendor/bin/rector init • Configure it for your needs • Your code folders • Rules to be applied • Easier config: SetLists or LevelSetLists

Slide 23

Slide 23 text

Rector And Silverstripe CMS

Slide 24

Slide 24 text

Rector And Silverstripe CMS • silverstripe-rector • Rectors and configurations for handling Silverstripe upgrades • Work in progress

Slide 25

Slide 25 text

silverstripe-rector: So Far • Add table-name to DataObjects if missing • Use Foo::create() instead of new Foo()

Slide 26

Slide 26 text

silverstripe-rector: Satisfy PHPStan • Change ->owner to ->getOwner() • Add @config to configuration properties

Slide 27

Slide 27 text

Let‘s Go! 2^8+2km

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

Rules And More

Slide 30

Slide 30 text

Add Rule To silverstripe-rector • Have an idea 💡 • Write a test ✅ • Configure rule 😊 • Add to Set 👍 • Party 🥳

Slide 31

Slide 31 text

The Future Is Here

Slide 32

Slide 32 text

The Future Is Automated

Slide 33

Slide 33 text

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