public function testRemovesDuplicates() { vfsStreamWrapper::register(); $structure = array('src' => array('src' => array(), 'Foo' => array('src' => array(), 'Foo' => array(), 'Bar.php' => '<?php'))); vfsStream::create($structure, VfsStream::setup('project')); $dir = VfsStream::url('project/src'); $directory = new Directory($dir, $dir); $files = $directory->findAllPhpFilesRecursivly(); $foundFiles = array(); foreach ($files as $f => $file) { $foundFiles[] = $f; } $this->assertEquals(array('vfs://project/src/Foo/Bar.php'), $foundFiles); }
public function refactor(Directory $directory) { $phpFiles = $directory->findAllPhpFilesRecursivly(); $renames = new Set(); $occurances = array(); $noImportedUsages = new NoImportedUsagesFilter(); foreach ($phpFiles as $phpFile) { $classes = $this->codeAnalysis->findClasses($phpFile); $occurances = array_merge($noImportedUsages->filter($this->nameScanner->findNames($phpFile)), $occurances); if (count($classes) !== 1) { continue; } $class = $classes[0]; $currentClassName = $class->declarationName(); $expectedClassName = $phpFile->extractPsr0ClassName(); $buffer = $this->editor->openBuffer($phpFile); // This is weird to be required here if ($expectedClassName->shortName() !== $currentClassName->shortName()) { $renames->add(new PhpNameChange($currentClassName, $expectedClassName)); } if (!$expectedClassName->namespaceName()->equals($currentClassName->namespaceName())) { $renames->add(new PhpNameChange($currentClassName->fullyQualified(), $expectedClassName->fullyQualified())); $buffer->replaceString($class->namespaceDeclarationLine(), $currentClassName->namespaceName()->fullyQualifiedName(), $expectedClassName->namespaceName()->fullyQualifiedName()); } } $occurances = array_filter($occurances, function ($occurance) { return $occurance->name()->type() !== PhpName::TYPE_NAMESPACE; }); foreach ($occurances as $occurance) { $name = $occurance->name(); foreach ($renames as $rename) { if ($rename->affects($name)) { $buffer = $this->editor->openBuffer($occurance->file()); $buffer->replaceString($occurance->declarationLine(), $name->relativeName(), $rename->change($name)->relativeName()); continue 2; } } } $this->editor->save(); }