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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ It will package libraries in separate, decoupled libraries
- [dantleech/class-to-file](https://github.com/dantleech/class-to-file): Convert files to class names and vice-versa.
- [dantleech/class-mover](https://github.com/dantleech/class-mover): Find and update class references.
- [dantleech/source-code-filesystem](https://github.com/dantleech/source-code-filesystem): Find and manage source code files.
- [dantleech/type-inference](https://github.com/dantleech/type-inference): Determine type of thing at a given offset (very limited actually).

About this project
------------------
Expand Down
1 change: 1 addition & 0 deletions bin/phpactor.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@
$application->run(null, $output);
} catch (\Exception $e) {
$application->renderException($e, $output);
exit(255);
}
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@
{
"type": "vcs",
"url": "git@github.com:dantleech/source-code-filesystem"
},
{
"type": "vcs",
"url": "git@github.com:dantleech/type-inference"
}
],
"require": {
"symfony/console": "~2.7|^3.0",
"phpbench/container": "^1.0",
"dantleech/class-mover": "dev-master",
"dantleech/source-code-filesystem": "dev-master",
"dantleech/type-inference": "dev-master",
"symfony/filesystem": "^3.3"
},
"require-dev": {
Expand Down
1 change: 0 additions & 1 deletion lib/Application/ClassMover/ClassMover.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

namespace Phpactor\Application\ClassMover;

use DTL\ClassFileConverter\CompositeTransformer;
Expand Down
60 changes: 60 additions & 0 deletions lib/Application/InformationForOffset/InformationForOffset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Phpactor\Application\InformationForOffset;

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

final class InformationForOffset
{
private $inference;
private $classToFileConverter;
private $filesystem;

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

public function infoForOffset(string $sourcePath, int $offset): array
{
$path = $this->filesystem->createPath($sourcePath);
$type = $this->inference->inferTypeAtOffset(
$this->filesystem->getContents($path),
$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;

}
}
29 changes: 29 additions & 0 deletions lib/Container/CoreExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
use DTL\Filesystem\Adapter\Git\GitFilesystem;
use DTL\Filesystem\Domain\Cwd;
use DTL\ClassMover\ClassMover;
use DTL\Filesystem\Adapter\Simple\SimpleFilesystem;
use Phpactor\Application\InformationForOffset\InformationForOffset;
use DTL\TypeInference\TypeInference;
use Phpactor\UserInterface\Console\Command\InformationForOffsetCommand;

class CoreExtension implements ExtensionInterface
{
Expand All @@ -34,6 +38,7 @@ public function load(Container $container)
$this->registerComposer($container);
$this->registerClassToFile($container);
$this->registerClassMover($container);
$this->registerTypeInference($container);
$this->registerSourceCodeFilesystem($container);
$this->registerApplicationServices($container);
}
Expand All @@ -54,6 +59,12 @@ private function registerConsole(Container $container)
$container->get('application.class_mover')
);
});

$container->register('command.offsetinfo', function (Container $container) {
return new InformationForOffsetCommand(
$container->get('application.information_for_offset')
);
});
}

private function registerComposer(Container $container)
Expand Down Expand Up @@ -111,6 +122,16 @@ private function registerSourceCodeFilesystem(Container $container)
$container->register('source_code_filesystem.git', function (Container $container) {
return new GitFilesystem(Cwd::fromCwd($container->getParameter('cwd')));
});
$container->register('source_code_filesystem.simple', function (Container $container) {
return new SimpleFilesystem(Cwd::fromCwd($container->getParameter('cwd')));
});
}

private function registerTypeInference(Container $container)
{
$container->register('type_inference.type_inference', function (Container $container) {
return new TypeInference();
});
}

private function registerApplicationServices(Container $container)
Expand All @@ -122,5 +143,13 @@ private function registerApplicationServices(Container $container)
$container->get('source_code_filesystem.git')
);
});

$container->register('application.information_for_offset', function (Container $container) {
return new InformationForOffset(
$container->get('type_inference.type_inference'),
$container->get('class_to_file.converter'),
$container->get('source_code_filesystem.simple')
);
});
}
}
1 change: 1 addition & 0 deletions lib/UserInterface/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function initialize()
$container->init();
$this->addCommands([
$container->get('command.move'),
$container->get('command.offsetinfo'),
]);
}
}
76 changes: 76 additions & 0 deletions lib/UserInterface/Console/Command/InformationForOffsetCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?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\ClassInformationForOffsetr\ClassInformationForOffsetr;
use Symfony\Component\Console\Input\InputArgument;
use Phpactor\Phpactor;
use Phpactor\UserInterface\Console\Logger\SymfonyConsoleInformationForOffsetLogger;
use Symfony\Component\Console\Input\InputOption;
use Phpactor\Application\InformationForOffset\InformationForOffset;

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(
InformationForOffset $infoForOffset
) {
parent::__construct();
$this->infoForOffset = $infoForOffset;
}

public function configure()
{
$this->setName('offset:info');
$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');
}

public function execute(InputInterface $input, OutputInterface $output)
{
$info = $this->infoForOffset->infoForOffset(
$input->getArgument('path'),
$input->getArgument('offset')
);

$format = $input->getOption('format');

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

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

private function outputConsole(OutputInterface $output, array $info)
{
foreach ($info as $key => $value) {
$output->writeln(sprintf(
'%s: %s', $key, $value
));
}
}
}
4 changes: 2 additions & 2 deletions lib/UserInterface/Console/Command/MoveCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public function __construct(

public function configure()
{
$this->setName('mv');
$this->setDescription('Move file (or directory) and magically update references to class contained.');
$this->setName('class:move');
$this->setDescription('Move class (by name or file path) and update all references to it');
$this->addArgument('src', InputArgument::REQUIRED, 'Source path or FQN');
$this->addArgument('dest', InputArgument::REQUIRED, 'Destination path or FQN');
$this->addOption('type', null, InputOption::VALUE_REQUIRED, sprintf(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace Phpactor\Tests\System\Console\Command;
namespace Phpactor\Tests\UserInterface\Console\Command;

use Phpactor\Tests\System\SystemTestCase;
use Phpactor\Tests\UserInterface\SystemTestCase;

class MoveCommandTest extends SystemTestCase
{
Expand Down Expand Up @@ -38,36 +38,36 @@ public function provideSmokeSuccess()
{
return [
'Move file 1' => [
'mv lib/Badger/Carnivorous.php lib/Aardvark/Insectarian.php',
'class:move lib/Badger/Carnivorous.php lib/Aardvark/Insectarian.php',
],
'Move file 2' => [
'mv lib/Aardvark/Edentate.php lib/Foobar.php',
'class:move lib/Aardvark/Edentate.php lib/Foobar.php',
[
'lib/Foobar.php' => true,
'lib/Aardvark/Edentate.php' => false,
],
],
'Move file non-existing folder' => [
'mv lib/Aardvark/Edentate.php lib/Hello/World/Foobar.php',
'class:move lib/Aardvark/Edentate.php lib/Hello/World/Foobar.php',
[
'lib/Hello/World/Foobar.php' => true,
],
],
'Move file force' => [
'mv lib/Aardvark/Edentate.php lib/Foobar.php --type=file',
'class:move lib/Aardvark/Edentate.php lib/Foobar.php --type=file',
],
'Move folder 1' => [
'mv lib/Aardvark lib/Elephant',
'class:move lib/Aardvark lib/Elephant',
[
'lib/Aardvark' => false,
'lib/Elephant/Edentate.php' => true,
],
],
'Move class by name 1' => [
'mv "Animals\\Badger\\Carnivorous" "Animals\\Badger\\Vicious"',
'class:move "Animals\\Badger\\Carnivorous" "Animals\\Badger\\Vicious"',
],
'Move class by name force' => [
'mv "Animals\\Badger\\Carnivorous" "Animals\\Badger\\Vicious" --type=class',
'class:move "Animals\\Badger\\Carnivorous" "Animals\\Badger\\Vicious" --type=class',
],
];
}
Expand All @@ -91,11 +91,11 @@ public function provideSmokeFailure()
null,
],
'Move class by file force class' => [
'mv lib/Aardvark/Edentate.php lib/Foobar.php --type=class',
'class:move lib/Aardvark/Edentate.php lib/Foobar.php --type=class',
null,
],
'Move invalid type' => [
'mv lib/Aardvark/Edentate.php lib/Foobar.php --type=foobar',
'class:move lib/Aardvark/Edentate.php lib/Foobar.php --type=foobar',
'Invalid type "foobar", must be one of: "auto", "file", "class"',
],
];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Phpactor\Tests\UserInterface\Console\Command;

use Phpactor\Tests\UserInterface\SystemTestCase;

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

/**
* @testdox It provides information about the thing under the cursor.
*/
public function testProvideInformationForOffset()
{
$process = $this->phpactor('offset:info lib/Badger.php 137');
$this->assertSuccess($process);
$this->assertContains('type: Animals\Badger\Carnivorous', $process->getOutput());
$this->assertContains('Badger/Carnivorous.php', $process->getOutput());
}

/**
* @testdox It provides information about the thing under the cursor as JSON
*/
public function testProvideInformationForOffsetAsJson()
{
$process = $this->phpactor('offset:info lib/Badger.php 137 --format=json');
$this->assertSuccess($process);
$this->assertContains('{"type":"Animals', $process->getOutput());
}

/**
* @testdox It throws an exception if an invalid format is passed
*/
public function testProvideInformationForOffsetAsInvalid()
{
$process = $this->phpactor('offset:info lib/Badger.php 137 --format=foobar');
$this->assertFailure($process, 'Invalid format');
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Phpactor\Tests\System;
namespace Phpactor\Tests\UserInterface;

use Symfony\Component\Process\Process;
use Symfony\Component\Filesystem\Filesystem;
Expand Down