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 @@ -30,11 +30,8 @@

class TolerantClassFinder implements ClassFinder
{
private Parser $parser;

public function __construct(?Parser $parser = null)
public function __construct(private Parser $parser = new Parser())
{
$this->parser = $parser ?: new Parser();
}

public function findIn(TextDocument $source): NamespacedClassReferences
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,12 @@ class WorseTolerantMemberFinder implements MemberFinder
{
private Reflector $reflector;

private Parser $parser;

private LoggerInterface $logger;

public function __construct(
?Reflector $reflector = null,
?Parser $parser = null,
?LoggerInterface $logger = null
private Parser $parser = new Parser(),
private LoggerInterface $logger = new NullLogger()
) {
$this->reflector = $reflector ?: ReflectorBuilder::create()->addSource(TextDocumentBuilder::empty());
$this->parser = $parser ?: new Parser();
$this->logger = $logger ?: new NullLogger();
}

public function findMembers(SourceCode $source, ClassMemberQuery $query): MemberReferences
Expand Down
12 changes: 4 additions & 8 deletions lib/ClassMover/ClassMover.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,10 @@

class ClassMover
{
private ClassFinder $finder;

private ClassReplacer $replacer;

public function __construct(?ClassFinder $finder = null, ?ClassReplacer $replacer = null)
{
$this->finder = $finder ?: new TolerantClassFinder();
$this->replacer = $replacer ?: new TolerantClassReplacer(new TolerantUpdater(new TwigRenderer()));
public function __construct(
private ClassFinder $finder = new TolerantClassFinder(),
private ClassReplacer $replacer = new TolerantClassReplacer(new TolerantUpdater(new TwigRenderer())),
) {
}

public function findReferences(string $source, string $fullyQualifiedName): FoundReferences
Expand Down
4 changes: 2 additions & 2 deletions lib/ClassMover/Extension/ClassMoverExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ private function registerClassMover(ContainerBuilder $container): void
{
$container->register(ClassMover::class, function (Container $container) {
return new ClassMover(
$container->get('class_mover.class_finder'),
$container->get('class_mover.ref_replacer')
$container->expect('class_mover.class_finder', TolerantClassFinder::class),
$container->expect('class_mover.ref_replacer', TolerantClassReplacer::class)
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Phpactor\ClassMover\Tests\Adapter\TolerantParser;

use PHPUnit\Framework\Attributes\TestDox;
use Microsoft\PhpParser\Parser;
use Phpactor\ClassMover\Adapter\TolerantParser\TolerantClassFinder;
use PHPUnit\Framework\TestCase;
use Phpactor\TextDocument\TextDocumentBuilder;
Expand All @@ -13,8 +12,7 @@ class TolerantClassFinderTest extends TestCase
#[TestDox('It finds all class references.')]
public function testFind(): void
{
$parser = new Parser();
$tolerantRefFinder = new TolerantClassFinder($parser);
$tolerantRefFinder = new TolerantClassFinder();
$source = TextDocumentBuilder::fromUri(__DIR__ . '/examples/Example1.php')->build();
$names = iterator_to_array($tolerantRefFinder->findIn($source));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\TestDox;
use Generator;
use Microsoft\PhpParser\Parser;
use Phpactor\ClassMover\Adapter\TolerantParser\TolerantClassFinder;
use PHPUnit\Framework\TestCase;
use Phpactor\ClassMover\Adapter\TolerantParser\TolerantClassReplacer;
Expand All @@ -20,8 +19,7 @@ class TolerantClassReplacerTest extends TestCase
#[TestDox('It finds all class references.')]
public function testFind(string $fileName, string $classFqn, string $replaceWithFqn, string $expectedSource): void
{
$parser = new Parser();
$tolerantRefFinder = new TolerantClassFinder($parser);
$tolerantRefFinder = new TolerantClassFinder();
$source = TextDocumentBuilder::fromUri(__DIR__ . '/examples/' . $fileName)->build();
$originalName = FullyQualifiedName::fromString($classFqn);

Expand Down
5 changes: 1 addition & 4 deletions lib/CodeBuilder/Adapter/TolerantParser/Edits.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@ class Edits
*/
private array $edits = [];

private TextFormat $format;

public function __construct(?TextFormat $format = null)
public function __construct(private TextFormat $format = new TextFormat())
{
$this->format = $format ?: new TextFormat();
}

/**
Expand Down
10 changes: 2 additions & 8 deletions lib/CodeBuilder/Adapter/TolerantParser/TolerantUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@

class TolerantUpdater implements Updater
{
private Parser $parser;

private TextFormat $textFormat;

private ClassUpdater $classUpdater;

private InterfaceUpdater $interfaceUpdater;
Expand All @@ -41,11 +37,9 @@ class TolerantUpdater implements Updater

public function __construct(
private Renderer $renderer,
?TextFormat $textFormat = null,
?Parser $parser = null
private TextFormat $textFormat = new TextFormat(),
private Parser $parser = new Parser()
) {
$this->parser = $parser ?: new Parser();
$this->textFormat = $textFormat ?: new TextFormat();
$this->classUpdater = new ClassUpdater($renderer);
$this->interfaceUpdater = new InterfaceUpdater($renderer);
$this->traitUpdater = new TraitUpdater($renderer);
Expand Down
5 changes: 1 addition & 4 deletions lib/CodeBuilder/Adapter/Twig/TwigRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,11 @@ final class TwigRenderer implements Renderer
{
private Environment $twig;

private TemplateNameResolver $templateNameResolver;

public function __construct(
?Environment $twig = null,
?TemplateNameResolver $templateNameResolver = null
private TemplateNameResolver $templateNameResolver = new ClassShortNameResolver(),
) {
$this->twig = $twig ?: $this->createTwig();
$this->templateNameResolver = $templateNameResolver ?: new ClassShortNameResolver();
}

public function render(Prototype $prototype, ?string $variant = null): Code
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ class TolerantUpdaterTest extends UpdaterTestCase
{
protected function updater(): Updater
{
return new TolerantUpdater(new TwigRenderer(), null, null);
return new TolerantUpdater(new TwigRenderer());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,8 @@

class ClassNameFixerTransformer implements Transformer
{
private Parser $parser;

public function __construct(
private FileToClass $fileToClass,
?Parser $parser = null
) {
$this->parser = $parser ?: new Parser();
public function __construct(private FileToClass $fileToClass, private Parser $parser = new Parser())
{
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@

class TolerantChangeVisiblity implements ChangeVisiblity
{
private Parser $parser;

public function __construct(?Parser $parser = null)
public function __construct(private Parser $parser = new Parser())
{
$this->parser = $parser ?: new Parser();
}

public function changeVisiblity(SourceCode $source, int $offset): SourceCode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@

class TolerantExtractExpression implements ExtractExpression
{
private Parser $parser;

public function __construct(?Parser $parser = null)
public function __construct(private Parser $parser = new Parser())
{
$this->parser = $parser ?: new Parser();
}

public function canExtractExpression(SourceCode $source, int $offsetStart, ?int $offsetEnd = null): bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,11 @@

class TolerantImportName implements ImportName
{
private Parser $parser;

public function __construct(
private Updater $updater,
?Parser $parser = null,
private bool $importGlobals = false
private Parser $parser = new Parser(),
private bool $importGlobals = false,
) {
$this->parser = $parser ?: new Parser();
}

public function importName(SourceCode $source, ByteOffset $offset, NameImport $nameImport): TextEdits
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,8 @@

class TolerantRenameVariable implements RenameVariable
{
private Parser $parser;

public function __construct(?Parser $parser = null)
public function __construct(private Parser $parser = new Parser())
{
$this->parser = $parser ?: new Parser();
}

public function renameVariable(SourceCode $sourceCode, int $offset, string $newName, string $scope = self::SCOPE_FILE): SourceCode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@

class WorseInterestingOffsetFinder implements InterestingOffsetFinder
{
private Parser $parser;

public function __construct(private SourceCodeReflector $reflector, ?Parser $parser = null)
{
$this->parser = $parser ?: new Parser();
public function __construct(
private SourceCodeReflector $reflector,
private Parser $parser = new Parser(),
) {
}

public function find(TextDocument $source, ByteOffset $offset): ByteOffset
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,8 @@

class WorseExtractConstant implements ExtractConstant
{
private Parser $parser;

public function __construct(
private Reflector $reflector,
private Updater $updater,
?Parser $parser = null
) {
$this->parser = $parser ?: new Parser();
public function __construct(private Reflector $reflector, private Updater $updater, private Parser $parser = new Parser())
{
}

public function extractConstant(SourceCode $sourceCode, int $offset, string $constantName): TextDocumentEdits
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,12 @@

class WorseExtractMethod implements ExtractMethod
{
private Parser $parser;

public function __construct(
private Reflector $reflector,
private BuilderFactory $factory,
private Updater $updater,
?Parser $parser = null
private Parser $parser = new Parser(),
) {
$this->parser = $parser ?: new Parser();
}

public function canExtractMethod(SourceCode $source, int $offsetStart, int $offsetEnd): bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,8 @@

class WorseReplaceQualifierWithImport implements ReplaceQualifierWithImport
{
private Parser $parser;

public function __construct(
private Reflector $reflector,
private BuilderFactory $factory,
private Updater $updater,
?Parser $parser = null
) {
$this->parser = $parser ?: new Parser();
public function __construct(private Reflector $reflector, private BuilderFactory $factory, private Updater $updater, private Parser $parser = new Parser())
{
}

public function getTextEdits(SourceCode $sourceCode, int $offset): TextDocumentEdits
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,8 @@ class AddMissingProperties implements Transformer
{
private const LENGTH_OF_THIS_PREFIX = 7;

private Parser $parser;

public function __construct(
private Reflector $reflector,
private Updater $updater,
?Parser $parser = null
) {
$this->parser = $parser ?: new Parser();
public function __construct(private Reflector $reflector, private Updater $updater, private Parser $parser = new Parser())
{
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@

class ChainTolerantCompletor implements Completor
{
private Parser $parser;

/**
* @param TolerantCompletor[] $tolerantCompletors
*/
public function __construct(private array $tolerantCompletors, ?Parser $parser = null)
{
$this->parser = $parser ?: new Parser();
public function __construct(
private array $tolerantCompletors,
private Parser $parser = new Parser(),
) {
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ public function __construct(
private ObjectFormatter $snippetFormatter,
?DocumentPrioritizer $prioritizer = null
) {
parent::__construct($nameSearcher, $prioritizer);
if ($prioritizer === null) {
parent::__construct($nameSearcher);
} else {
parent::__construct($nameSearcher, $prioritizer);
}
}

// 1. If no namespace separator - search by short name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,11 @@

class ScfClassCompletor implements TolerantCompletor, TolerantQualifiable
{
private ClassQualifier $qualifier;

public function __construct(
private Filesystem $filesystem,
private FileToClass $fileToClass,
?ClassQualifier $qualifier = null
private ClassQualifier $qualifier = new ClassQualifier(),
) {
$this->qualifier = $qualifier ?: new ClassQualifier();
}

public function qualifier(): TolerantQualifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct(
private ObjectFormatter $formatter,
?VariableCompletionHelper $variableCompletionHelper = null
) {
$this->variableCompletionHelper = $variableCompletionHelper ?: new VariableCompletionHelper($reflector);
$this->variableCompletionHelper = $variableCompletionHelper ?? new VariableCompletionHelper($reflector);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,12 @@

class DoctrineAnnotationCompletor extends NameSearcherCompletor implements Completor
{
private Parser $parser;

public function __construct(
NameSearcher $nameSearcher,
private Reflector $reflector,
?Parser $parser = null
private Parser $parser = new Parser()
) {
parent::__construct($nameSearcher, null);
$this->parser = $parser ?: new Parser();
parent::__construct($nameSearcher);
}

public function complete(TextDocument $source, ByteOffset $byteOffset): Generator
Expand Down
Loading