Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upNewObjectToFactoryCreateRector #982
Conversation
This comment has been minimized.
This comment has been minimized.
@TomasVotruba please could you check my code? I am getting weird error with this config:
|
This comment has been minimized.
This comment has been minimized.
|
JanMikes
changed the title
[WIP] Failing tests
[WIP] NewObjectToFactoryCreateRector
Jan 22, 2019
This comment has been minimized.
This comment has been minimized.
@TomasVotruba call for help again, i digged into it a bit and found out that it does not matter whether i use The problem is when using array in config: services:
Rector\Rector\Architecture\Factory\NewObjectToFactoryCreateRector:
Rector\Tests\Rector\Architecture\Factory\NewObjectToFactoryCreateRector\Source\MyClass: [] It stops working. Could you please have a quick look at that? |
TomasVotruba
reviewed
Jan 23, 2019
The issue is, there is no constructor to autowire parameters to. services:
Rector\Rector\Architecture\Factory\NewObjectToFactoryCreateRector:
anything... is shortcut for services:
Rector\Rector\Architecture\Factory\NewObjectToFactoryCreateRector:
arguments:
anything... Your Rector needs constructor to pass configuration into. Inspire here: rector/src/Rector/Class_/ClassReplacerRector.php Lines 22 to 28 in 3872fa4 |
This comment has been minimized.
This comment has been minimized.
<?php
final class NewObjectToFactoryCreateRector extends AbstractRector
{
/**
* @var string[][]
*/
private $factoryWithMethodByObject;
/**
* @param string[][] $factoryWithMethodByObject
*/
public function __construct(array $factoryWithMethodByObject)
{
$this->factoryWithMethodByObject = $factoryWithMethodByObject;
}
/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [New_::class];
}
/**
* @param New_ $node
*/
public function refactor(Node $node): ?Node
{
foreach ($this->factoryWithMethodByObject as $type => $factoryWithMethod) {
if (! $this->isType($node, $type)) {
continue;
}
// refactor to MethodCall
// add property
}
return $node;
}
} This will be then configured in config: # rector.yml
services:
NewObjectToFactoryCreateRector: # ← rector class
# configuration array that will be passed as $factoryWithMethodByObject
FirstObject: ['FirstFactory', 'method']
SecondObject: ['SecondFactory', 'method'] ↓ full syntax without magic. Normally people would have to write all this manually and be careful about typos in "factoryWithMethodByObject", which can be really annoying, because there is no error on typo of argument name. services:
NewObjectToFactoryCreateRector: # ← rector class
arguments:
$factoryWithMethodByObject:
FirstObject: ['FirstFactory', 'method']
SecondObject: ['SecondFactory', 'method'] |
This comment has been minimized.
This comment has been minimized.
Just to let you know, I have tried this and this does not work, that is why i asked and felt so weird:
|
This comment has been minimized.
This comment has been minimized.
Could you push it? Now there is no array in constructor to put it to |
This comment has been minimized.
This comment has been minimized.
It works as expected. Here is the change in code: https://github.com/rectorphp/rector/compare/new-object-to-factory-method-rector...quick-example?expand=1 |
TomasVotruba
referenced this pull request
Jan 23, 2019
Closed
Throw informative exception when arguments are not autowireable #983
JanMikes
force-pushed the
new-object-to-factory-method-rector
branch
4 times, most recently
from
5a738bd
to
da068fd
Jan 24, 2019
JanMikes
changed the title
[WIP] NewObjectToFactoryCreateRector
NewObjectToFactoryCreateRector
Jan 24, 2019
This comment has been minimized.
This comment has been minimized.
@TomasVotruba working, rebased on master (solved conflicts) and ready to merge As well i squashed commits so all new feature is just in one commit: da068fd |
This comment has been minimized.
This comment has been minimized.
Btw i did not test/cover edge cases - property already exists, factory already exists in different property in class etc. |
This comment has been minimized.
This comment has been minimized.
@JanMikes I see some commits already in master. Could you rebase on it? |
This comment has been minimized.
This comment has been minimized.
Feature works, nice Testing edge cases would be nice to have, but not neccesary. It's up to you. Also there is 1 fail on static analysis, please fix it: |
JanMikes
force-pushed the
new-object-to-factory-method-rector
branch
from
da068fd
to
86826e1
Jan 24, 2019
This comment has been minimized.
This comment has been minimized.
Thank you, i fixed that and i will as well cover the edge cases, while i am already in it I think that history must be mistaken, i have just checked it and there are no commits in master yet -> i rebased this branch on master. Last my commit i can see in master is |
This comment has been minimized.
This comment has been minimized.
You pushed 10 minutes ago, that fixed the commits |
This comment has been minimized.
This comment has been minimized.
Great |
JanMikes
force-pushed the
new-object-to-factory-method-rector
branch
from
86826e1
to
43de770
Jan 24, 2019
This comment has been minimized.
This comment has been minimized.
This edge case covered by: https://github.com/rectorphp/rector/blob/43de77013c5ca172fb53a87cb35ec13e2391c289/tests/Rector/Architecture/Factory/NewObjectToFactoryCreateRector/Fixture/fixture2.php.inc Now it is finally ready to merge Any feedback/code review is very appreciated |
This comment has been minimized.
This comment has been minimized.
I'm on it :) |
This comment has been minimized.
This comment has been minimized.
Btw, no need to rebase after each commit, just once in the end. |
TomasVotruba
reviewed
Jan 24, 2019
I've added what I could find. Very good job in effective code |
This comment has been minimized.
This comment has been minimized.
Great! Ready to merge for me |
JanMikes
force-pushed the
new-object-to-factory-method-rector
branch
from
5e46a7a
to
a78d88a
Jan 24, 2019
JanMikes
merged commit 753a478
into
master
Jan 24, 2019
JanMikes
deleted the
new-object-to-factory-method-rector
branch
Jan 24, 2019
This comment has been minimized.
This comment has been minimized.
Kaboom |
This comment has been minimized.
This comment has been minimized.
Looking forward to get this tagged |
JanMikes commentedJan 22, 2019
•
edited by TomasVotruba
Closes #981
Before
After