Skip to content

Commit

Permalink
Enhancement: Add support for rector/rector:~0.6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Jan 8, 2020
1 parent fde60a3 commit cb776ed
Show file tree
Hide file tree
Showing 9 changed files with 1,205 additions and 3 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ script:
exit 1;
fi
- cd generator/tests/rector/0.5 && composer install && composer rector && composer test && cd ../../../..
- cd generator/tests/rector/0.6 && composer install && composer rector && composer test && cd ../../../..

after_script:
- cd generator && travis_retry php vendor/bin/php-coveralls -v --root_dir="./generator"
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,13 @@ tool that performs instant refactoring of your application.
First, you need to install Rector:
```bash
$ composer require --dev rector/rector ^0.5
$ composer require --dev rector/rector ^0.6
```

Now, you simply need to run Rector with this command:

```bash
vendor/bin/rector process src/ --config vendor/thecodingmachine/safe/rector-migrate.yml
vendor/bin/rector process src/ --config vendor/thecodingmachine/safe/rector-migrate-0.6.yml
```

*Note:* do not forget to replace "src/" with the path to your source directory.
Expand Down
35 changes: 35 additions & 0 deletions generator/src/FileCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ public function generateFunctionsList(array $functions, string $path): void
}

/**
* @deprecated Official support for rector/rector:~0.5 is not advertised anymore.
*
* This function generate a rector yml file containing a replacer for all functions
*
* @param Method[] $functions
Expand All @@ -140,6 +142,39 @@ public function generateRectorFile(array $functions, string $path): void
fclose($stream);
}

/**
* Generates a configuration file for replacing all functions when using rector/rector:~0.6.
*
* @param Method[] $functions
* @param string $path
*/
public function generateRectorFileForZeroPointSix(array $functions, string $path): void
{
$functionNames = $this->getFunctionsNameList($functions);

$stream = fopen($path, 'w');

if ($stream === false) {
throw new \RuntimeException('Unable to write to '.$path);
}

$header = <<<'TXT'
# This file configures rector/rector:~0.6.0 to replace all PHP functions with their equivalent "safe" functions
services:
Rector\Renaming\Rector\Function_\RenameFunctionRector:
$oldFunctionToNewFunction:

TXT;

fwrite($stream, $header);

foreach ($functionNames as $functionName) {
fwrite($stream, ' '.$functionName.": 'Safe\\".$functionName."'\n");
}

fclose($stream);
}

public function createExceptionFile(string $moduleName): void
{
Expand Down
3 changes: 2 additions & 1 deletion generator/src/GenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$fileCreator->generatePhpFile($functions, __DIR__ . '/../../generated/');
$fileCreator->generateFunctionsList($functions, __DIR__ . '/../../generated/functionsList.php');
$fileCreator->generateRectorFile($functions, __DIR__ . '/../../rector-migrate.yml');
$fileCreator->generateRectorFileForZeroPointSix($functions, __DIR__ . '/../../rector-migrate-0.6.yml');


$modules = [];
Expand Down Expand Up @@ -75,7 +76,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
// Finally, let's edit the composer.json file
$output->writeln('Editing composer.json');
ComposerJsonEditor::editFiles(\array_values($modules));

return 0;
}

Expand Down
18 changes: 18 additions & 0 deletions generator/tests/rector/0.6/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"autoload": {
"psr-4": {
"Test\\": "src/"
}
},
"require": {
"php": "^7.2",
"rector/rector": "^0.6.7"
},
"require-dev": {
"phpunit/phpunit": "^7"
},
"scripts": {
"rector": "rector process src/ --config ../../../../rector-migrate-0.6.yml",
"test": "phpunit"
}
}
18 changes: 18 additions & 0 deletions generator/tests/rector/0.6/phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
bootstrap="vendor/autoload.php"
>
<testsuites>
<testsuite name="Main test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
3 changes: 3 additions & 0 deletions generator/tests/rector/0.6/src/test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

glob('*');
13 changes: 13 additions & 0 deletions generator/tests/rector/0.6/tests/RectorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

use PHPUnit\Framework\TestCase;

final class RectorTest extends TestCase
{
public function testRectorSucceeded()
{
$content = file_get_contents(__DIR__.'/../src/test.php');

$this->assertContains('Safe', $content);
}
}
Loading

0 comments on commit cb776ed

Please sign in to comment.