Skip to content
This repository was archived by the owner on Mar 6, 2022. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,12 @@ public function generateFromExisting(ClassName $existingClass, ClassName $target
}

if ($method->returnType()->isDefined()) {
$methodBuilder->returnType($method->returnType()->short());
$shortTypeAsString = ($method->returnType()->isNullable() ? '?' : '') .
$method->returnType()->short();
$methodBuilder->returnType($shortTypeAsString);

if ($method->returnType()->isClass()) {
$sourceBuilder->use($method->returnType());
$sourceBuilder->use(ltrim($method->returnType(), '?'));
}
}

Expand All @@ -74,7 +76,9 @@ public function generateFromExisting(ClassName $existingClass, ClassName $target
} else {
$className = $parameter->type()->className();
if ($className) {
$parameterBuilder->type($className->short());
$shortTypeAsString = ($parameter->type()->isNullable() ? '?' : '') .
$className->short();
$parameterBuilder->type($shortTypeAsString);
$paramClassName = $parameter->type()->className();
if ($paramClassName) {
$useClasses[$paramClassName->__toString()] = true;
Expand Down
11 changes: 10 additions & 1 deletion lib/Adapter/WorseReflection/Refactor/WorseExtractMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,17 @@ private function addParametersAndGetArgs(array $freeVariables, $methodBuilder, S
$variableType = $freeVariable->symbolContext()->type();

if ($variableType->isDefined()) {
$parameterBuilder->type($variableType->short());
// TODO Create a utils class or something to get the shortest type by taking
// into account the import table, to handle aliases and partial namespaces
$shortTypeAsString = ($variableType->isNullable() ? '?' : '') . $variableType->short();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we remove the TODO. This is to do with the qualified names topic :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do


$parameterBuilder->type($shortTypeAsString);
if ($variableType->isClass()) {
// TODO Remove this ?
// We should not have to import it, we are extracting the
// method into the same file. We should not try to fix an
// oversight from the user, espacially since he might want
// to keep his partial qualified name
$builder->use((string) $variableType);

@dantleech dantleech Jan 18, 2020 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need to import it -- f.e.

  • a variable was retrieved from a method from another namespace
  • the variable has a type
  • it is now a parameter in the new method in the current namespace
  • we need to import the type.

Can we change the comment to reflect that?

We need to fix the whole qualfiied name issue, but it's a separate thing :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay to respond, I needed to take care of some other stuff and I don't have a lot of time left.

I'm not sure to see the case you're talking about.
Here we are extracting a method from a selection.
So the new method will always be created in the same scope: the class.
Meaning that any variable that we will have to provide as parameters to new method will already be imported in the scope.
If it is not, it's the fault of the user and we have no way of knowing which one to import in case there is multiple possibilities.

So I understand that we can try, just in case there is only one possibility.
But I don't think it's really SRP, here we should only build a method from the information at our disposal in the scope.
We should not modify the scope, even to fix an error.

If you still want to use the import I will change the comment for the one you proposed.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Meaning that any variable that we will have to provide as parameters to new method will already be imported in the scope.

This is not true (I previously fell for this) -- if you call a method on an imported class, that method can return a type not in the current scope and you will therefore need to import it if it's a parameter of the extracted mehtod.

}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ public function transform(SourceCode $code): SourceCode
->property($variable->name())
->visibility('private');
if ($variable->symbolContext()->type()->isDefined()) {
$propertyBuilder->type($variable->symbolContext()->type()->short());
$type = $variable->symbolContext()->type();
$shortTypeAsString = ($type->isNullable() ? '?' : '') . $type->short();
$propertyBuilder->type($shortTypeAsString);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@ public function transform(SourceCode $source): SourceCode
$propertyBuilder = $classBuilder->property($parameter->name());
$propertyBuilder->visibility('private');
if ($parameter->type()->isDefined()) {
$propertyBuilder->type((string) $parameter->type()->short());
$shortType = $parameter->type()->short();
if ($parameter->type()->isNullable()) {
$shortType = "?$shortType";
}

$propertyBuilder->type($shortType);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,40 @@ public function play(): string;
EOT

],
'Generates interface with nullable types' => [
'Music\Beat',
'Music\BeatInterface',
<<<'EOT'
<?php

namespace Music;

class Beat
{
public function play(?Song $b, ?string $s): ?string
{
}

public function foo(): ?Song
{
}
}
EOT
, <<<'EOT'
<?php

namespace Music;

use Music\Song;

interface BeatInterface
{
public function play(?Song $b, ?string $s): ?string;

public function foo(): ?Song;
}
EOT
],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ public function provideExtractMethod()
'extractMethod23.test',
'newMethod',
],
'extract method with a nullable parameter' => [
'extractMethodWithNullableParameter.test',
'newMethod',
],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ public function provideExtractAccessor()
'generateAccessor6.test',
$propertyName,
],
'nullable typed property' => [
'generateAccessorForNullableProperty.test',
'baz',
],
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ public function provideExtractMethod()
'static public method' => [
'generateMethod11.test',
],
'method with nullable parameter' => [
'generateMethodWithNullableParameter.test',
],
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ public function provideExtractMethod()
'ChildClass',
'barbar'
],
'method with nullable types' => [
'overrideMethodWithNullableTypes.test',
'Test\Foo\Baz',
'baz'
],
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class extractMethod
if ($foobar) {
return 'yes';
}

return 'no';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class extractMethod
if ($foobar) {
return $barfoo;
}

return 'no';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class extractMethod
if ($foobar) {
return 'yes';
}

return $foobar;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// File: source
<?php

namespace Test\Foo;

class Bar
{
public function baz(?string $s)
{
<>$foobar = $s.'hello';<>
}
}
// File: expected
<?php

namespace Test\Foo;

class Bar
{
public function baz(?string $s)
{
$this->newMethod($s);
}

private function newMethod(?string $s)
{
$foobar = $s.'hello';
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// File: source
<?php

namespace Test\Foo;

class Bar
{
pr<>ivate ?string $baz;
}
// File: expected
<?php

namespace Test\Foo;

class Bar
{
private ?string $baz;

public function baz(): ?string
{
return $this->baz;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// File: source
<?php

namespace Test\Foo;

class Bar
{
public function baz(?int $i): string
{
$this->f<>oo($i);
}
}
// File: expected
<?php

namespace Test\Foo;

class Bar
{
public function baz(?int $i): string
{
$this->foo($i);
}

private function foo(?int $i)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// File: source
<?php

namespace Test\Foo;

class Bar
{
public function baz(?int $i): ?string
{
}
}

class Baz extends Bar
{
}
// File: expected
<?php

namespace Test\Foo;

class Bar
{
public function baz(?int $i): ?string
{
}
}

class Baz extends Bar
{
public function baz(?int $i): ?string
{
}
}
Loading