GenerateAccessor handles both offset and names#24
GenerateAccessor handles both offset and names#24dantleech merged 11 commits intophpactor:masterfrom
Conversation
|
That's probably fine, we're testing the transformation. We can validate the method name in other tests.
…On 10 June 2019 14:06:56 CEST, ely ***@***.***> wrote:
elythyr commented on this pull request.
> @@ -48,4 +48,16 @@ protected function
sourceExpectedAndOffset($manifestPath)
return [ $source, $expected, $offsetStart, $offsetEnd ];
}
+
+ protected function
sourceExpectedAndWordUnderCursor($manifestPath): array
+ {
+ list($source, $expected) =
$this->sourceExpected($manifestPath);
+
+ preg_match('/(\w+)<>(\w+)/', $source, $matches);
+ $word = $matches[1] . $matches[2];
+
+ $source = preg_replace('/<>/', '', $source);
+
+ return [ $source, $expected, $word ];
We could keep it so we can still use the same data provider in the
test.
If we don't then I feel like we shouldn't use the
`generateAccessorX.test` files, because it will enforce a implicit rule
to name the tested property `method` and nothing else.
--
You are receiving this because you commented.
Reply to this email directly or view it on GitHub:
#24 (comment)
--
Sent from my Android device with K-9 Mail. Please excuse my brevity.
|
|
I think this could be an exception to that law. The reflection API is a tree structure and is self-contained (and immutable).
…On 10 June 2019 14:04:04 CEST, ely ***@***.***> wrote:
elythyr commented on this pull request.
> + }
+
+ public function generateFromPropertyName(SourceCode $sourceCode,
string $propertyName): SourceCode
+ {
+ $class = $this->class((string) $sourceCode);
+ $property = $class->properties()->get($propertyName);
+
+ $info = SymbolContext::for(Symbol::fromTypeNameAndPosition(
+ Symbol::PROPERTY,
+ $property->name(),
+ $property->position()
+ ))->withContainerType(Type::class($class->name()))
+ ->withTypes($property->docblock()->vars()->types())
+ ;
+
+ return $this->generate($info, $sourceCode);
I even wanted to gave it only a `ReflectionProperty` since it have all
the needed information.
For the namespace and class name `$property->class()->name()` and
`$property->class()->name()->namespace()`.
But to respect the law of demeter I propose to create:
* `ReflectionClassLike::namespace()`
* `ClassMemberReflection::namespace()`
* `ClassMemberReflection::className()`
--
You are receiving this because you commented.
Reply to this email directly or view it on GitHub:
#24 (comment)
--
Sent from my Android device with K-9 Mail. Please excuse my brevity.
|
I think we didn't understand each over because I forgot to update the test so it uses the property name when calling the generate method.
We can keep it self-contained and immutable, for instance with public function namespace(): string
{
return $this->name()->namespace();
}But I think this way it makes more sense. Still it's an early optimization, it could wait another PR. |
|
Another idea while I think about it. But once again, maybe a too early optimization... I kind of often do that >< EDIT: |
Don't understand was there was an error anyway but...
Replied to comment in the code.
I guess my question is: what value do we gain? Positive:
Negative:
Neutral:
It also depends on how you look at the role of the name / namespace. If you consider the name to be an FQN, then the namespace should be in the It may well be the second way is more correct (possibly) but the first way is how it is currently modelled, so we should keep it like that, at least for now. |
|
I agree with you. |
Yeah, let's discuss in another issue -- there have also been requests for generating "setters" also (:scream: , but I guess can be valid for things like DTOs). But in general, all for extracting classes :+1:
What has been done in this situation before is to pass the cursor offset and let Phpactor determine the name of the class. I'm not sure where ... I'll try and look. |
Not a big fan of setters neither, even for DTOs. Most of the time I think we are better of with immutable objects in those cases so no need for setters. Is there a todolist or idea box somewhere to register this kind of things for rainy days with nothing better to do ? :D
That's a possibility, but isn't it asking to Phpactor to do the job of the underlying plugin used by the editor ? |
Just the issue list :) |
Should had been in the previous commit but forgot. In order to bo able to test the retriving of the class from the cursor position I couldn't use the cursor to find out the name of the property.
I tried to do something like that. |
dantleech
left a comment
There was a problem hiding this comment.
Thanks again for your effort on this! Some CS comments relating to docblocks, but in general it looks good :)
| ], | ||
| 'multiple-classes' => [ | ||
| 'generateAccessor6.test', | ||
| $propertyName, |
There was a problem hiding this comment.
why do we add $propertyName when it is always the same?
There was a problem hiding this comment.
Would have slightly preferred to keep "constants" as a value in the test rather than repeating here (we don't vary in this test). But not important :)
There was a problem hiding this comment.
I always try to keep tests configurable, not saying it's a good thing.
It makes my life terrible some time ! :D
But that's why, I just don't like the idea of coupling even... Early optimization, sill got some work to do on that ><
|
Thanks @elythyr |
|
My pleasure, I will update the |
Go along with phpactor/phpactor#778