Skip to content
This repository was archived by the owner on Jul 12, 2020. It is now read-only.

Allow stdin input for single file refactors #69

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Based on data from these sources the Refactoring Browser consists of two distinc

## Install & Basic Usage

[Download PHAR](https://github.com/QafooLabs/php-refactoring-browser/releases)
[Download PHAR](https://github.com/FlickerBean/php-refactoring-browser/releases/tag/v0.1.1)

The refactoring browser is used with:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,20 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$file = File::createFromPath($input->getArgument('file'), getcwd());
$filename = $input->getArgument('file');
if ('-' == $filename) {
$filename = false;
$contents = '';
while (!feof(STDIN)) {
$contents .= fread(STDIN, 1024);
}
}
if ($filename) {
$file = File::createFromPath($filename, getcwd());
} else {
$file = File::createFromContents($contents, getcwd());
}

$line = (int)$input->getArgument('line');
$variable = new Variable($input->getArgument('variable'));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,20 @@ function and generates the argument list and return statement accordingly.

protected function execute(InputInterface $input, OutputInterface $output)
{
$file = File::createFromPath($input->getArgument('file'), getcwd());
$filename = $input->getArgument('file');
if ('-' == $filename) {
$filename = false;
$contents = '';
while (!feof(STDIN)) {
$contents .= fread(STDIN, 1024);
}
}
if ($filename) {
$file = File::createFromPath($filename, getcwd());
} else {
$file = File::createFromContents($contents, getcwd());
}

$range = LineRange::fromString($input->getArgument('range'));
$newMethodName = $input->getArgument('newmethod');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,19 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$file = File::createFromPath($input->getArgument('file'), getcwd());
$filename = $input->getArgument('file');
if ('-' == $filename) {
$filename = false;
$contents = '';
while (!feof(STDIN)) {
$contents .= fread(STDIN, 1024);
}
}
if ($filename) {
$file = File::createFromPath($filename, getcwd());
} else {
$file = File::createFromContents($contents, getcwd());
}

$codeAnalysis = new StaticCodeAnalysis();
$editor = new PatchEditor(new OutputPatchCommand($output));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,20 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$file = File::createFromPath($input->getArgument('file'), getcwd());
$filename = $input->getArgument('file');
if ('-' == $filename) {
$filename = false;
$contents = '';
while (!feof(STDIN)) {
$contents .= fread(STDIN, 1024);
}
}
if ($filename) {
$file = File::createFromPath($filename, getcwd());
} else {
$file = File::createFromContents($contents, getcwd());
}

$line = (int)$input->getArgument('line');
$name = new Variable($input->getArgument('name'));
$newName = new Variable($input->getArgument('new-name'));
Expand Down
26 changes: 26 additions & 0 deletions src/main/QafooLabs/Refactoring/Domain/Model/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class File
{
private $relativePath;
private $code;
private $temp_file;

/**
* @param string $path
Expand All @@ -45,6 +46,31 @@ public static function createFromPath($path, $workingDirectory)
return new self($relativePath, $code);
}

/**
* @param mixed $content
* @param mixed $workingDirectory
*
* @return File
*/
public static function createFromContents($content, $workingDirectory)
{
$temp = tmpfile();
$meta_datas = stream_get_meta_data($temp);
$tmp_filename = $meta_datas['uri'];
fwrite($temp, $content);

// Now we move the file to prevent it being instantly deleted at the
// end of the script. This allows piping it to 'patch' for instance,
// which requires the original file.
$new_tmp_filename = $tmp_filename.'1';
if (!rename($tmp_filename, $new_tmp_filename)) {
throw new RuntimeException("Could not move temporary file " . $tmp_filename);
}

return File::createFromPath($new_tmp_filename, $workingDirectory);
}


public function __construct($relativePath, $code)
{
$this->relativePath = $relativePath;
Expand Down