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
22 changes: 17 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Phpactor
This project aims to provide heavy-lifting *refactoring* and *introspection*
tools which can be used with editors such as VIM.

The tool is currently limited to working with projects using **GIT** and **Composer**.

It is currently under development.

VIM Plugin
Expand Down Expand Up @@ -40,34 +42,44 @@ source code to it.
Move the single class from one path to another:

```bash
$ phpactor mv lib/Path/To/MyClass.php lib/NewLocation.php
$ phpactor class:move lib/Path/To/MyClass.php lib/NewLocation.php
```

Relocate all classes under `Acme` to `Foobar`:

```bash
$ phpactor mv lib/Acme lib/Foobar
$ phpactor class:move lib/Acme lib/Foobar
```

Relocate all classes in the `lib` directory to a new subfolder:

```bash
$ phpactor mv lib/* lib/Core
$ phpactor class:move lib/* lib/Core
```

Move a class by name:

```bash
$ phpactor mv "Acme\\BlogPost" "Acme\\Article"
$ phpactor class:move "Acme\\BlogPost" "Acme\\Article"
```

![Class mover](https://user-images.githubusercontent.com/530801/27299917-d0f6da86-5525-11e7-901e-f3881e3afd83.gif)

- Moves individual class *files* or *directories*.
- Move by fully qualified class name of file path.
- Updates references for all moved classes in currently GIT tree.
- Updates references for all moved classes in currently **GIT tree**.
- Use statements are updated or added when required.

### Copy classes

As with move, except only update the class names of the copied class(es).

```bash
$ phpactor class:copy lib/Path/To/MyClass.php lib/Path/To/CopyOfMyClass.php
$ cat lib/Path/To/CopyOfMyClass.php | grep class
class CopyOfMyClass
```

### Class Search

Search for a class by its (short) name and return a list of fully qualified
Expand Down
8 changes: 4 additions & 4 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

126 changes: 126 additions & 0 deletions lib/Application/ClassCopy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php

namespace Phpactor\Application;

use DTL\ClassFileConverter\Domain\ClassName;
use DTL\ClassFileConverter\Domain\ClassToFileFileToClass;
use DTL\ClassFileConverter\Domain\FilePath as ConverterFilePath;
use DTL\ClassMover\ClassMover as ClassMoverFacade;
use DTL\ClassMover\Domain\FullyQualifiedName;
use DTL\Filesystem\Domain\FilePath;
use DTL\Filesystem\Domain\Filesystem;
use Phpactor\Application\ClassCopy\MoveOperation;
use Phpactor\Phpactor;
use Webmozart\Glob\Glob;
use Webmozart\PathUtil\Path;
use Phpactor\Application\Logger\ClassCopyLogger;
use DTL\Filesystem\Domain\CopyReport;

class ClassCopy
{
private $fileClassConverter;
private $classMover;
private $filesystem;

// rename compositetransformer => classToFileConverter
public function __construct(
ClassToFileFileToClass $fileClassConverter,
ClassMoverFacade $classMover,
Filesystem $filesystem
) {
$this->fileClassConverter = $fileClassConverter;
$this->filesystem = $filesystem;
$this->classMover = $classMover;
}

/**
* Move - guess if moving by class name or file.
*/
public function copy(ClassCopyLogger $logger, string $src, string $dest)
{
$srcPath = $src;
$destPath = $dest;

if (false === Phpactor::isFile($src)) {
$srcPathCandidates = $this->fileClassConverter->classToFileCandidates(ClassName::fromString($src));
if (false === $srcPathCandidates->noneFound()) {
$srcPath = (string) $srcPathCandidates->best();
}
}

if (false === Phpactor::isFile($dest)) {
$destPathCandidates = $this->fileClassConverter->classToFileCandidates(ClassName::fromString($dest));

if (false === $destPathCandidates->noneFound()) {
$destPath = (string) $destPathCandidates->best();
}
}

return $this->copyFile($logger, $srcPath, $destPath);
}

public function copyClass(ClassCopyLogger $logger, string $srcName, string $destName)
{
return $this->copyFile(
$logger,
(string) $this->fileClassConverter->classToFileCandidates(ClassName::fromString($srcName))->best(),
(string) $this->fileClassConverter->classToFileCandidates(ClassName::fromString($destName))->best()
);
}

public function copyFile(ClassCopyLogger $logger, string $srcPath, string $destPath)
{
$srcPath = Phpactor::normalizePath($srcPath);
foreach (Glob::glob($srcPath) as $globPath) {

$globDest = $destPath;
// if the src is not the same as the globbed src, then it is a wildcard
// and we want to append the filename to the destination
if ($srcPath !== $globPath) {
$globDest = Path::join($destPath, Path::getFilename($globPath));
}

try {
$this->doCopyFile($logger, $globPath, $globDest);
} catch (\Exception $e) {
throw new \RuntimeException(sprintf('Could not copy file "%s" to "%s"', $srcPath, $destPath), null, $e);
}
}
}

private function doCopyFile(ClassCopyLogger $logger, string $srcPath, string $destPath)
{
$destPath = Phpactor::normalizePath($destPath);

$srcPath = $this->filesystem->createPath($srcPath);
$destPath = $this->filesystem->createPath($destPath);

$report = $this->filesystem->copy($srcPath, $destPath);
$this->updateReferences($logger, $report);
$logger->copying($srcPath, $destPath);
}

private function updateReferences(ClassCopyLogger $logger, CopyReport $copyReport)
{
foreach ($copyReport->srcFiles() as $srcPath) {
$destPath = $copyReport->destFiles()->current();

$srcClassName = $this->fileClassConverter->fileToClassCandidates(
ConverterFilePath::fromString($srcPath->path())
)->best();
$destClassName = $this->fileClassConverter->fileToClassCandidates(
ConverterFilePath::fromString($destPath->path())
)->best();

$references = $this->classMover->findReferences($this->filesystem->getContents($srcPath), $srcClassName);
$logger->replacing($destPath, $references, FullyQualifiedName::fromString($destClassName));
$source = $this->classMover->replaceReferences(
$references,
$destClassName
);

$this->filesystem->writeContents($destPath, (string) $source);
$copyReport->destFiles()->next();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Phpactor\Application\ClassMover;
namespace Phpactor\Application;

use DTL\ClassFileConverter\Domain\ClassName;
use DTL\ClassFileConverter\Domain\ClassToFileFileToClass;
Expand All @@ -13,6 +13,7 @@
use Phpactor\Phpactor;
use Webmozart\Glob\Glob;
use Webmozart\PathUtil\Path;
use Phpactor\Application\Logger\ClassMoverLogger;

class ClassMover
{
Expand All @@ -34,7 +35,7 @@ public function __construct(
/**
* Move - guess if moving by class name or file.
*/
public function move(MoveLogger $logger, string $src, string $dest)
public function move(ClassMoverLogger $logger, string $src, string $dest)
{
$srcPath = $src;
$destPath = $dest;
Expand All @@ -57,7 +58,7 @@ public function move(MoveLogger $logger, string $src, string $dest)
return $this->moveFile($logger, $srcPath, $destPath);
}

public function moveClass(MoveLogger $logger, string $srcName, string $destName)
public function moveClass(ClassMoverLogger $logger, string $srcName, string $destName)
{
return $this->moveFile(
$logger,
Expand All @@ -66,7 +67,7 @@ public function moveClass(MoveLogger $logger, string $srcName, string $destName)
);
}

public function moveFile(MoveLogger $logger, string $srcPath, string $destPath)
public function moveFile(ClassMoverLogger $logger, string $srcPath, string $destPath)
{
$srcPath = Phpactor::normalizePath($srcPath);
foreach (Glob::glob($srcPath) as $globPath) {
Expand All @@ -86,7 +87,7 @@ public function moveFile(MoveLogger $logger, string $srcPath, string $destPath)
}
}

private function doMoveFile(MoveLogger $logger, string $srcPath, string $destPath)
private function doMoveFile(ClassMoverLogger $logger, string $srcPath, string $destPath)
{
$destPath = Phpactor::normalizePath($destPath);

Expand Down Expand Up @@ -119,7 +120,7 @@ private function directoryMap(FilePath $srcPath, FilePath $destPath)
return $files;
}

private function replaceThoseReferences(MoveLogger $logger, array $files)
private function replaceThoseReferences(ClassMoverLogger $logger, array $files)
{
foreach ($files as $paths) {
list($srcPath, $destPath) = $paths;
Expand All @@ -134,7 +135,7 @@ private function replaceThoseReferences(MoveLogger $logger, array $files)
}
}

private function replaceReferences(MoveLogger $logger, string $srcName, string $destName)
private function replaceReferences(ClassMoverLogger $logger, string $srcName, string $destName)
{
foreach ($this->filesystem->fileList()->phpFiles() as $filePath) {
$references = $this->classMover->findReferences($this->filesystem->getContents($filePath), $srcName);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Phpactor\Application\ClassSearch;
namespace Phpactor\Application;

use DTL\Filesystem\Domain\Filesystem;
use DTL\ClassFileConverter\Domain\FileToClass;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Phpactor\Application\FileInfo;
namespace Phpactor\Application;

use DTL\TypeInference\TypeInference;
use DTL\TypeInference\Domain\Offset;
Expand Down
95 changes: 95 additions & 0 deletions lib/Application/FileInfoAtOffset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

namespace Phpactor\Application;

use DTL\TypeInference\TypeInference;
use DTL\TypeInference\Domain\Offset;
use DTL\TypeInference\Domain\SourceCode;
use DTL\Filesystem\Domain\Filesystem;
use DTL\ClassFileConverter\Domain\FilePath;
use DTL\ClassFileConverter\Domain\ClassName;
use DTL\ClassFileConverter\Domain\ClassToFile;
use DTL\TypeInference\Domain\InferredType;
use DTL\TypeInference\Domain\TypeInferer;
use DTL\ClassFileConverter\Domain\FileToClass;

final class FileInfo
{
/**
* @var TypeInference
*/
private $inference;

/**
* @var FileToClass
*/
private $classToFileConverter;

/**
* @var Filesystem
*/
private $filesystem;

public function __construct(
TypeInferer $inference,
FileToClass $classToFileConverter,
Filesystem $filesystem
)
{
$this->inference = $inference;
$this->classToFileConverter = $classToFileConverter;
$this->filesystem = $filesystem;
}

public function infoForFile(string $sourcePath)
{
$path = $this->filesystem->createPath($sourcePath);
$classCandidates = $this->classToFileConverter->fileToClassCandidates(FilePath::fromString((string) $path));
$return = [
'class' => null,
'class_name' => null,
'class_namespace' => null,
];

if ($classCandidates->noneFound()) {
return $return;
}

$best = $classCandidates->best();

return [
'class' => (string) $best,
'class_name' => $best->name(),
'class_namespace' => $best->namespace(),
];
}

public function infoForOffset(string $sourcePath, int $offset): array
{
$path = $this->filesystem->createPath($sourcePath);
$type = $this->inference->inferTypeAtOffset(
SourceCode::fromString(
$this->filesystem->getContents($path)
),
Offset::fromInt($offset)
);

$return = [
'type' => (string) $type,
'path' => null,
];

if (InferredType::unknown() == $type) {
return $return;
}

$fileCandidates = $this->classToFileConverter->classToFileCandidates(ClassName::fromString((string) $type));
foreach ($fileCandidates as $candidate) {
if (file_exists((string) $candidate)) {
$return['path'] = (string) $candidate;
}
}

return $return;
}
}
Loading