Skip to content
Merged
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 @@ -2,6 +2,7 @@

namespace Rector\SymfonyPHPUnit\Rector\Class_;

use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\Assign;
Expand All @@ -11,6 +12,7 @@
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
Expand Down Expand Up @@ -212,7 +214,14 @@ private function createSelfContainerGetWithTypeMethodCall(string $serviceType):
$staticPropertyFetch = new StaticPropertyFetch(new Name('self'), 'container');

$methodCall = new MethodCall($staticPropertyFetch, 'get');
$methodCall->args[] = new Arg($this->createClassConstantReference($serviceType));
if (Strings::contains($serviceType, '_')) {
// keep string
$getArgumentValue = new String_($serviceType);
} else {
$getArgumentValue = $this->createClassConstantReference($serviceType);
}

$methodCall->args[] = new Arg($getArgumentValue);

return $methodCall;
}
Expand All @@ -232,7 +241,7 @@ private function createSelfContainerGetWithTypeAssigns(Class_ $class, array $ser
$className = $class->getAttribute(AttributeKey::CLASS_NAME);

foreach ($serviceTypes as $serviceType) {
$propertyName = $this->propertyNaming->fqnToVariableName($serviceType);
$propertyName = $this->resolvePropertyNameFromServiceType($serviceType);

// skip existing properties
if (property_exists($className, $propertyName)) {
Expand Down Expand Up @@ -263,7 +272,7 @@ private function createPrivatePropertiesFromTypes(Class_ $class, array $serviceT
$className = $class->getAttribute(AttributeKey::CLASS_NAME);

foreach ($serviceTypes as $serviceType) {
$propertyName = $this->propertyNaming->fqnToVariableName($serviceType);
$propertyName = $this->resolvePropertyNameFromServiceType($serviceType);

// skip existing properties
if (property_exists($className, $propertyName)) {
Expand Down Expand Up @@ -296,7 +305,12 @@ private function collectContainerGetServiceTypes(Class_ $class): array
}

/** @var MethodCall $node */
$serviceTypes[] = $this->getValue($node->args[0]->value);
$serviceType = $this->getValue($node->args[0]->value);
if ($this->shouldSkipServiceType($serviceType)) {
return null;
}

$serviceTypes[] = $serviceType;
});

return array_unique($serviceTypes);
Expand Down Expand Up @@ -344,7 +358,7 @@ private function removeAndCollectFormerAssignedVariables(Class_ $class): array
return null;
}

$propertyName = $this->propertyNaming->fqnToVariableName($type);
$propertyName = $this->resolvePropertyNameFromServiceType($type);

return new PropertyFetch(new Variable('this'), $propertyName);
});
Expand Down Expand Up @@ -375,10 +389,25 @@ private function replaceFormerVariablesWithPropertyFetch(Class_ $class, array $f
return null;
}

$type = $formerVariablesByMethods[$methodName][$variableName];
$propertyName = $this->propertyNaming->fqnToVariableName($type);
$serviceType = $formerVariablesByMethods[$methodName][$variableName];

$propertyName = $this->resolvePropertyNameFromServiceType($serviceType);

return new PropertyFetch(new Variable('this'), $propertyName);
});
}

private function shouldSkipServiceType(string $serviceType): bool
{
return $serviceType === 'Symfony\Component\HttpFoundation\Session\SessionInterface';
}

private function resolvePropertyNameFromServiceType(string $serviceType): string
{
if (Strings::contains($serviceType, '_')) {
return $this->propertyNaming->underscoreToName($serviceType);
}

return $this->propertyNaming->fqnToVariableName($serviceType);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Rector\SymfonyPHPUnit\Tests\Rector\Class_\MultipleServiceGetToSetUpMethodRector\Fixture;

use Rector\SymfonyPHPUnit\Tests\Rector\Class_\MultipleServiceGetToSetUpMethodRector\Source\ParentClassWithPropertyKernelTestCase;
use Symfony\Component\HttpFoundation\Session\SessionInterface;

class SkipSessions extends ParentClassWithPropertyKernelTestCase
{
public function testOne()
{
$firstSession = self::$container->get(SessionInterface::class);
$firstSession->doStuff();
}

public function testTwo()
{
$secondSession = self::$container->get(SessionInterface::class);
$secondSession->doAnotherStuff();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Rector\SymfonyPHPUnit\Tests\Rector\Class_\MultipleServiceGetToSetUpMethodRector\Fixture;

use ItemRepository;
use Rector\SymfonyPHPUnit\Tests\Rector\Class_\MultipleServiceGetToSetUpMethodRector\Source\DummyKernelTestCase;

class StringServiceName extends DummyKernelTestCase
{
public function testOne()
{
$someValue = self::$container->get('some_value');
$someValue->doStuff();
}

public function testTwo()
{
$someValue = self::$container->get('some_value');
$someValue->doAnotherStuff();
}
}

?>
-----
<?php

namespace Rector\SymfonyPHPUnit\Tests\Rector\Class_\MultipleServiceGetToSetUpMethodRector\Fixture;

use ItemRepository;
use Rector\SymfonyPHPUnit\Tests\Rector\Class_\MultipleServiceGetToSetUpMethodRector\Source\DummyKernelTestCase;

class StringServiceName extends DummyKernelTestCase
{
/**
* @var \some_value
*/
private $someValue;
protected function setUp()
{
parent::setUp();
$this->someValue = self::$container->get('some_value');
}
public function testOne()
{
$this->someValue->doStuff();
}

public function testTwo()
{
$this->someValue->doAnotherStuff();
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ public function test(): void
$this->doTestFiles([
__DIR__ . '/Fixture/fixture.php.inc',
__DIR__ . '/Fixture/existing_setup.php.inc',
__DIR__ . '/Fixture/string_service_name.php.inc',
__DIR__ . '/Fixture/extends_parent_class_with_property.php.inc',
__DIR__ . '/Fixture/instant_call.php.inc',
__DIR__ . '/Fixture/skip_sessions.php.inc',
]);
}

Expand Down