Writing PHP code is easy! 👌
Not easy 🤔
legacy legacy legacy
😭
The plan 📝
1. Realize the problems 2. Understand what to do and how to do 3. Celebrate success 🍻
The problems 😬
Problems - They do too much God classes - They hold too much - Difficult to read, maintain and extend - Responsibility is too wide - Violates SRP
Problems God classes
Problems - They do too much Long functions - They hold too much - Difficult to read, maintain and extend - Responsibility is too wide - Violates SRP
Problems - Familiar parts of code in several places Code duplication - Need to apply the changes in several places - Forged to apply somewhere - inconsistent system - Leads to bugs
Problems Code duplication
Problems Code duplication
Problems - No standards for a structure Poor structure - Difficulty in Understanding - Maintenance Challenges - Reinvent the wheel
Problems - No documentation Bad documentation - Outdated documentation - Difficult to understanding the system - Onboarding Challenges - Maintainability issues
Problems - Difficult to understand Lack of modularity - Difficult to extend - Difficult to maintain
BUGS
Software Development Costs: Bugfixing https://blog.pdark.de/2012/07/21/software-development-costs-bugfixing/
What to do 🤔
No right way
Each team has its own practices
Our Superhero Approaches
4. Make it platform agnostic 2. Understand responsibilities 3. Split by purpose 6. Documentation 1. Follow Coding Standards 5. Apply Design Patterns List of approaches 7. Cover it by Tests
Follow Coding Standards
Which one?
4. Copy Paste Detector (phpcpd) 2. PSR-1, PSR-2, PSR-12 3. PHPStan, Psalm 6. CI/CD is a must 1. PHP Code Style (phpcs) 5. PHP Mess Detector (phpmd) Must use 7. Rector, PHP CS Fixer 8. …
Understand responsibilities
4. Low coupling, high cohesion! 2. What is the role of the class? 3. KISS 💋 (and focused) 1. What does the class do? Think about
1. Easy to read 2. Easy to maintain 3. Easy to reuse 4. Easy to test Benefits of focused components
Split by purpose or type
- Entity - Value Object - Repository - DTO Most used types - Validator - Data Provider - Mapper - Service
Service
4. Unaware of context 2. Stateless Operations 3. Сover one domain use-case 1. Domain-Focused Logic Service
Wrong Service contract
Right Service contract
Platform-agnostic code
1. Difficult to replace framework 2. Difficult to replace environment 3. Difficult to split by module 4. Code lives as long as framework Problems
Apply Design Patterns
https://codeopinion.com/design-patterns-who-gives-a/ Patterns usage
4. Use proper naming 2. Think twice before use 3. Focus on commonly used patterns 1. Be careful! 5. Anti-Patterns Patterns
Documentation
- Docs near the code 2. Docs as Code - Docs under version control 1. Avoid meaningless PHPDocs 3. Document interaction with integrations Documentation 4. Architecture Decision Record
Cover by tests
4. Think as a test 2. Tests are the part of documentation 3. Safe refactoring 1. Early find problems Tests
Software Development Costs: Bugfixing https://blog.pdark.de/2012/07/21/software-development-costs-bugfixing/
2. Component tests 3. Integration tests 1. Unit tests Commonly used tests types
Cubertura Test Coverage Report
Pros and Cons
2. Easy to read and understand 3. Reduced Bugs and Errors 1. Maintainability 4. Scalability Pros
2. Learning 3. Over-Engineering 1. Initial Overhead 4. Initial Investment in Tooling Cons
Success 🚀
Q & A

"PHP Code Avengers: Structuring, Refactoring, and Superhero Practices", Vladyslav Sikailo