Skip to content

Implements SRP - Single Responsibility principle of S.O.L.I.D in PHP

Notifications You must be signed in to change notification settings

thiiagoms/single-responsibility-php

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Case Study: Single Responsibility Principle (SRP)

Overview

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.

Problem Statement

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.

Refactoring Process

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.

Benefits of SRP

1. Maintainability

  • 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.

2. Testability

  • Each class can be tested independently, making unit testing more straightforward.

3. Flexibility

  • 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.

4. Readability

  • The code is more readable because each class is focused on a single aspect of the application’s functionality.

Use application:

$ 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 

Conclusion

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.

About

Implements SRP - Single Responsibility principle of S.O.L.I.D in PHP

Topics

Resources

Stars

Watchers

Forks