SlideShare a Scribd company logo
1 of 54
Tackling
Tech Debt
with Rector
Michele Orselli
Eng Manager@Spreaker
_orso_
micheleorselli
michele.orselli@spreaker.com
● Spreaker is a podcast platform
● Within Spreaker, podcasts can be
hosted, distributed, monetized,
discovered, and listened to
● Part of iHeartMedia family since
2020
🤓 Expert teams can tame it, choosing willingly
to accumulate it
→ trade off cleaner solution with a good
enough one
Tech Debt
🐥 Green teams accumulate it unconsciously
→ let's try this and that
Never saw a project without it 🙄
The mere fact that
we are writing code
exposes us to tech debt
Tech Debt
-> Language Evolves
-> Libraries evolve
-> Frameworks evolve
-> Things are (re)discovered
How can we deal with it?
Invest 🕒 and ⚡
to repay the debt
😫 Sometimes it is hard
to justify the investment
🤔 Is there a way to get away
with it?
Tech Debt
Performs automated refactorings on a codebase
It provides a list of predefined rectors, aka rules, refactorings you can choose
to apply on your codebase, via a rector.php config file. E.g.
- Change a property access with a method call
from $obj->property to $obj->method()
- Substitute class invocation with another one
from Banana::method() to Apple::anotherMethod()
Rector
Provided a ton of rules out of the box
- Code style/quality
- Manipulate properties/methods
- Upgrade language version
- Upgrade framework version
Rector
https://github.com/rectorphp/rector/blob/main/docs/rector_rules_overview.md
Why do I need it?
-> Regexp are not enough?
-> Search and replace is not enough?
Rectors parses the code, so it can perform complex manipulation. For this
reason it is also unaffected by code formatting (spaces, newlines, …)
Rector
Installation/configuration
$ composer require rector/rector --dev
If you create custom rules, configure composer.json to enable autoload
"autoload-dev": {
"psr-4": {
"UtilsRector": "tools/php/rector/src",
"UtilsRectorTests": "tools/php/rector/tests"
}
}
Installation/configuration
Create an empty rector.php file in the project root directory
$ vendor/bin/rector init
Change rector.php to specify which directories should be scanned and which
rules should be applied
rector.php
<?php
return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([
__DIR__ . '/apps',
__DIR__ . '/lib',
__DIR__ . '/test',
]);
$rectorConfig->rule(AddContainerAwareIfNotPresent::class);
$rectorConfig->rule(MigrateSpUtilStaticCallsRector::class);
};
Run
In the project root directory run
$ vendor/bin/rector process
If you want to see a preview of the changes without touching the files
$ vendor/bin/rector process --dry-run
Rules
RectorPhp74RectorPropertyTypedPropertyRector
TypedPropertyRector
RectorPhp80RectorClass_StringableForToStringRector
StringableForToStringRector
RectorPhp82RectorFuncCallUtf8DecodeEncodeToMbConvertEncodingRector
Utf8DecodeEncodeToMbConvertEncodingRector
Configurable Rules
Rule accepting configuration params
RectorTransformRectorClass_AddAllowDynamicPropertiesAttributeRector
AddAllowDynamicPropertiesAttributeRector
SetLists
Define a group of rules to avoid specify them one by one
rector.php
<?php
return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([
__DIR__ . '/apps',
__DIR__ . '/lib',
__DIR__ . '/test',
]);
$rectorConfig->sets([SetList::DEAD_CODE, SetList::PHP_82]);
};
LevelSetLists
Applies all the rules up to a certain PHP version
rector.php
<?php
return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([
__DIR__ . '/apps',
__DIR__ . '/lib',
__DIR__ . '/test',
]);
$rectorConfig->sets([LevelSetList::UP_TO_PHP_82]);
};
1. Loads configuration from rector.php
2. Iterates all files
3. For each file
a. Preparation phase: parse file, creates AST (array of nodes), add some
metadata
b. Rectify Phase
i. Traverse AST
ii. Check if rectors can be applied to that node
iii. If yse, apply
c. Save/Diff phase
How it works
A rule is a PHP class that extends AbstractRector
Implement 3 method
Writing Custom Rules
public function getRuleDefinition(): RuleDefinition
public function getNodeTypes(): array
public function refactor(Node $node): ?Node
Life of a PHP file 🐘
<?php
namespace AppTime;
class Clock
{
public function now()
{
return date('Y-m-d');
}
}
<?php
namespace AppTime;
class Clock
{
public function now()
{
return date('Y-m-d');
}
}
T_OPEN_TAG ('<?php')
T_NAMESPACE ('namespace')
T_WHITESPACE (' ')
T_STRING ('App')
T_NS_SEPARATOR ('')
T_STRING ('Time')
T_WHITESPACE (' ')
T_CLASS ('class')
T_WHITESPACE (' ')
T_STRING ('Clock')
T_WHITESPACE (' ')
T_WHITESPACE (' ')
T_PUBLIC ('public')
T_WHITESPACE (' ')
T_FUNCTION ('function')
T_WHITESPACE (' ')
T_STRING ('now')
T_WHITESPACE (' ')
T_WHITESPACE ('')
T_RETURN ('return')
T_WHITESPACE (' ')
T_STRING ('date')
T_CONSTANT_ENCAPSED_STRING
(''Y-m-d'')
T_WHITESPACE ('')
T_WHITESPACE ('')
T_WHITESPACE ('')
Tokenizer
<?php
namespace AppTime;
class Clock
{
public function now()
{
return date('Y-m-d');
}
}
T_OPEN_TAG ('<?php')
T_NAMESPACE ('namespace')
T_WHITESPACE (' ')
T_STRING ('App')
T_NS_SEPARATOR ('')
T_STRING ('Time')
T_WHITESPACE (' ')
T_CLASS ('class')
T_WHITESPACE (' ')
T_STRING ('Clock')
T_WHITESPACE (' ')
T_WHITESPACE (' ')
T_PUBLIC ('public')
T_WHITESPACE (' ')
T_FUNCTION ('function')
T_WHITESPACE (' ')
T_STRING ('now')
T_WHITESPACE (' ')
T_WHITESPACE ('')
T_RETURN ('return')
T_WHITESPACE (' ')
T_STRING ('date')
T_CONSTANT_ENCAPSED_STRING
(''Y-m-d'')
T_WHITESPACE ('')
T_WHITESPACE ('')
T_WHITESPACE ('')
Tokenizer
<?php
namespace AppTime;
class Clock
{
public function now()
{
return date('Y-m-d');
}
}
T_OPEN_TAG ('<?php')
T_NAMESPACE ('namespace')
T_WHITESPACE (' ')
T_STRING ('App')
T_NS_SEPARATOR ('')
T_STRING ('Time')
T_WHITESPACE (' ')
T_CLASS ('class')
T_WHITESPACE (' ')
T_STRING ('Clock')
T_WHITESPACE (' ')
T_WHITESPACE (' ')
T_PUBLIC ('public')
T_WHITESPACE (' ')
T_FUNCTION ('function')
T_WHITESPACE (' ')
T_STRING ('now')
T_WHITESPACE (' ')
T_WHITESPACE ('')
T_RETURN ('return')
T_WHITESPACE (' ')
T_STRING ('date')
T_CONSTANT_ENCAPSED_STRING
(''Y-m-d'')
T_WHITESPACE ('')
T_WHITESPACE ('')
T_WHITESPACE ('')
Tokenizer
<?php
namespace AppTime;
class Clock
{
public function now()
{
return date('Y-m-d');
}
}
T_OPEN_TAG ('<?php')
T_NAMESPACE ('namespace')
T_WHITESPACE (' ')
T_STRING ('App')
T_NS_SEPARATOR ('')
T_STRING ('Time')
T_WHITESPACE (' ')
T_CLASS ('class')
T_WHITESPACE (' ')
T_STRING ('Clock')
T_WHITESPACE (' ')
T_WHITESPACE (' ')
T_PUBLIC ('public')
T_WHITESPACE (' ')
T_FUNCTION ('function')
T_WHITESPACE (' ')
T_STRING ('now')
T_WHITESPACE (' ')
T_WHITESPACE ('')
T_RETURN ('return')
T_WHITESPACE (' ')
T_STRING ('date')
T_CONSTANT_ENCAPSED_STRING
(''Y-m-d'')
T_WHITESPACE ('')
T_WHITESPACE ('')
T_WHITESPACE ('')
Tokenizer
<?php
namespace AppTime;
class Clock
{
public function now()
{
return date('Y-m-d');
}
}
T_OPEN_TAG ('<?php')
T_NAMESPACE ('namespace')
T_WHITESPACE (' ')
T_STRING ('App')
T_NS_SEPARATOR ('')
T_STRING ('Time')
T_WHITESPACE (' ')
T_CLASS ('class')
T_WHITESPACE (' ')
T_STRING ('Clock')
T_WHITESPACE (' ')
T_WHITESPACE (' ')
T_PUBLIC ('public')
T_WHITESPACE (' ')
T_FUNCTION ('function')
T_WHITESPACE (' ')
T_STRING ('now')
T_WHITESPACE (' ')
T_WHITESPACE ('')
T_RETURN ('return')
T_WHITESPACE (' ')
T_STRING ('date')
T_CONSTANT_ENCAPSED_STRING
(''Y-m-d'')
T_WHITESPACE ('')
T_WHITESPACE ('')
T_WHITESPACE ('')
Tokenizer
<?php
namespace AppTime;
class Clock
{
public function now()
{
return date('Y-m-d');
}
}
T_OPEN_TAG ('<?php')
T_NAMESPACE ('namespace')
T_WHITESPACE (' ')
T_STRING ('App')
T_NS_SEPARATOR ('')
T_STRING ('Time')
T_WHITESPACE (' ')
T_CLASS ('class')
T_WHITESPACE (' ')
T_STRING ('Clock')
T_WHITESPACE (' ')
T_WHITESPACE (' ')
T_PUBLIC ('public')
T_WHITESPACE (' ')
T_FUNCTION ('function')
T_WHITESPACE (' ')
T_STRING ('now')
T_WHITESPACE (' ')
T_WHITESPACE ('')
T_RETURN ('return')
T_WHITESPACE (' ')
T_STRING ('date')
T_CONSTANT_ENCAPSED_STRING
(''Y-m-d'')
T_WHITESPACE ('')
T_WHITESPACE ('')
T_WHITESPACE ('')
Tokenizer
<?php
namespace AppTime;
class Clock
{
public function now()
{
return date('Y-m-d');
}
}
T_OPEN_TAG ('<?php')
T_NAMESPACE ('namespace')
T_WHITESPACE (' ')
T_STRING ('App')
T_NS_SEPARATOR ('')
T_STRING ('Time')
T_WHITESPACE (' ')
T_CLASS ('class')
T_WHITESPACE (' ')
T_STRING ('Clock')
T_WHITESPACE (' ')
T_WHITESPACE (' ')
T_PUBLIC ('public')
T_WHITESPACE (' ')
T_FUNCTION ('function')
T_WHITESPACE (' ')
T_STRING ('now')
T_WHITESPACE (' ')
T_WHITESPACE ('')
T_RETURN ('return')
T_WHITESPACE (' ')
T_STRING ('date')
T_CONSTANT_ENCAPSED_STRING
(''Y-m-d'')
T_WHITESPACE ('')
T_WHITESPACE ('')
T_WHITESPACE ('')
Tokenizer
<?php
namespace AppTime;
c
class Clock
{
public function now()
{
return date('Y-m-d');
}
}
T_OPEN_TAG ('<?php')
T_NAMESPACE ('namespace')
T_WHITESPACE (' ')
T_STRING ('App')
T_NS_SEPARATOR ('')
T_STRING ('Time')
T_WHITESPACE (' ')
T_CLASS ('class')
T_WHITESPACE (' ')
T_STRING ('Clock')
T_WHITESPACE (' ')
T_WHITESPACE (' ')
T_PUBLIC ('public')
T_WHITESPACE (' ')
T_FUNCTION ('function')
T_WHITESPACE (' ')
T_STRING ('now')
T_WHITESPACE (' ')
T_WHITESPACE ('')
T_RETURN ('return')
T_WHITESPACE (' ')
T_STRING ('date')
T_CONSTANT_ENCAPSED_STRING
(''Y-m-d'')
T_WHITESPACE ('')
T_WHITESPACE ('')
T_WHITESPACE ('')
Tokenizer
<?php
namespace AppTime;
c
class Clock
{
public function now()
{
return date('Y-m-d');
}
}
T_OPEN_TAG ('<?php')
T_NAMESPACE ('namespace')
T_WHITESPACE (' ')
T_STRING ('App')
T_NS_SEPARATOR ('')
T_STRING ('Time')
T_WHITESPACE (' ')
T_CLASS ('class')
T_WHITESPACE (' ')
T_STRING ('Clock')
T_WHITESPACE (' ')
T_WHITESPACE (' ')
T_PUBLIC ('public')
T_WHITESPACE (' ')
T_FUNCTION ('function')
T_WHITESPACE (' ')
T_STRING ('now')
T_WHITESPACE (' ')
T_WHITESPACE ('')
T_RETURN ('return')
T_WHITESPACE (' ')
T_STRING ('date')
T_CONSTANT_ENCAPSED_STRING
(''Y-m-d'')
T_WHITESPACE ('')
T_WHITESPACE ('')
T_WHITESPACE ('')
Tokenizer
<?php
namespace AppTime;
c
class Clock
{
public function now()
{
return date('Y-m-d');
}
}
T_OPEN_TAG ('<?php')
T_NAMESPACE ('namespace')
T_WHITESPACE (' ')
T_STRING ('App')
T_NS_SEPARATOR ('')
T_STRING ('Time')
T_WHITESPACE (' ')
T_CLASS ('class')
T_WHITESPACE (' ')
T_STRING ('Clock')
T_WHITESPACE (' ')
T_WHITESPACE (' ')
T_PUBLIC ('public')
T_WHITESPACE (' ')
T_FUNCTION ('function')
T_WHITESPACE (' ')
T_STRING ('now')
T_WHITESPACE (' ')
T_WHITESPACE ('')
T_RETURN ('return')
T_WHITESPACE (' ')
T_STRING ('date')
T_CONSTANT_ENCAPSED_STRING
(''Y-m-d'')
T_WHITESPACE ('')
T_WHITESPACE ('')
T_WHITESPACE ('')
Tokenizer
token_get_all
T_OPEN_TAG ('<?php')
T_NAMESPACE ('namespace')
T_WHITESPACE (' ')
T_STRING ('App')
T_NS_SEPARATOR ('')
T_STRING ('Time')
T_WHITESPACE (' ')
T_CLASS ('class')
T_WHITESPACE (' ')
T_STRING ('Clock')
T_WHITESPACE (' ')
T_WHITESPACE (' ')
T_PUBLIC ('public')
T_WHITESPACE (' ')
T_FUNCTION ('function')
T_WHITESPACE (' ')
T_STRING ('now')
T_WHITESPACE (' ')
T_WHITESPACE ('')
T_RETURN ('return')
T_WHITESPACE (' ')
T_STRING ('date')
T_CONSTANT_ENCAPSED_STRING
(''Y-m-d'')
T_WHITESPACE ('')
T_WHITESPACE ('')
T_WHITESPACE ('')
Parser
T_OPEN_TAG ('<?php')
T_NAMESPACE ('namespace')
T_WHITESPACE (' ')
T_STRING ('App')
T_NS_SEPARATOR ('')
T_STRING ('Time')
T_WHITESPACE (' ')
T_CLASS ('class')
T_WHITESPACE (' ')
T_STRING ('Clock')
T_WHITESPACE (' ')
T_WHITESPACE (' ')
T_PUBLIC ('public')
T_WHITESPACE (' ')
T_FUNCTION ('function')
T_WHITESPACE (' ')
T_STRING ('now')
T_WHITESPACE (' ')
T_WHITESPACE ('')
T_RETURN ('return')
T_WHITESPACE (' ')
T_STRING ('date')
T_CONSTANT_ENCAPSED_STRING
(''Y-m-d'')
T_WHITESPACE ('')
T_WHITESPACE ('')
T_WHITESPACE ('')
Parser
T_OPEN_TAG ('<?php')
T_NAMESPACE ('namespace')
T_WHITESPACE (' ')
T_STRING ('App')
T_NS_SEPARATOR ('')
T_STRING ('Time')
T_WHITESPACE (' ')
T_CLASS ('class')
T_WHITESPACE (' ')
T_STRING ('Clock')
T_WHITESPACE (' ')
T_WHITESPACE (' ')
T_PUBLIC ('public')
T_WHITESPACE (' ')
T_FUNCTION ('function')
T_WHITESPACE (' ')
T_STRING ('now')
T_WHITESPACE (' ')
T_WHITESPACE ('')
T_RETURN ('return')
T_WHITESPACE (' ')
T_STRING ('date')
T_CONSTANT_ENCAPSED_STRING
(''Y-m-d'')
T_WHITESPACE ('')
T_WHITESPACE ('')
T_WHITESPACE ('')
Parser
AST:
Abstract
Syntax
Tree
PHP-Parser
PHP Vm
AST:
Abstract
Syntax
Tree
Compiler
PHP Vm
AST:
Abstract
Syntax
Tree
Compiler
PHP Vm
Opcodes
AST:
Abstract
Syntax
Tree
Compiler
PHP Vm
Opcodes
AST:
Abstract
Syntax
Tree
AST - Abstract Syntax Tree
Intermediate representation of the source code
Machine friendly
Tree structure
Traversing the AST means
traversing the original source code
https://phpast.com/
https://github.com/rectorphp/php-parser-nodes-docs
PHP-Parser nodes docs
A rule is a PHP class that extends AbstractRector
Implement 3 method
Writing Custom Rules
public function getRuleDefinition(): RuleDefinition
public function getNodeTypes(): array
public function refactor(Node $node): ?Node
Look for existing rules that perform similar tasks. Use it as a starting point.
Look for helper classes: classes that perform some heavy lifting for you. E.g
pull up a property
There are plenty:
- ClassDependencyManipulator
- ClassInsertManipulator
- NodeFactory
- ParamAnalyzer
Create a small snippet of code to test it with --dry-run
Start with an existing rule
Let's 👀 at the code…
Look for a rule that does something similar to what you need and use it as a
starting point
This part is a bit time consuming, there are multiple ways to achieve the same
result
Don't try to do too much in a single rule, combine multiple rules
Writing Custom Rules: lesson learned
Get all the corner cases is hard, Pareto rulez!
NodeDumper is your friend
Create a Dummy snippet for quick testing
Writing Custom Rules: lesson learned
Extensions: composer installable set of rules for specific framework/libraries
These are provided out of the box
https://github.com/rectorphp/rector-symfony
https://github.com/rectorphp/rector-doctrine
https://github.com/rectorphp/rector-phpunit
Extensions
That's All Folks!
https://leanpub.com/rector-the-power-of-automated-refactoring
Questions?

More Related Content

Similar to Tackling Tech Debt with Rector

Workshop quality assurance for php projects - ZendCon 2013
Workshop quality assurance for php projects - ZendCon 2013Workshop quality assurance for php projects - ZendCon 2013
Workshop quality assurance for php projects - ZendCon 2013Michelangelo van Dam
 
Bring the fun back to java
Bring the fun back to javaBring the fun back to java
Bring the fun back to javaciklum_ods
 
Living With Legacy Code
Living With Legacy CodeLiving With Legacy Code
Living With Legacy CodeRowan Merewood
 
Spstc2011 managed metadata real world
Spstc2011 managed metadata real worldSpstc2011 managed metadata real world
Spstc2011 managed metadata real worldAtul Chhoda
 
Spstc2011 managed metadata real world
Spstc2011 managed metadata real worldSpstc2011 managed metadata real world
Spstc2011 managed metadata real worldAtul Chhoda
 
Workshop quality assurance for php projects - phpbelfast
Workshop quality assurance for php projects - phpbelfastWorkshop quality assurance for php projects - phpbelfast
Workshop quality assurance for php projects - phpbelfastMichelangelo van Dam
 
TYPO3 Extension development using new Extbase framework
TYPO3 Extension development using new Extbase frameworkTYPO3 Extension development using new Extbase framework
TYPO3 Extension development using new Extbase frameworkChristian Trabold
 
How to perform debounce in react
How to perform debounce in reactHow to perform debounce in react
How to perform debounce in reactBOSC Tech Labs
 
Evolving a Clean, Pragmatic Architecture at JBCNConf 2019
Evolving a Clean, Pragmatic Architecture at JBCNConf 2019Evolving a Clean, Pragmatic Architecture at JBCNConf 2019
Evolving a Clean, Pragmatic Architecture at JBCNConf 2019Victor Rentea
 
What's New In Laravel 5
What's New In Laravel 5What's New In Laravel 5
What's New In Laravel 5Darren Craig
 
[xp2013] Narrow Down What to Test
[xp2013] Narrow Down What to Test[xp2013] Narrow Down What to Test
[xp2013] Narrow Down What to TestZsolt Fabok
 
Introducing PHP Latest Updates
Introducing PHP Latest UpdatesIntroducing PHP Latest Updates
Introducing PHP Latest UpdatesIftekhar Eather
 
What's new in Doctrine
What's new in DoctrineWhat's new in Doctrine
What's new in DoctrineJonathan Wage
 
Reactive programming with RxJS - Taiwan
Reactive programming with RxJS - TaiwanReactive programming with RxJS - Taiwan
Reactive programming with RxJS - Taiwanmodernweb
 
Code decoupling from Symfony (and others frameworks) - PHP Conference Brasil ...
Code decoupling from Symfony (and others frameworks) - PHP Conference Brasil ...Code decoupling from Symfony (and others frameworks) - PHP Conference Brasil ...
Code decoupling from Symfony (and others frameworks) - PHP Conference Brasil ...Miguel Gallardo
 
course slides -- powerpoint
course slides -- powerpointcourse slides -- powerpoint
course slides -- powerpointwebhostingguy
 

Similar to Tackling Tech Debt with Rector (20)

Workshop quality assurance for php projects - ZendCon 2013
Workshop quality assurance for php projects - ZendCon 2013Workshop quality assurance for php projects - ZendCon 2013
Workshop quality assurance for php projects - ZendCon 2013
 
Bring the fun back to java
Bring the fun back to javaBring the fun back to java
Bring the fun back to java
 
Living With Legacy Code
Living With Legacy CodeLiving With Legacy Code
Living With Legacy Code
 
Dependency Injection
Dependency InjectionDependency Injection
Dependency Injection
 
Spstc2011 managed metadata real world
Spstc2011 managed metadata real worldSpstc2011 managed metadata real world
Spstc2011 managed metadata real world
 
Spstc2011 managed metadata real world
Spstc2011 managed metadata real worldSpstc2011 managed metadata real world
Spstc2011 managed metadata real world
 
Workshop quality assurance for php projects - phpbelfast
Workshop quality assurance for php projects - phpbelfastWorkshop quality assurance for php projects - phpbelfast
Workshop quality assurance for php projects - phpbelfast
 
TYPO3 Extension development using new Extbase framework
TYPO3 Extension development using new Extbase frameworkTYPO3 Extension development using new Extbase framework
TYPO3 Extension development using new Extbase framework
 
How to perform debounce in react
How to perform debounce in reactHow to perform debounce in react
How to perform debounce in react
 
Evolving a Clean, Pragmatic Architecture at JBCNConf 2019
Evolving a Clean, Pragmatic Architecture at JBCNConf 2019Evolving a Clean, Pragmatic Architecture at JBCNConf 2019
Evolving a Clean, Pragmatic Architecture at JBCNConf 2019
 
Symfony 1, mi viejo amigo
Symfony 1, mi viejo amigoSymfony 1, mi viejo amigo
Symfony 1, mi viejo amigo
 
Magento code audit
Magento code auditMagento code audit
Magento code audit
 
What's New In Laravel 5
What's New In Laravel 5What's New In Laravel 5
What's New In Laravel 5
 
[xp2013] Narrow Down What to Test
[xp2013] Narrow Down What to Test[xp2013] Narrow Down What to Test
[xp2013] Narrow Down What to Test
 
Introducing PHP Latest Updates
Introducing PHP Latest UpdatesIntroducing PHP Latest Updates
Introducing PHP Latest Updates
 
Event Sourcing with php
Event Sourcing with phpEvent Sourcing with php
Event Sourcing with php
 
What's new in Doctrine
What's new in DoctrineWhat's new in Doctrine
What's new in Doctrine
 
Reactive programming with RxJS - Taiwan
Reactive programming with RxJS - TaiwanReactive programming with RxJS - Taiwan
Reactive programming with RxJS - Taiwan
 
Code decoupling from Symfony (and others frameworks) - PHP Conference Brasil ...
Code decoupling from Symfony (and others frameworks) - PHP Conference Brasil ...Code decoupling from Symfony (and others frameworks) - PHP Conference Brasil ...
Code decoupling from Symfony (and others frameworks) - PHP Conference Brasil ...
 
course slides -- powerpoint
course slides -- powerpointcourse slides -- powerpoint
course slides -- powerpoint
 

More from Michele Orselli

A recommendation engine for your applications codemotion ams
A recommendation engine for your applications codemotion amsA recommendation engine for your applications codemotion ams
A recommendation engine for your applications codemotion amsMichele Orselli
 
A recommendation engine for your applications phpday
A recommendation engine for your applications phpdayA recommendation engine for your applications phpday
A recommendation engine for your applications phpdayMichele Orselli
 
Hopping in clouds - phpuk 17
Hopping in clouds - phpuk 17Hopping in clouds - phpuk 17
Hopping in clouds - phpuk 17Michele Orselli
 
A recommendation engine for your php application
A recommendation engine for your php applicationA recommendation engine for your php application
A recommendation engine for your php applicationMichele Orselli
 
Symfony e micro (non così tanto) services
Symfony e micro (non così tanto) servicesSymfony e micro (non così tanto) services
Symfony e micro (non così tanto) servicesMichele Orselli
 
Hopping in clouds: a tale of migration from one cloud provider to another
Hopping in clouds: a tale of migration from one cloud provider to anotherHopping in clouds: a tale of migration from one cloud provider to another
Hopping in clouds: a tale of migration from one cloud provider to anotherMichele Orselli
 
Vagrant for real (codemotion rome 2016)
Vagrant for real (codemotion rome 2016)Vagrant for real (codemotion rome 2016)
Vagrant for real (codemotion rome 2016)Michele Orselli
 
Vagrant for real codemotion (moar tips! ;-))
Vagrant for real codemotion (moar tips! ;-))Vagrant for real codemotion (moar tips! ;-))
Vagrant for real codemotion (moar tips! ;-))Michele Orselli
 
Implementing data sync apis for mibile apps @cloudconf
Implementing data sync apis for mibile apps @cloudconfImplementing data sync apis for mibile apps @cloudconf
Implementing data sync apis for mibile apps @cloudconfMichele Orselli
 
Server side data sync for mobile apps with silex
Server side data sync for mobile apps with silexServer side data sync for mobile apps with silex
Server side data sync for mobile apps with silexMichele Orselli
 
Continuous, continuous, continuous
Continuous, continuous, continuousContinuous, continuous, continuous
Continuous, continuous, continuousMichele Orselli
 
Deploy a PHP App on Google App Engine
Deploy a PHP App on Google App EngineDeploy a PHP App on Google App Engine
Deploy a PHP App on Google App EngineMichele Orselli
 
Implementing Server Side Data Synchronization for Mobile Apps
Implementing Server Side Data Synchronization for Mobile AppsImplementing Server Side Data Synchronization for Mobile Apps
Implementing Server Side Data Synchronization for Mobile AppsMichele Orselli
 
Deploy a php app on Google App Engine
Deploy a php app on Google App EngineDeploy a php app on Google App Engine
Deploy a php app on Google App EngineMichele Orselli
 
Manage a project portfolio
Manage a project portfolioManage a project portfolio
Manage a project portfolioMichele Orselli
 
Developing sustainable php projects
Developing sustainable php projectsDeveloping sustainable php projects
Developing sustainable php projectsMichele Orselli
 

More from Michele Orselli (20)

A dive into Symfony 4
A dive into Symfony 4A dive into Symfony 4
A dive into Symfony 4
 
A recommendation engine for your applications codemotion ams
A recommendation engine for your applications codemotion amsA recommendation engine for your applications codemotion ams
A recommendation engine for your applications codemotion ams
 
A recommendation engine for your applications phpday
A recommendation engine for your applications phpdayA recommendation engine for your applications phpday
A recommendation engine for your applications phpday
 
Hopping in clouds - phpuk 17
Hopping in clouds - phpuk 17Hopping in clouds - phpuk 17
Hopping in clouds - phpuk 17
 
A recommendation engine for your php application
A recommendation engine for your php applicationA recommendation engine for your php application
A recommendation engine for your php application
 
Symfony e micro (non così tanto) services
Symfony e micro (non così tanto) servicesSymfony e micro (non così tanto) services
Symfony e micro (non così tanto) services
 
Hopping in clouds: a tale of migration from one cloud provider to another
Hopping in clouds: a tale of migration from one cloud provider to anotherHopping in clouds: a tale of migration from one cloud provider to another
Hopping in clouds: a tale of migration from one cloud provider to another
 
Vagrant for real (codemotion rome 2016)
Vagrant for real (codemotion rome 2016)Vagrant for real (codemotion rome 2016)
Vagrant for real (codemotion rome 2016)
 
Vagrant for real codemotion (moar tips! ;-))
Vagrant for real codemotion (moar tips! ;-))Vagrant for real codemotion (moar tips! ;-))
Vagrant for real codemotion (moar tips! ;-))
 
Migrare a Symfony 3
Migrare a Symfony 3Migrare a Symfony 3
Migrare a Symfony 3
 
Vagrant for real
Vagrant for realVagrant for real
Vagrant for real
 
Implementing data sync apis for mibile apps @cloudconf
Implementing data sync apis for mibile apps @cloudconfImplementing data sync apis for mibile apps @cloudconf
Implementing data sync apis for mibile apps @cloudconf
 
Server side data sync for mobile apps with silex
Server side data sync for mobile apps with silexServer side data sync for mobile apps with silex
Server side data sync for mobile apps with silex
 
Continuous, continuous, continuous
Continuous, continuous, continuousContinuous, continuous, continuous
Continuous, continuous, continuous
 
Deploy a PHP App on Google App Engine
Deploy a PHP App on Google App EngineDeploy a PHP App on Google App Engine
Deploy a PHP App on Google App Engine
 
Implementing Server Side Data Synchronization for Mobile Apps
Implementing Server Side Data Synchronization for Mobile AppsImplementing Server Side Data Synchronization for Mobile Apps
Implementing Server Side Data Synchronization for Mobile Apps
 
Deploy a php app on Google App Engine
Deploy a php app on Google App EngineDeploy a php app on Google App Engine
Deploy a php app on Google App Engine
 
Sf2 wtf
Sf2 wtfSf2 wtf
Sf2 wtf
 
Manage a project portfolio
Manage a project portfolioManage a project portfolio
Manage a project portfolio
 
Developing sustainable php projects
Developing sustainable php projectsDeveloping sustainable php projects
Developing sustainable php projects
 

Recently uploaded

Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...aditisharan08
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
cybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningcybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningVitsRangannavar
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyFrank van der Linden
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsMehedi Hasan Shohan
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 

Recently uploaded (20)

Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
cybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningcybersecurity notes for mca students for learning
cybersecurity notes for mca students for learning
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The Ugly
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software Solutions
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 

Tackling Tech Debt with Rector

  • 3. ● Spreaker is a podcast platform ● Within Spreaker, podcasts can be hosted, distributed, monetized, discovered, and listened to ● Part of iHeartMedia family since 2020
  • 4. 🤓 Expert teams can tame it, choosing willingly to accumulate it → trade off cleaner solution with a good enough one Tech Debt 🐥 Green teams accumulate it unconsciously → let's try this and that Never saw a project without it 🙄
  • 5. The mere fact that we are writing code exposes us to tech debt Tech Debt -> Language Evolves -> Libraries evolve -> Frameworks evolve -> Things are (re)discovered
  • 6. How can we deal with it? Invest 🕒 and ⚡ to repay the debt 😫 Sometimes it is hard to justify the investment 🤔 Is there a way to get away with it? Tech Debt
  • 7. Performs automated refactorings on a codebase It provides a list of predefined rectors, aka rules, refactorings you can choose to apply on your codebase, via a rector.php config file. E.g. - Change a property access with a method call from $obj->property to $obj->method() - Substitute class invocation with another one from Banana::method() to Apple::anotherMethod() Rector
  • 8. Provided a ton of rules out of the box - Code style/quality - Manipulate properties/methods - Upgrade language version - Upgrade framework version Rector https://github.com/rectorphp/rector/blob/main/docs/rector_rules_overview.md
  • 9. Why do I need it? -> Regexp are not enough? -> Search and replace is not enough? Rectors parses the code, so it can perform complex manipulation. For this reason it is also unaffected by code formatting (spaces, newlines, …) Rector
  • 10. Installation/configuration $ composer require rector/rector --dev If you create custom rules, configure composer.json to enable autoload "autoload-dev": { "psr-4": { "UtilsRector": "tools/php/rector/src", "UtilsRectorTests": "tools/php/rector/tests" } }
  • 11. Installation/configuration Create an empty rector.php file in the project root directory $ vendor/bin/rector init Change rector.php to specify which directories should be scanned and which rules should be applied
  • 12. rector.php <?php return static function (RectorConfig $rectorConfig): void { $rectorConfig->paths([ __DIR__ . '/apps', __DIR__ . '/lib', __DIR__ . '/test', ]); $rectorConfig->rule(AddContainerAwareIfNotPresent::class); $rectorConfig->rule(MigrateSpUtilStaticCallsRector::class); };
  • 13. Run In the project root directory run $ vendor/bin/rector process If you want to see a preview of the changes without touching the files $ vendor/bin/rector process --dry-run
  • 14. Rules
  • 18. Configurable Rules Rule accepting configuration params
  • 20. SetLists Define a group of rules to avoid specify them one by one
  • 21. rector.php <?php return static function (RectorConfig $rectorConfig): void { $rectorConfig->paths([ __DIR__ . '/apps', __DIR__ . '/lib', __DIR__ . '/test', ]); $rectorConfig->sets([SetList::DEAD_CODE, SetList::PHP_82]); };
  • 22. LevelSetLists Applies all the rules up to a certain PHP version
  • 23. rector.php <?php return static function (RectorConfig $rectorConfig): void { $rectorConfig->paths([ __DIR__ . '/apps', __DIR__ . '/lib', __DIR__ . '/test', ]); $rectorConfig->sets([LevelSetList::UP_TO_PHP_82]); };
  • 24. 1. Loads configuration from rector.php 2. Iterates all files 3. For each file a. Preparation phase: parse file, creates AST (array of nodes), add some metadata b. Rectify Phase i. Traverse AST ii. Check if rectors can be applied to that node iii. If yse, apply c. Save/Diff phase How it works
  • 25. A rule is a PHP class that extends AbstractRector Implement 3 method Writing Custom Rules public function getRuleDefinition(): RuleDefinition public function getNodeTypes(): array public function refactor(Node $node): ?Node
  • 26. Life of a PHP file 🐘
  • 27. <?php namespace AppTime; class Clock { public function now() { return date('Y-m-d'); } }
  • 28. <?php namespace AppTime; class Clock { public function now() { return date('Y-m-d'); } } T_OPEN_TAG ('<?php') T_NAMESPACE ('namespace') T_WHITESPACE (' ') T_STRING ('App') T_NS_SEPARATOR ('') T_STRING ('Time') T_WHITESPACE (' ') T_CLASS ('class') T_WHITESPACE (' ') T_STRING ('Clock') T_WHITESPACE (' ') T_WHITESPACE (' ') T_PUBLIC ('public') T_WHITESPACE (' ') T_FUNCTION ('function') T_WHITESPACE (' ') T_STRING ('now') T_WHITESPACE (' ') T_WHITESPACE ('') T_RETURN ('return') T_WHITESPACE (' ') T_STRING ('date') T_CONSTANT_ENCAPSED_STRING (''Y-m-d'') T_WHITESPACE ('') T_WHITESPACE ('') T_WHITESPACE ('') Tokenizer
  • 29. <?php namespace AppTime; class Clock { public function now() { return date('Y-m-d'); } } T_OPEN_TAG ('<?php') T_NAMESPACE ('namespace') T_WHITESPACE (' ') T_STRING ('App') T_NS_SEPARATOR ('') T_STRING ('Time') T_WHITESPACE (' ') T_CLASS ('class') T_WHITESPACE (' ') T_STRING ('Clock') T_WHITESPACE (' ') T_WHITESPACE (' ') T_PUBLIC ('public') T_WHITESPACE (' ') T_FUNCTION ('function') T_WHITESPACE (' ') T_STRING ('now') T_WHITESPACE (' ') T_WHITESPACE ('') T_RETURN ('return') T_WHITESPACE (' ') T_STRING ('date') T_CONSTANT_ENCAPSED_STRING (''Y-m-d'') T_WHITESPACE ('') T_WHITESPACE ('') T_WHITESPACE ('') Tokenizer
  • 30. <?php namespace AppTime; class Clock { public function now() { return date('Y-m-d'); } } T_OPEN_TAG ('<?php') T_NAMESPACE ('namespace') T_WHITESPACE (' ') T_STRING ('App') T_NS_SEPARATOR ('') T_STRING ('Time') T_WHITESPACE (' ') T_CLASS ('class') T_WHITESPACE (' ') T_STRING ('Clock') T_WHITESPACE (' ') T_WHITESPACE (' ') T_PUBLIC ('public') T_WHITESPACE (' ') T_FUNCTION ('function') T_WHITESPACE (' ') T_STRING ('now') T_WHITESPACE (' ') T_WHITESPACE ('') T_RETURN ('return') T_WHITESPACE (' ') T_STRING ('date') T_CONSTANT_ENCAPSED_STRING (''Y-m-d'') T_WHITESPACE ('') T_WHITESPACE ('') T_WHITESPACE ('') Tokenizer
  • 31. <?php namespace AppTime; class Clock { public function now() { return date('Y-m-d'); } } T_OPEN_TAG ('<?php') T_NAMESPACE ('namespace') T_WHITESPACE (' ') T_STRING ('App') T_NS_SEPARATOR ('') T_STRING ('Time') T_WHITESPACE (' ') T_CLASS ('class') T_WHITESPACE (' ') T_STRING ('Clock') T_WHITESPACE (' ') T_WHITESPACE (' ') T_PUBLIC ('public') T_WHITESPACE (' ') T_FUNCTION ('function') T_WHITESPACE (' ') T_STRING ('now') T_WHITESPACE (' ') T_WHITESPACE ('') T_RETURN ('return') T_WHITESPACE (' ') T_STRING ('date') T_CONSTANT_ENCAPSED_STRING (''Y-m-d'') T_WHITESPACE ('') T_WHITESPACE ('') T_WHITESPACE ('') Tokenizer
  • 32. <?php namespace AppTime; class Clock { public function now() { return date('Y-m-d'); } } T_OPEN_TAG ('<?php') T_NAMESPACE ('namespace') T_WHITESPACE (' ') T_STRING ('App') T_NS_SEPARATOR ('') T_STRING ('Time') T_WHITESPACE (' ') T_CLASS ('class') T_WHITESPACE (' ') T_STRING ('Clock') T_WHITESPACE (' ') T_WHITESPACE (' ') T_PUBLIC ('public') T_WHITESPACE (' ') T_FUNCTION ('function') T_WHITESPACE (' ') T_STRING ('now') T_WHITESPACE (' ') T_WHITESPACE ('') T_RETURN ('return') T_WHITESPACE (' ') T_STRING ('date') T_CONSTANT_ENCAPSED_STRING (''Y-m-d'') T_WHITESPACE ('') T_WHITESPACE ('') T_WHITESPACE ('') Tokenizer
  • 33. <?php namespace AppTime; class Clock { public function now() { return date('Y-m-d'); } } T_OPEN_TAG ('<?php') T_NAMESPACE ('namespace') T_WHITESPACE (' ') T_STRING ('App') T_NS_SEPARATOR ('') T_STRING ('Time') T_WHITESPACE (' ') T_CLASS ('class') T_WHITESPACE (' ') T_STRING ('Clock') T_WHITESPACE (' ') T_WHITESPACE (' ') T_PUBLIC ('public') T_WHITESPACE (' ') T_FUNCTION ('function') T_WHITESPACE (' ') T_STRING ('now') T_WHITESPACE (' ') T_WHITESPACE ('') T_RETURN ('return') T_WHITESPACE (' ') T_STRING ('date') T_CONSTANT_ENCAPSED_STRING (''Y-m-d'') T_WHITESPACE ('') T_WHITESPACE ('') T_WHITESPACE ('') Tokenizer
  • 34. <?php namespace AppTime; class Clock { public function now() { return date('Y-m-d'); } } T_OPEN_TAG ('<?php') T_NAMESPACE ('namespace') T_WHITESPACE (' ') T_STRING ('App') T_NS_SEPARATOR ('') T_STRING ('Time') T_WHITESPACE (' ') T_CLASS ('class') T_WHITESPACE (' ') T_STRING ('Clock') T_WHITESPACE (' ') T_WHITESPACE (' ') T_PUBLIC ('public') T_WHITESPACE (' ') T_FUNCTION ('function') T_WHITESPACE (' ') T_STRING ('now') T_WHITESPACE (' ') T_WHITESPACE ('') T_RETURN ('return') T_WHITESPACE (' ') T_STRING ('date') T_CONSTANT_ENCAPSED_STRING (''Y-m-d'') T_WHITESPACE ('') T_WHITESPACE ('') T_WHITESPACE ('') Tokenizer
  • 35. <?php namespace AppTime; c class Clock { public function now() { return date('Y-m-d'); } } T_OPEN_TAG ('<?php') T_NAMESPACE ('namespace') T_WHITESPACE (' ') T_STRING ('App') T_NS_SEPARATOR ('') T_STRING ('Time') T_WHITESPACE (' ') T_CLASS ('class') T_WHITESPACE (' ') T_STRING ('Clock') T_WHITESPACE (' ') T_WHITESPACE (' ') T_PUBLIC ('public') T_WHITESPACE (' ') T_FUNCTION ('function') T_WHITESPACE (' ') T_STRING ('now') T_WHITESPACE (' ') T_WHITESPACE ('') T_RETURN ('return') T_WHITESPACE (' ') T_STRING ('date') T_CONSTANT_ENCAPSED_STRING (''Y-m-d'') T_WHITESPACE ('') T_WHITESPACE ('') T_WHITESPACE ('') Tokenizer
  • 36. <?php namespace AppTime; c class Clock { public function now() { return date('Y-m-d'); } } T_OPEN_TAG ('<?php') T_NAMESPACE ('namespace') T_WHITESPACE (' ') T_STRING ('App') T_NS_SEPARATOR ('') T_STRING ('Time') T_WHITESPACE (' ') T_CLASS ('class') T_WHITESPACE (' ') T_STRING ('Clock') T_WHITESPACE (' ') T_WHITESPACE (' ') T_PUBLIC ('public') T_WHITESPACE (' ') T_FUNCTION ('function') T_WHITESPACE (' ') T_STRING ('now') T_WHITESPACE (' ') T_WHITESPACE ('') T_RETURN ('return') T_WHITESPACE (' ') T_STRING ('date') T_CONSTANT_ENCAPSED_STRING (''Y-m-d'') T_WHITESPACE ('') T_WHITESPACE ('') T_WHITESPACE ('') Tokenizer
  • 37. <?php namespace AppTime; c class Clock { public function now() { return date('Y-m-d'); } } T_OPEN_TAG ('<?php') T_NAMESPACE ('namespace') T_WHITESPACE (' ') T_STRING ('App') T_NS_SEPARATOR ('') T_STRING ('Time') T_WHITESPACE (' ') T_CLASS ('class') T_WHITESPACE (' ') T_STRING ('Clock') T_WHITESPACE (' ') T_WHITESPACE (' ') T_PUBLIC ('public') T_WHITESPACE (' ') T_FUNCTION ('function') T_WHITESPACE (' ') T_STRING ('now') T_WHITESPACE (' ') T_WHITESPACE ('') T_RETURN ('return') T_WHITESPACE (' ') T_STRING ('date') T_CONSTANT_ENCAPSED_STRING (''Y-m-d'') T_WHITESPACE ('') T_WHITESPACE ('') T_WHITESPACE ('') Tokenizer token_get_all
  • 38. T_OPEN_TAG ('<?php') T_NAMESPACE ('namespace') T_WHITESPACE (' ') T_STRING ('App') T_NS_SEPARATOR ('') T_STRING ('Time') T_WHITESPACE (' ') T_CLASS ('class') T_WHITESPACE (' ') T_STRING ('Clock') T_WHITESPACE (' ') T_WHITESPACE (' ') T_PUBLIC ('public') T_WHITESPACE (' ') T_FUNCTION ('function') T_WHITESPACE (' ') T_STRING ('now') T_WHITESPACE (' ') T_WHITESPACE ('') T_RETURN ('return') T_WHITESPACE (' ') T_STRING ('date') T_CONSTANT_ENCAPSED_STRING (''Y-m-d'') T_WHITESPACE ('') T_WHITESPACE ('') T_WHITESPACE ('') Parser
  • 39. T_OPEN_TAG ('<?php') T_NAMESPACE ('namespace') T_WHITESPACE (' ') T_STRING ('App') T_NS_SEPARATOR ('') T_STRING ('Time') T_WHITESPACE (' ') T_CLASS ('class') T_WHITESPACE (' ') T_STRING ('Clock') T_WHITESPACE (' ') T_WHITESPACE (' ') T_PUBLIC ('public') T_WHITESPACE (' ') T_FUNCTION ('function') T_WHITESPACE (' ') T_STRING ('now') T_WHITESPACE (' ') T_WHITESPACE ('') T_RETURN ('return') T_WHITESPACE (' ') T_STRING ('date') T_CONSTANT_ENCAPSED_STRING (''Y-m-d'') T_WHITESPACE ('') T_WHITESPACE ('') T_WHITESPACE ('') Parser
  • 40. T_OPEN_TAG ('<?php') T_NAMESPACE ('namespace') T_WHITESPACE (' ') T_STRING ('App') T_NS_SEPARATOR ('') T_STRING ('Time') T_WHITESPACE (' ') T_CLASS ('class') T_WHITESPACE (' ') T_STRING ('Clock') T_WHITESPACE (' ') T_WHITESPACE (' ') T_PUBLIC ('public') T_WHITESPACE (' ') T_FUNCTION ('function') T_WHITESPACE (' ') T_STRING ('now') T_WHITESPACE (' ') T_WHITESPACE ('') T_RETURN ('return') T_WHITESPACE (' ') T_STRING ('date') T_CONSTANT_ENCAPSED_STRING (''Y-m-d'') T_WHITESPACE ('') T_WHITESPACE ('') T_WHITESPACE ('') Parser AST: Abstract Syntax Tree PHP-Parser
  • 45. AST - Abstract Syntax Tree Intermediate representation of the source code Machine friendly Tree structure Traversing the AST means traversing the original source code
  • 48. A rule is a PHP class that extends AbstractRector Implement 3 method Writing Custom Rules public function getRuleDefinition(): RuleDefinition public function getNodeTypes(): array public function refactor(Node $node): ?Node
  • 49. Look for existing rules that perform similar tasks. Use it as a starting point. Look for helper classes: classes that perform some heavy lifting for you. E.g pull up a property There are plenty: - ClassDependencyManipulator - ClassInsertManipulator - NodeFactory - ParamAnalyzer Create a small snippet of code to test it with --dry-run Start with an existing rule
  • 50. Let's 👀 at the code…
  • 51. Look for a rule that does something similar to what you need and use it as a starting point This part is a bit time consuming, there are multiple ways to achieve the same result Don't try to do too much in a single rule, combine multiple rules Writing Custom Rules: lesson learned
  • 52. Get all the corner cases is hard, Pareto rulez! NodeDumper is your friend Create a Dummy snippet for quick testing Writing Custom Rules: lesson learned
  • 53. Extensions: composer installable set of rules for specific framework/libraries These are provided out of the box https://github.com/rectorphp/rector-symfony https://github.com/rectorphp/rector-doctrine https://github.com/rectorphp/rector-phpunit Extensions