Tools for writing
state-of-the-art PHP code
Carsten Windler
Code
is
beautiful
Carsten Windler
Principal Engineer @
https://www.linkedin.com/in/cwindler
https://carstenwindler.de
https://plana.earth
What will we talk about today?
- Coding standards
- Static code analysis
- Local pipeline
- Automated refactoring
- Dealing with legacy code
Coding standards
- Lower cognitive frictions
- Fewer code changes
- Focus on the important stuff
https://www.pexels.com/photo/white-ruled-paper-99562/
Which standard to chose?
- Doesn't matter so much
- Checked by tools
- PSR-12
Which tool to use?
- Automation is key
- Force coding standards
- Fixer
PHP-CS-Fixer
Why use an extra tool for it?
PHP-CS-Fixer
- Analyse code without executing it
- Identify issues
- Bugs
- Security vulnerabilities
- Maintainability problems
- Performance issues
- Early in the process
- i.e. local environment
Static code analysis
Costs of fixing a bug
- Pretty popular
- Easy to configure
- Strictness levels (0-9)
- Baseline feature
- https://phpstan.org/try
PHPStan
PHPStan
PHPStan
class Vat
{
private float $vat = 0.19;
public function getVat(): int
{
return $this->vat;
}
}
class OrderPosition
{
public function getGrossPrice(float $netPrice): float
{
$vatModel = new Vat();
$vat = $vatModel->getVat();
return $netPrice * (1 + $vat);
}
}
$orderPosition = new OrderPosition();
echo $orderPosition->getGrossPrice(100);
- Prevent code messing up
- Rulesets, e.g.
- Clean code
- Code size
- Naming conventions
- Controversial
- Baseline feature
PHPMD
PHPMD
phpmd
class ExampleClass
{
public function loadData(int $id): void
{
$user = User::find($id);
$test = new Order();
$order = $test->find($id);
// ....
}
}
Others
IDE extensions
IDE extensions
IDE extensions
- Overall code quality
- Prioritize refactoring
- Prevent bad code
- Don't overdo
Code metrics
Why bother?
- Manual execution is hard 🤯
- Typical excuses:
- Deadlines 🤷🏻♂️
- "Forgot" 🤷🏻♂️
- Doesn't work for me 🤷🏻♂️
- Too slow 🤷🏻♂️
- I don't like it 🤷🏻♂️
- It's useless crap anyway 🤷🏻♂️
Local pipeline
👉 Automate!
- run checks before commit
- fail commit if violations are detected
- checks ordered by execution speed:
- (PHP linter)
- Code sniffer
- PHP-CS-Fixer
- Static code analysis
- PHPStan
- Unit tests
- Other tests
pre-commit hook
Captain Hook
{
"config": {
"fail-on-first-error": true
},
"pre-commit": {
"enabled": true,
"actions": [
{
"action": "vendor/bin/php-cs-fixer fix --dry-run"
},
{
"action": "vendor/bin/phpstan"
}
]
}
}
- Local pipeline can be skipped
- Typical excuses:
- Doesn't work for me 🤷🏻♂️
- I didn't get any errors 🤷🏻♂️
- I don't know why it didn't run 🤷🏻♂️
- Deadlines, crap, etc 🤷🏻♂️
- We have to ensure that code base is kept in good shape
- Make sure to implement checks on CI as well
Outlook: Continuous Integration
- Rector
- PHP upgrades & downgrades
- Code cleanup
- Add type declaration
- and much more!
- Rules overview
- https://getrector.com/demo
Automated refactoring
Rector
- Coding standard can be fixed in one go
- Start low, improve over time
- PHPStan has rule levels
- Automated refactoring
- Psalm
- Rector
- apply on small parts of the code first
Legacy code
- Baseline feature
- current issues will be ignored
- only new issues will be reported
- auto-generate
- downside: issues don't get fixed
Legacy code
parameters:
ignoreErrors:
-
message: "#^Method Vat\\:\\:getVat\\(\\) should return int but returns float\\.$#"
count: 1
path: phpstan/example.php
- Don't overdo
- tools sometimes contradict each other
- issues too hard to fix
- Tools require time to configure & fine tune
- Focus on one or two
- Don't overwhelm the team
How much is too much?
Thank you!
https://www.pexels.com/photo/empty-photo-frame-hanging-above-cabinet-in-light-room-6373506/
This is a great book!
⭐⭐⭐⭐⭐
- Carsten W.
👉 Buy it on Amazon 👈
Clean Code in PHP
As an AI, I am not allowed
to write fake book reviews
⭐⭐⭐⭐⭐⭐⭐
- Chad Geepety
Let's connect
Carsten Windler
PhpMetrics
- Code quality metrics
- Nice looking reports
- Quite complex
PhpMetrics
PHP Insights
👉 somewhere between static code analysis and metrics
PHP Depend
👉 crazy number crunching
Other tools
With a little help from my friends
By Carsten Windler
With a little help from my friends
- 200