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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ Relocate all classes under `Acme` to `Foobar`:
$ phpactor mv lib/Acme lib/Foobar
```

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

```bash
$ phpactor mv lib/* lib/Core
```

Move a class by name:

```bash
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"dantleech/class-mover": "dev-master",
"dantleech/source-code-filesystem": "dev-master",
"dantleech/type-inference": "dev-master",
"symfony/filesystem": "^3.3"
"symfony/filesystem": "^3.3",
"webmozart/glob": "^4.1"
},
"require-dev": {
"phpunit/phpunit": "^5.2",
Expand Down
22 changes: 17 additions & 5 deletions lib/Application/ClassMover/ClassMover.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use Phpactor\Application\ClassMover\MoveOperation;
use Phpactor\Phpactor;
use DTL\ClassFileConverter\ClassName;
use Webmozart\Glob\Glob;
use Webmozart\PathUtil\Path;

class ClassMover
{
Expand Down Expand Up @@ -66,16 +68,26 @@ public function moveClass(MoveLogger $logger, string $srcName, string $destName)

public function moveFile(MoveLogger $logger, string $srcPath, string $destPath)
{
try {
$this->doMoveFile($logger, $srcPath, $destPath);
} catch (\Exception $e) {
throw new \RuntimeException(sprintf('Could not move file "%s" to "%s"', $srcPath, $destPath), null, $e);
$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->doMoveFile($logger, $globPath, $globDest);
} catch (\Exception $e) {
throw new \RuntimeException(sprintf('Could not move file "%s" to "%s"', $srcPath, $destPath), null, $e);
}
}
}

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

$srcPath = $this->filesystem->createPath($srcPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function replacing(FilePath $path, FoundReferences $references, FullyQual
if ($references->references()->isEmpty()) {
return;
}
$this->output->writeln('<comment># '.$path.'</>');
$this->output->writeln('<info>[REPL]</> <comment>'.$path.'</>');

foreach ($references->references() as $reference) {
$this->output->writeln(sprintf(
Expand Down
8 changes: 8 additions & 0 deletions tests/UserInterface/Console/Command/ClassMoveCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ public function provideSmokeSuccess()
'lib/Elephant/Edentate.php' => true,
],
],
'Move wildcard' => [
'class:move "lib/*" lib/Foobar',
[
'lib/Foobar/Aardvark' => true,
'lib/Foobar/Badger.php' => true,
'lib/Badger.php' => false,
],
],
'Move class by name 1' => [
'class:move "Animals\\Badger\\Carnivorous" "Animals\\Badger\\Vicious"',
],
Expand Down