This case study explores the implementation of the Single Responsibility Principle (SRP) in a PHP application. SRP, one of the five principles of SOLID design, states that a class should have only one reason to change. This means that each class should have a single responsibility or function in the system, making it easier to maintain, test, and refactor.
The original codebase had an OrderService class that was handling multiple responsibilities:
-
Establishing a database connection.
-
Fetching product and stock information.
-
Applying discounts.
-
Processing payments.
-
Managing the order process.
This violated the SRP because changes in the payment method, database connection, or discount logic would require modifications to the same class, increasing the risk of introducing bugs and making the class harder to maintain.
To comply with SRP, we refactored the OrderService class by separating its responsibilities into multiple classes. Each class now has a clear, singular responsibility, making the code more modular and easier to maintain.
-
Original implementation: https://github.com/thiiagoms/single-responsibility-php/commit/73ce4901e2cd570a8c8214c618dc3dc5fbf1ecda
-
Refactored Implementation: https://github.com/thiiagoms/single-responsibility-php/commit/7d60fefcfdd9422e1a2c32f2c8c555e207b5bf3f
- The code is easier to maintain because each class has a specific responsibility. If a change is needed in the payment process, only the PaymentProcessor class needs to be modified.
- Each class can be tested independently, making unit testing more straightforward.
- The application is more flexible and can easily adapt to new requirements, such as adding new payment methods, without affecting unrelated parts of the code.
- The code is more readable because each class is focused on a single aspect of the application’s functionality.
$ git clone https://github.com/thiiagoms/single-responsibility-php
$ cd single-responsibility-php
single-responsibility-php $ docker-compose up -d
single-responsibility-php $ docker-compose exec app bash
single-responsibility-php $ composer install -vvv
single-responsibility-php $ docker exec -i <mysql-container-id> mysql -u root -proot < ./.devops/mysql/script.sql
By applying the Single Responsibility Principle, we have created a more modular, maintainable, and testable codebase. This refactor allows for easier extension of functionality and better adherence to SOLID principles, resulting in a more robust and scalable application.