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

Better Code Design in PHP

Better Code Design in PHP

Are you tired of spending hours reading code just to find the right place to make a small change? Are you forced to split your application into several just so that devs don't step on each other's toes? This talk unites theory with practice to show you how to structure your code so that it is easy to read, to test and to maintain. You will step out with concrete ideas of how to improve your code design.

Anna Filina

November 27, 2023
Tweet

More Decks by Anna Filina

Other Decks in Programming

Transcript

  1. Better Code
    Design in PHP
    BETTERCODE() 2023 | ONLINE
    @[email protected]

    View full-size slide

  2. Anna Filina
    • Coding since 1997.
    • PHP, Java, C#, etc.
    • Legacy archaeology.
    • Test automation.
    • Talks and workshops.
    • YouTube videos.
    • Filina Consulting.

    View full-size slide

  3. Object
    Mutual reference
    Collaborator

    View full-size slide

  4. Object
    One-way reference
    Collaborator

    View full-size slide

  5. Object
    Chatty
    Collaborator
    Step 1
    Step 2
    Step 3

    View full-size slide

  6. Object
    Less chatty
    Collaborator
    Task
    Step 1
    Step 2
    Step 3

    View full-size slide

  7. Is the complexity justified?
    Object 1
    Object 2
    Object 3
    Object 4
    Bus

    View full-size slide

  8. Design Drive
    • Avoid complexity until I need it.
    • Do I understand what’s going on in terms of the business use case?

    View full-size slide

  9. Objects should speak business,
    not technical.

    View full-size slide

  10. $lineItems = $this->orders->getLineItems($pendingOrderNumber);
    $taxTotals = $this->taxes->calculateForLineItems($lineItems);

    View full-size slide

  11. Project
    • Marketplace for short-term rental:
    • Search properties for rent by city, date and number of guests.
    • Reserve & pay.

    View full-size slide

  12. ReservationService
    ::reserve()
    PaymentManager
    ReservationManager
    processPayment(data)
    Templating
    saveReservation(data)
    confirmReservation(data)
    html
    EmailManager
    payment
    savePayment(data)
    confirmReservation(html)

    View full-size slide

  13. ReservationService
    ::reserve()
    PaymentManager
    ReservationManager
    processPayment(data)
    Templating
    saveReservation(data)
    confirmReservation(data)
    html
    EmailManager
    payment
    savePayment(data)
    confirmReservation(html)

    View full-size slide

  14. MakeReservation ReservationRepository
    add(paidReservation)
    ReservationNotifier
    PaymentProcessor
    capture(charge)
    paymentConfirmation
    notifyBuyer(paidReservation)

    View full-size slide

  15. MakeReservation ReservationRepository
    add(paidReservation)
    ReservationNotifier
    PaymentProcessor
    capture(charge)
    paymentConfirmation
    notifyBuyer(paidReservation)

    View full-size slide

  16. MakeReservation ReservationRepository
    add(paidReservation)
    ReservationNotifier
    PaymentProcessor
    capture(charge)
    paymentConfirmation
    notifyBuyer(paidReservation)

    View full-size slide

  17. MakeReservation ReservationRepository
    add(paidReservation)
    notifyBuyer(paidReservation)
    ReservationNotifier
    PaymentProcessor
    capture(charge)
    paymentConfirmation

    View full-size slide

  18. MakeReservation ReservationRepository
    add(paidReservation)
    ReservationNotifier
    PaymentProcessor
    capture(charge)
    paymentConfirmation
    notifyBuyer(paidReservation)

    View full-size slide

  19. Good design needs no diagrams.

    View full-size slide

  20. Simple Code
    • No need for framework.
    • No need to design the database.
    • No need to have a UI.
    • Code the business rules.

    View full-size slide

  21. Handle HTTP Request
    Return HTTP Response
    2 adults
    in Lviv
    on Jan 1 to Jan 7
    Results containing
    “Lviv for 4”

    View full-size slide

  22. Handle HTTP Request
    Return HTTP Response
    2 adults
    in Lviv
    on Jan 1 to Jan 7
    Results containing
    “Lviv for 4”

    View full-size slide

  23. A complex system that works is
    invariably found to have evolved from a
    simple system that worked.
    — John Gall

    View full-size slide

  24. Too reusable becomes unusable.

    View full-size slide

  25. Input/output Business rules Infrastructure
    Use case

    View full-size slide

  26. PropertySearchHandler
    Lviv, Lviv Oblast, Ukraine Validate anyway.
    Convert to coordinates.
    CitySearchHandler
    Lviv Lviv, Lviv Oblast, Ukraine
    Lviv Boulevard, Oshawa, ON

    View full-size slide

  27. PropertySearchHandler
    49.8397, 24.0297 Longitude: -180 to +180
    Latitude: -90 to +90
    CitySearchHandler
    Lviv
    Lviv, Lviv Oblast, Ukraine
    49.8397
    24.0297

    View full-size slide

  28. Code is simply a model of
    the business rules and knowledge.

    View full-size slide