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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Bug fixes:
- [code-builder] Class import doesn't work with single element namespace
#760
- [code-builder] Variant is not passed to class generator (#766)
- [phpactor CLI] response shows source code instead of path (#792)

BC Break:

Expand Down
45 changes: 28 additions & 17 deletions lib/Extension/CodeTransformExtra/Command/ClassNewCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Phpactor\Extension\CodeTransformExtra\Command;

use Phpactor\CodeTransform\Domain\SourceCode;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -59,33 +60,43 @@ private function process(InputInterface $input, OutputInterface $output)
{
$src = $input->getArgument('src');
$variant = $input->getOption('variant');
$response = [
'src' => $src,
'path' => null,
'exists' => false,
];

try {
$response['path'] = $this->classNew->generate($src, $variant, $input->getOption('force'));
$sourceCode = $this->generateSourceCode($src, $variant, $input, $output);
} catch (FileAlreadyExists $exception) {
$questionHelper = new QuestionHelper();
$question = new ConfirmationQuestion('<question>File already exists, overwrite? [y/n]</>', false);

if (false === $questionHelper->ask($input, $output, $question)) {
$response['exists'] = true;
return $response;
}

$filePath = $this->classNew->generate($src, $variant, true);
$response['path'] = $filePath;
return [
'src' => $src,
'path' => null,
'exists' => true,
];
}

return $response;
return [
'src' => $src,
'path' => $sourceCode->path(),
'exists' => false,
];
}

private function listGenerators(InputInterface $input, OutputInterface $output)
{
$dumper = $this->dumperRegistry->get($input->getOption('format'));
$dumper->dump($output, $this->classNew->availableGenerators());
}

private function generateSourceCode(string $src, string $variant, InputInterface $input, OutputInterface $output): SourceCode
{
try {
return $this->classNew->generate($src, $variant, $input->getOption('force'));
} catch (FileAlreadyExists $exception) {
$questionHelper = new QuestionHelper();
$question = new ConfirmationQuestion('<question>File already exists, overwrite? [y/n]</>', false);

if (false === $questionHelper->ask($input, $output, $question)) {
throw $exception;
}

return $this->classNew->generate($src, $variant, true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,57 @@ public function setUp()
{
$this->workspace()->reset();
$this->loadProject('Animals');
$this->workspace()->put('.phpactor/templates/foobar/SourceCode.php.twig', 'Foobar');
$this->workspace()->put(
'.phpactor.json',
<<<EOF
{
"code_transform.class_new.variants": {
"foobar": "foobar"
}
}
EOF
);
}

/**
* Application level smoke tests
*
* @dataProvider provideNewClass
*/
public function testNewClass($command, $expected)
public function testNewClass($command, $expected, $expectedFile)
{
$process = $this->phpactor($command);
$this->assertSuccess($process);

$this->assertContains($expected, trim($process->getOutput()));
$this->assertFileExists($this->workspaceDir() . '/lib/Badger/Teeth.php');
$this->assertFileExists($this->workspaceDir() . $expectedFile);
}

public function provideNewClass()
{
return [
'New class' => [
'class:new lib/Badger/Teeth.php --no-interaction --force',
<<<'EOT'
yield 'New class' => [
'class:new lib/Badger/Teeth.php --no-interaction --force',
<<<'EOT'
src:lib/Badger/Teeth.php
EOT
, '/lib/Badger/Teeth.php'
];

yield 'New class with variant' => [
'class:new lib/Badger/Teeth.php --variant=foobar --no-interaction --force',
<<<'EOT'
src:lib/Badger/Teeth.php
EOT
],
, '/lib/Badger/Teeth.php'
];

yield 'New class from FQN' => [
'class:new "Animals\\Pigeon" --no-interaction',
<<<'EOT'
lib/Pigeon.php
EOT
, '/lib/Pigeon.php'
];
}
}