Skip to content

Commit

Permalink
PHAR compiler inside phpstan/phpstan-src
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Nov 4, 2019
1 parent 37927e0 commit 3398198
Show file tree
Hide file tree
Showing 36 changed files with 721 additions and 12 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
/build/phpstan-generated.neon
/compiler/composer.lock
/compiler/tmp
/compiler/vendor
/composer.lock
/conf/config.local.yml
/vendor
Expand Down
26 changes: 15 additions & 11 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,24 @@ script:

jobs:
include:
- stage: test
name: Compiler tests
script:
- |
cd compiler && \
composer install --no-interaction && \
vendor/bin/phpunit -c tests/phpunit.xml tests && \
../bin/phpstan analyse -l 8 src && \
./run-e2e-tests.sh $TRAVIS_COMMIT
- stage: phar
name: PHAR
script:
- |
git clone https://github.com/phpstan/phpstan-compiler && \
cd phpstan-compiler && \
./run-e2e-tests.sh $TRAVIS_COMMIT && \
cd .. && \
composer install --working-dir=phpstan-compiler && \
php phpstan-compiler/bin/compile $TRAVIS_COMMIT && \
git clone https://${GITHUB_TOKEN}@github.com/phpstan/phpstan.git > /dev/null 2>&1 && \
cp phpstan-compiler/tmp/phpstan.phar phpstan/phpstan.phar && \
cp phpstan-compiler/tmp/phpstan.phar phpstan/phpstan && \
cd phpstan && \
composer install --working-dir=compiler && \
php compiler/bin/compile $TRAVIS_COMMIT && \
git clone https://${GITHUB_TOKEN}@github.com/phpstan/phpstan.git phpstan-dist > /dev/null 2>&1 && \
cp compiler/tmp/phpstan.phar phpstan-dist/phpstan.phar && \
cp compiler/tmp/phpstan.phar phpstan-dist/phpstan && \
cd phpstan-dist && \
git config user.email "[email protected]" && \
git config user.name "Travis CI" && \
git add phpstan phpstan.phar && \
Expand Down
3 changes: 3 additions & 0 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@
<arg path="tests/PHPStan/Rules/Arrays/data/offset-access-without-dim-for-reading.php"/>
<arg path="src" />
<arg path="tests" />
<arg path="compiler/src" />
</exec>
</target>

Expand All @@ -148,6 +149,7 @@
<arg value="-sp"/>
<arg path="src"/>
<arg path="tests"/>
<arg path="compiler/src" />
</exec>
</then>
</if>
Expand All @@ -167,6 +169,7 @@
<arg value="-sp"/>
<arg path="src"/>
<arg path="tests"/>
<arg path="compiler/src" />
</exec>
</target>

Expand Down
12 changes: 12 additions & 0 deletions compiler/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# PHAR Compiler for PHPStan

## Compile the PHAR

```bash
composer install
php bin/compile [version] [repository]
```

Default `version` is `master`, and default `repository` is `https://github.com/phpstan/phpstan.git`.

The compiled PHAR will be in `tmp/phpstan.phar`.
21 changes: 21 additions & 0 deletions compiler/bin/compile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env php
<?php declare(strict_types = 1);

use PHPStan\Compiler\Console\CompileCommand;
use PHPStan\Compiler\Filesystem\SymfonyFilesystem;
use PHPStan\Compiler\Process\DefaultProcessFactory;
use Symfony\Component\Console\Application;

require_once __DIR__ . '/../vendor/autoload.php';

$compileCommand = new CompileCommand(
new SymfonyFilesystem(new \Symfony\Component\Filesystem\Filesystem()),
new DefaultProcessFactory(),
__DIR__ . '/../build',
__DIR__ . '/../tmp/build'
);

$application = new Application();
$application->add($compileCommand);
$application->setDefaultCommand($compileCommand->getName(), true);
$application->run();
17 changes: 17 additions & 0 deletions compiler/build/box.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"alias": "phpstan.phar",
"banner": false,
"base-path": "../tmp/build",
"check-requirements": false,
"compactors": [
"KevinGH\\Box\\Compactor\\PhpScoper"
],
"directories": [
"conf",
"src",
"stubs",
"vendor"
],
"output": "../phpstan.phar",
"php-scoper": "../../build/scoper.inc.php"
}
Binary file added compiler/build/box.phar
Binary file not shown.
4 changes: 4 additions & 0 deletions compiler/build/phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
includes:
- ../vendor/phpstan/phpstan-strict-rules/rules.neon
- ../vendor/phpstan/phpstan-phpunit/extension.neon
- ../vendor/phpstan/phpstan-phpunit/rules.neon
81 changes: 81 additions & 0 deletions compiler/build/scoper.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php declare(strict_types=1);

$stubs = [];
$stubFinder = \Isolated\Symfony\Component\Finder\Finder::create();
foreach ($stubFinder->files()->name('*.php')->in('../tmp/build/stubs') as $file) {
$stubs[] = $file->getPathName();
}

return [
'prefix' => null,
'finders' => [],
'files-whitelist' => $stubs,
'patchers' => [
function (string $filePath, string $prefix, string $content): string {
if ($filePath !== 'bin/phpstan') {
return $content;
}
return str_replace('__DIR__ . \'/..', '\'phar://phpstan.phar', $content);
},
function (string $filePath, string $prefix, string $content): string {
if ($filePath !== 'vendor/nette/di/src/DI/Compiler.php') {
return $content;
}
return str_replace('|Nette\\\\DI\\\\Statement', sprintf('|\\\\%s\\\\Nette\\\\DI\\\\Statement', $prefix), $content);
},
function (string $filePath, string $prefix, string $content): string {
if ($filePath !== 'vendor/nette/di/src/DI/Config/DefinitionSchema.php') {
return $content;
}
$content = str_replace(
sprintf('\'%s\\\\callable', $prefix),
'\'callable',
$content
);
$content = str_replace(
'|Nette\\\\DI\\\\Definitions\\\\Statement',
sprintf('|%s\\\\Nette\\\\DI\\\\Definitions\\\\Statement', $prefix),
$content
);

return $content;
},
function (string $filePath, string $prefix, string $content): string {
if ($filePath !== 'vendor/nette/di/src/DI/Extensions/ExtensionsExtension.php') {
return $content;
}
$content = str_replace(
sprintf('\'%s\\\\string', $prefix),
'\'string',
$content
);
$content = str_replace(
'|Nette\\\\DI\\\\Definitions\\\\Statement',
sprintf('|%s\\\\Nette\\\\DI\\\\Definitions\\\\Statement', $prefix),
$content
);

return $content;
},
function (string $filePath, string $prefix, string $content): string {
if ($filePath !== 'src/Testing/TestCase.php') {
return $content;
}
return str_replace(sprintf('\\%s\\PHPUnit\\Framework\\TestCase', $prefix), '\\PHPUnit\\Framework\\TestCase', $content);
},
function (string $filePath, string $prefix, string $content): string {
if ($filePath !== 'src/Testing/LevelsTestCase.php') {
return $content;
}
return str_replace(
[sprintf('\\%s\\PHPUnit\\Framework\\AssertionFailedError', $prefix), sprintf('\\%s\\PHPUnit\\Framework\\TestCase', $prefix)],
['\\PHPUnit\\Framework\\AssertionFailedError', '\\PHPUnit\\Framework\\TestCase'],
$content
);
},
],
'whitelist' => [
'PHPStan\*',
'PhpParser\*',
],
];
25 changes: 25 additions & 0 deletions compiler/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "phpstan/phpstan-compiler",
"type": "project",
"description": "PHAR Compiler for PHPStan",
"license": ["MIT"],
"require": {
"php": "^7.1",
"symfony/console": "^4.1",
"symfony/process": "^4.1",
"symfony/filesystem": "^4.1"
},
"autoload": {
"psr-4": {
"PHPStan\\Compiler\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"PHPStan\\Compiler\\": "tests/"
}
},
"require-dev": {
"phpunit/phpunit": "^7.5"
}
}
2 changes: 2 additions & 0 deletions compiler/e2e/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/vendor
/composer.lock
28 changes: 28 additions & 0 deletions compiler/e2e/PharTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php declare(strict_types = 1);

class PharTest extends \PHPStan\Testing\LevelsTestCase
{

public function dataTopics(): array
{
return [
['strictRulesExtension'],
];
}

public function getDataPath(): string
{
return __DIR__ . '/data';
}

public function getPhpStanExecutablePath(): string
{
return __DIR__ . '/vendor/phpstan/phpstan/phpstan.phar';
}

public function getPhpStanConfigPath(): ?string
{
return __DIR__ . '/phpstan.neon';
}

}
16 changes: 16 additions & 0 deletions compiler/e2e/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"require": {
"php": "^7.1"
},
"require-dev": {
"phpstan/phpstan": "dev-master",
"phpstan/phpstan-strict-rules": "dev-master",
"nikic/php-parser": "^4.0",
"phpunit/phpunit": "^7.0"
},
"autoload": {
"psr-4": {
"PHPStan\\E2E\\": "src/"
}
}
}
7 changes: 7 additions & 0 deletions compiler/e2e/data/strictRulesExtension-0.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"message": "Dynamic call to static method PHPStan\\E2E\\DynamicCallToStaticMethod::bar().",
"line": 10,
"ignorable": true
}
]
17 changes: 17 additions & 0 deletions compiler/e2e/data/strictRulesExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php declare(strict_types = 1);

namespace PHPStan\E2E;

class DynamicCallToStaticMethod
{

public function foo(): void
{
$this->bar();
}

public static function bar(): void
{
}

}
2 changes: 2 additions & 0 deletions compiler/e2e/phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
includes:
- vendor/phpstan/phpstan-strict-rules/rules.neon
17 changes: 17 additions & 0 deletions compiler/e2e/src/PharAutoloaderWorks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php declare(strict_types = 1);

namespace PHPStan\E2E;

use PhpParser\Node\Scalar\String_;

class PharAutoloaderWorks
{

public function __construct(String_ $string)
{
unset($string);
}

}

new PharAutoloaderWorks(new String_(''));
5 changes: 5 additions & 0 deletions compiler/e2e/testPharAutoloader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

require_once __DIR__ . '/vendor/autoload.php';

require_once __DIR__ . '/src/PharAutoloaderWorks.php';
21 changes: 21 additions & 0 deletions compiler/run-e2e-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env bash
set -ev

# compile phar
if [ ! -f tmp/phpstan.phar ]; then
php bin/compile $1
fi

# setup
cd e2e
rm -rf vendor
rm -f composer.lock
composer install --no-interaction
cp -f ../tmp/phpstan.phar vendor/phpstan/phpstan/phpstan.phar
cp -f ../tmp/phpstan.phar vendor/phpstan/phpstan/phpstan

# test that the phar autoloader works
php testPharAutoloader.php

# test levels
vendor/bin/phpunit PharTest.php
Loading

0 comments on commit 3398198

Please sign in to comment.