Skip to content

Commit

Permalink
Merge pull request #1 from getpop/rector
Browse files Browse the repository at this point in the history
Rector
  • Loading branch information
leoloso authored Sep 1, 2020
2 parents 8328e72 + 70b694c commit c205230
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 35 deletions.
31 changes: 26 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
########################################################################
# This pipeline is executed on 2 environments: PHP 7.4 and PHP 7.2
# PHP 7.4 is for development
# PHP 7.2 is for production (optional), downgrading the code via Rector
#
# Concerning PHP 7.2:
#-----------------------------------------------------------------------
# composer.json corresponds to PHP 7.4,
# so the constraints for dependencies must be downgraded:
# - php: ~7.2
# - phpunit/phpunit: <= remove
#
# PHPUnit will not work, because phpunit/phpunit version 9.3 requires PHP 7.3+.
# If running PHPStan fails, then downgrading the code has issues.
########################################################################
dist: trusty
language: php

php:
- 7.2
- 7.3
- 7.4

## Cache composer
Expand All @@ -18,15 +32,22 @@ cache:
# env: 'COMPOSER_FLAGS="--prefer-stable --prefer-lowest"'

before_script:
- travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-dist
# For PHP 7.2 replace PHP version constraint from 7.4 to 7.2, and remove PHPUnit
- if [[ ${TRAVIS_PHP_VERSION:0:3} == "7.2" ]]; then composer require php:~7.2 --no-update --no-scripts; fi
- if [[ ${TRAVIS_PHP_VERSION:0:3} == "7.2" ]]; then composer remove phpunit/phpunit --dev --no-update --no-scripts; fi
- travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-dist
# For PHP 7.2 downgrade the code via Rector
- if [[ ${TRAVIS_PHP_VERSION:0:3} == "7.2" ]]; then vendor/bin/rector process src; fi

script:
- vendor/bin/phpcs -n --standard=psr2 src/
- vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover
# Tests only on PHP 7.4
- if [[ ${TRAVIS_PHP_VERSION:0:3} != "7.2" ]]; then vendor/bin/phpcs -n --standard=psr2 src/; fi
- if [[ ${TRAVIS_PHP_VERSION:0:3} != "7.2" ]]; then vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover; fi
- vendor/bin/phpstan analyse -c phpstan.neon.dist src

after_script:
- wget https://scrutinizer-ci.com/ocular.phar && php ocular.phar code-coverage:upload --format=php-clover coverage.clover
# Tests only on PHP 7.4
- if [[ ${TRAVIS_PHP_VERSION:0:3} != "7.2" ]]; then wget https://scrutinizer-ci.com/ocular.phar && php ocular.phar code-coverage:upload --format=php-clover coverage.clover; fi

notifications:
email:
Expand Down
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,27 @@ Initialize the component:
]);
```

## PHP versions

Allowed PHP code:

- PHP 7.2
- Typed properties from PHP 7.4

The code can be downgraded from PHP 7.4 to PHP 7.2 to run in production:

| | Development | Production |
| --- | --- | --- |
| **Min PHP version** | 7.4 | 7.2 (After removing typed properties) |

### Downgrading PHP code

Dry run to downgrade the code, via [Rector](https://github.com/rectorphp/rector):

```bash
composer downgrade-code
```

## Standards

[PSR-1](https://www.php-fig.org/psr/psr-1), [PSR-4](https://www.php-fig.org/psr/psr-4) and [PSR-12](https://www.php-fig.org/psr/psr-12).
Expand Down
8 changes: 5 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@
}
],
"require": {
"php": "~7.2",
"php": "~7.4",
"symfony/config": "^4.3",
"symfony/dependency-injection": "^4.3",
"symfony/dotenv": "^4.3",
"symfony/yaml": "^4.3"
},
"require-dev": {
"phpstan/phpstan": "^0.12",
"phpunit/phpunit": ">=8.5",
"phpunit/phpunit": ">=9.3",
"rector/rector": "^0.7.65",
"squizlabs/php_codesniffer": "^3.0"
},
"autoload": {
Expand All @@ -41,7 +42,8 @@
"test": "phpunit",
"check-style": "phpcs src tests",
"fix-style": "phpcbf src tests",
"analyse": "phpstan analyse -c phpstan.neon.dist"
"analyse": "phpstan analyse -c phpstan.neon.dist",
"downgrade-code": "vendor/bin/rector process --dry-run"
},
"extra": {
"branch-alias": {
Expand Down
45 changes: 19 additions & 26 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="getpop Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
<logging>
<log type="junit" target="build/report.junit.xml"/>
<log type="coverage-html" target="build/coverage"/>
<log type="coverage-text" target="build/coverage.txt"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" backupGlobals="false" backupStaticAttributes="false" colors="true" verbose="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory suffix=".php">src/</directory>
</include>
<report>
<clover outputFile="build/logs/clover.xml"/>
<html outputDirectory="build/coverage"/>
<text outputFile="build/coverage.txt"/>
</report>
</coverage>
<testsuites>
<testsuite name="getpop Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<logging>
<junit outputFile="build/report.junit.xml"/>
</logging>
</phpunit>
30 changes: 30 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

use Rector\Core\Configuration\Option;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Rector\Set\ValueObject\SetList;

return static function (ContainerConfigurator $containerConfigurator): void {
// get parameters
$parameters = $containerConfigurator->parameters();

// paths to refactor; solid alternative to CLI arguments
$parameters->set(Option::PATHS, [
__DIR__ . '/src',
]);

// is there a file you need to skip?
$parameters->set(Option::EXCLUDE_PATHS, [
__DIR__ . '/vendor/getpop/migrate-*/*',
]);

// here we can define, what sets of rules will be applied
$parameters->set(Option::SETS, [
SetList::DOWNGRADE
]);

// is your PHP version different from the one your refactor to? [default: your PHP version]
$parameters->set(Option::PHP_VERSION_FEATURES, '7.2');
};
7 changes: 6 additions & 1 deletion src/Container/ContainerBuilderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ public static function init(
$directory .= \DIRECTORY_SEPARATOR . $namespace;
}
if (!is_dir($directory)) {
@mkdir($directory, 0777, true);
if (@mkdir($directory, 0777, true) === false) {
throw new \RuntimeException(sprintf(
'The directory %s could not be created.',
$directory
));
}
}
$directory .= \DIRECTORY_SEPARATOR;
// On Windows the whole path is limited to 258 chars
Expand Down
24 changes: 24 additions & 0 deletions tests/CompontentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace PoP\Root;

use PHPUnit\Framework\TestCase;

class ComponentTest extends TestCase
{
/**
* The root component cannot have any dependency
*/
public function testHasNoDependencies(): void
{
$this->assertEmpty(
Component::getDependedComponentClasses()
);
$this->assertEmpty(
Component::getDependedConditionalComponentClasses()
);
$this->assertEmpty(
Component::getDependedMigrationPlugins()
);
}
}

0 comments on commit c205230

Please sign in to comment.