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
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,30 @@ $ phpactor mv "Acme\\BlogPost" "Acme\\Article"
- Updates references for all moved classes in currently GIT tree.
- Use statements are updated or added when required.

### Class Search

Search for a class by its (short) name and return a list of fully qualified
names => absolute paths.

```bash
./bin/phpactor class:search Filesystem
DTL\Filesystem\Domain\Filesystem:/.../vendor/dantleech/source-code-filesystem/lib/Domain/Filesystem.php
Symfony\Component\Filesystem\Filesystem:/.../vendor/symfony/filesystem/Filesystem.php
```

Also returns JSON with `--format=json`

### Information at offset

Return the fully qualified name of the class at the offset in the given file:

```php
$ phpactor offset:info lib/Application/InformationForOffset/InformationForOffset.php 1382
type:DTL\ClassFileConverter\ClassName
path:/.../vendor/dtl/class-to-file/lib/ClassName.php
```
Also returns JSON with `--format=json`

Child Libraries
---------------

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

namespace Phpactor\Application\ClassSearch;

use DTL\Filesystem\Domain\Filesystem;
use DTL\ClassFileConverter\FileToClass;
use DTL\ClassFileConverter\FilePath;

final class ClassSearch
{
private $fileToClass;
private $filesystem;

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

public function classSearch(string $name)
{
$files = $this->filesystem->fileList()->named($name . '.php');

$results = [];
foreach ($files as $file) {
$result = [
'path' => (string) $file->absolutePath(),
'name' => null,
];

$candidates = $this->fileToClass->fileToClass(FilePath::fromString((string) $file->absolutePath()));

if (false === $candidates->noneFound()) {
$result['name'] = (string) $candidates->best();
}

$results[] = $result;
}

return $results;
}
}
29 changes: 17 additions & 12 deletions lib/Container/CoreExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use DTL\ClassFileConverter\Composer\ComposerClassToFile;
use DTL\ClassFileConverter\Composer\ComposerFileToClass;
use DTL\ClassFileConverter\CompositeTransformer;
use Phpactor\UserInterface\Console\Command\MoveCommand;
use Phpactor\UserInterface\Console\Command\ClassMoveCommand;
use Phpactor\Application\ClassMover\ClassMover as ClassMoverApp;
use DTL\Filesystem\Adapter\Git\GitFilesystem;
use DTL\Filesystem\Domain\Cwd;
Expand All @@ -18,6 +18,8 @@
use Phpactor\Application\InformationForOffset\InformationForOffset;
use DTL\TypeInference\TypeInference;
use Phpactor\UserInterface\Console\Command\InformationForOffsetCommand;
use Phpactor\Application\ClassSearch\ClassSearch;
use Phpactor\UserInterface\Console\Command\ClassSearchCommand;

class CoreExtension implements ExtensionInterface
{
Expand Down Expand Up @@ -45,20 +47,16 @@ public function load(Container $container)

private function registerConsole(Container $container)
{
$container->register('application', function (Container $container) {
$application = new Application(self::APP_NAME, self::APP_VERSION);
$application->addCommands([
$container->get('command.move'),
]);

return $application;
});

$container->register('command.move', function (Container $container) {
return new MoveCommand(
$container->register('command.class_move', function (Container $container) {
return new ClassMoveCommand(
$container->get('application.class_mover')
);
});
$container->register('command.class_search', function (Container $container) {
return new ClassSearchCommand(
$container->get('application.class_search')
);
});

$container->register('command.offsetinfo', function (Container $container) {
return new InformationForOffsetCommand(
Expand Down Expand Up @@ -151,5 +149,12 @@ private function registerApplicationServices(Container $container)
$container->get('source_code_filesystem.simple')
);
});

$container->register('application.class_search', function (Container $container) {
return new ClassSearch(
$container->get('source_code_filesystem.simple'),
$container->get('class_to_file.converter')
);
});
}
}
3 changes: 2 additions & 1 deletion lib/UserInterface/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public function initialize()
], []);
$container->init();
$this->addCommands([
$container->get('command.move'),
$container->get('command.class_move'),
$container->get('command.class_search'),
$container->get('command.offsetinfo'),
]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Phpactor\UserInterface\Console\Logger\SymfonyConsoleMoveLogger;
use Symfony\Component\Console\Input\InputOption;

class MoveCommand extends Command
class ClassMoveCommand extends Command
{
const TYPE_AUTO = 'auto';
const TYPE_CLASS = 'class';
Expand Down
61 changes: 61 additions & 0 deletions lib/UserInterface/Console/Command/ClassSearchCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Phpactor\UserInterface\Console\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Phpactor\Application\ClassSearchr\ClassSearchr;
use Symfony\Component\Console\Input\InputArgument;
use Phpactor\Phpactor;
use Phpactor\UserInterface\Console\Logger\SymfonyConsoleSearchLogger;
use Symfony\Component\Console\Input\InputOption;
use Phpactor\Application\ClassSearch\ClassSearch;

class ClassSearchCommand extends Command
{
public function __construct(
ClassSearch $search
) {
parent::__construct();
$this->search = $search;
}

public function configure()
{
$this->setName('class:search');
$this->setDescription('Search for class by (short) name and return informations on candidates');
$this->addArgument('name', InputArgument::REQUIRED, 'Source path or FQN');
Handler\FormatHandler::configure($this);
}

public function execute(InputInterface $input, OutputInterface $output)
{
$results = $this->search->classSearch($input->getArgument('name'));
$format = $input->getOption('format');

switch ($format) {
case Handler\FormatHandler::FORMAT_JSON:
$output->write(json_encode($results));
return;
case Handler\FormatHandler::FORMAT_CONSOLE:
return $this->outputConsole($output, $results);
}

throw new \InvalidArgumentException(sprintf(
'Invalid format "%s", known formats: "%s"',
$format, implode('", "', Handler\FormatHandler::VALID_FORMATS)
));
}

private function outputConsole(OutputInterface $output, array $results)
{
foreach ($results as $result) {
if (!$result['name']) {
continue;
}

$output->writeln(sprintf('%s:%s', $result['name'], $result['path']));
}
}
}
24 changes: 24 additions & 0 deletions lib/UserInterface/Console/Command/Handler/FormatHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Phpactor\UserInterface\Console\Command\Handler;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputOption;

class FormatHandler
{
const FORMAT_JSON = 'json';
const FORMAT_CONSOLE = 'console';

const VALID_FORMATS = [
self::FORMAT_JSON,
self::FORMAT_CONSOLE
];

public static function configure(Command $command)
{
$command->addOption('format', null, InputOption::VALUE_REQUIRED, sprintf(
'Output format: "%s"', implode('", "', self::VALID_FORMATS)
), 'console');
}
}
18 changes: 4 additions & 14 deletions lib/UserInterface/Console/Command/InformationForOffsetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,6 @@

class InformationForOffsetCommand extends Command
{
const FORMAT_JSON = 'json';
const FORMAT_CONSOLE = 'console';

const VALID_FORMATS = [
self::FORMAT_JSON,
self::FORMAT_CONSOLE
];

private $infoForOffset;

public function __construct(
Expand All @@ -37,9 +29,7 @@ public function configure()
$this->setDescription('Return information about given file at the given offset');
$this->addArgument('path', InputArgument::REQUIRED, 'Source path or FQN');
$this->addArgument('offset', InputArgument::REQUIRED, 'Destination path or FQN');
$this->addOption('format', null, InputOption::VALUE_REQUIRED, sprintf(
'Output format: "%s"', implode('", "', self::VALID_FORMATS)
), 'console');
Handler\FormatHandler::configure($this);
}

public function execute(InputInterface $input, OutputInterface $output)
Expand All @@ -52,16 +42,16 @@ public function execute(InputInterface $input, OutputInterface $output)
$format = $input->getOption('format');

switch ($format) {
case self::FORMAT_JSON:
case Handler\FormatHandler::FORMAT_JSON:
$output->write(json_encode($info));
return;
case self::FORMAT_CONSOLE:
case Handler\FormatHandler::FORMAT_CONSOLE:
return $this->outputConsole($output, $info);
}

throw new \InvalidArgumentException(sprintf(
'Invalid format "%s", known formats: "%s"',
$format, implode('", "', self::VALID_FORMATS)
$format, implode('", "', Handler\FormatHandler::VALID_FORMATS)
));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Phpactor\Tests\UserInterface\SystemTestCase;

class MoveCommandTest extends SystemTestCase
class ClassMoveCommandTest extends SystemTestCase
{
public function setUp()
{
Expand Down
35 changes: 35 additions & 0 deletions tests/UserInterface/Console/Command/ClassSearchCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Phpactor\Tests\UserInterface\Console\Command;

use Phpactor\Tests\UserInterface\SystemTestCase;

class ClassSearchCommandTest extends SystemTestCase
{
public function setUp()
{
$this->initWorkspace();
$this->loadProject('Animals');
}

/**
* @testdox It should return information baesd on a class "short" name.
*/
public function testSearchName()
{
$process = $this->phpactor('class:search "Badger"');
$this->assertSuccess($process);
$this->assertContains('Badger.php', $process->getOutput());
}

/**
* @testdox It should return information baesd on a class "short" name.
*/
public function testSearchNameJson()
{
$process = $this->phpactor('class:search "Badger" --format=json');
$this->assertSuccess($process);
$this->assertContains('Badger.php"', $process->getOutput());
}
}