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 7, 2020
1 parent fde60a3 commit f0fe776
Show file tree
Hide file tree
Showing 15 changed files with 2,415 additions and 1,129 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ matrix:
- php: 7.2
env: PREFER_LOWEST=""
allow_failures:
# PHP 7.4 testing is allowed to fail because the container isn't ready yet
# PHP 7.4 testing is allowed to fail because the container isn't ready yet
- php: 7.4
env: PREFER_LOWEST=""

Expand Down Expand Up @@ -61,7 +61,9 @@ script:
echo "Generated files are different from commited files. Please run './safe.php generate' command and commit the results."
exit 1;
fi
- cd generator/tests/rector/default && composer install && composer rector && composer test && cd ../../../..
- 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"
38 changes: 31 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ $content = file_get_contents('foobar.json');
$foobar = json_decode($content);
```

All PHP functions that can return `false` on error are part of Safe.
All PHP functions that can return `false` on error are part of Safe.
In addition, Safe also provide 2 'Safe' classes: `Safe\DateTime` and `Safe\DateTimeImmutable` whose methods will throw exceptions instead of returning false.

## PHPStan integration
Expand Down Expand Up @@ -111,18 +111,42 @@ find these functions but changing the namespace of the functions one function at
Fortunately, Safe comes bundled with a "Rector" configuration file. [Rector](https://github.com/rectorphp/rector) is a command-line
tool that performs instant refactoring of your application.
First, you need to install Rector:
### `rector/rector:~0.5.0`

Run

```bash
$ composer require --dev rector/rector:~0.5
```

to install `rector/rector:~0.5`.

Run

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

to automatically refactor.

### `rector/rector:~0.6`

Run

```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:
to install `rector/rector:~0.6`.

Run

```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
```

to automatically refactor.

*Note:* do not forget to replace "src/" with the path to your source directory.

**Important:** the refactoring only performs a "dumb" replacement of functions. It will not modify the way
Expand Down Expand Up @@ -159,8 +183,8 @@ try {

Safe is loading 1000+ functions from ~85 files on each request. Yet, the performance impact of this loading is quite low.

In case you worry, using Safe will "cost" you ~700µs on each request. The [performance section](performance/README.md)
contains more information regarding the way we tested the performance impact of Safe.
In case you worry, using Safe will "cost" you ~700µs on each request. The [performance section](performance/README.md)
contains more information regarding the way we tested the performance impact of Safe.

## Learn more

Expand Down
54 changes: 48 additions & 6 deletions generator/src/FileCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,27 +116,69 @@ public function generateFunctionsList(array $functions, string $path): void
}

/**
* This function generate a rector yml file containing a replacer for all functions
* Generates a configuration file for replacing all functions when using rector/rector:~0.5.
*
* @param Method[] $functions
* @param string $path
*/
public function generateRectorFile(array $functions, string $path): void
public function generateRectorFileForZeroPointFive(array $functions, string $path): void
{
$functionNames = $this->getFunctionsNameList($functions);

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

if ($stream === false) {
throw new \RuntimeException('Unable to write to '.$path);
}
fwrite($stream, "# This rector file is replacing all core PHP functions with the equivalent \"safe\" functions
# It is targetting Rector 0.5.x versions.
# If you are using Rector 0.4, please upgrade your Rector version

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

TXT;

fwrite($stream, $header);

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

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);
}

Expand Down
5 changes: 3 additions & 2 deletions generator/src/GenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
$fileCreator = new FileCreator();
$fileCreator->generatePhpFile($functions, __DIR__ . '/../../generated/');
$fileCreator->generateFunctionsList($functions, __DIR__ . '/../../generated/functionsList.php');
$fileCreator->generateRectorFile($functions, __DIR__ . '/../../rector-migrate.yml');
$fileCreator->generateRectorFileForZeroPointFive($functions, __DIR__ . '/../../rector-migrate-0.5.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.1",
"rector/rector": "^0.6"
},
"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);
}
}
18 changes: 18 additions & 0 deletions generator/tests/rector/default/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"autoload": {
"psr-4": {
"Test\\": "src/"
}
},
"require": {
"php": ">=7.1",
"rector/rector": "^0.5"
},
"require-dev": {
"phpunit/phpunit": "^7"
},
"scripts": {
"rector": "rector process src/ --config ../../../../rector-migrate.yml",
"test": "phpunit"
}
}
18 changes: 18 additions & 0 deletions generator/tests/rector/default/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/default/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/default/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 f0fe776

Please sign in to comment.