Permalink
Browse files

Allow to whitelist specific classes (#88)

  • Loading branch information...
theofidry committed Sep 4, 2017
1 parent dcddf73 commit d2fb4d4ecdbc2b5c2db8e7fceffbe792a0a5cd67
Showing with 1,722 additions and 1,269 deletions.
  1. +3 βˆ’2 .travis.yml
  2. +21 βˆ’2 Makefile
  3. +1 βˆ’1 appveyor.yml
  4. +2 βˆ’1 composer.json
  5. +43 βˆ’43 composer.lock
  6. +35 βˆ’0 fixtures/set011/bin/greet.php
  7. +15 βˆ’0 fixtures/set011/box.json.dist
  8. +15 βˆ’0 fixtures/set011/composer.json
  9. +16 βˆ’0 fixtures/set011/composer.json.dist
  10. +4 βˆ’0 fixtures/set011/expected-output
  11. +27 βˆ’0 fixtures/set011/scoper.inc.php
  12. +13 βˆ’0 fixtures/set011/src/Dictionary.php
  13. +65 βˆ’0 fixtures/set011/src/DirectionaryLocator.php
  14. +30 βˆ’0 fixtures/set011/src/Greeter.php
  15. +21 βˆ’0 fixtures/set011/tests/SalutationDictionary.php
  16. +3 βˆ’3 phpunit.xml.dist
  17. +2 βˆ’1 src/Console/Command/AddPrefixCommand.php
  18. +70 βˆ’19 src/Console/Configuration.php
  19. +11 βˆ’5 src/Handler/HandleAddPrefix.php
  20. +41 βˆ’0 src/NodeVisitor/CollectNamespaceStmtNodeVisitor.php
  21. +19 βˆ’8 .../{FunctionCallScoperNodeVisitor.php β†’ FunctionStmt/ScopeFunctionCallArgumentsStmtNodeVisitor.php}
  22. +92 βˆ’0 src/NodeVisitor/FunctionStmt/ScopeFunctionCallStmtNodeVisitor.php
  23. +101 βˆ’0 src/NodeVisitor/FunctionStmt/ScopeStaticCallStmtNodeVisitor.php
  24. +81 βˆ’9 src/NodeVisitor/IgnoreNamespaceScoperNodeVisitor.php
  25. +65 βˆ’0 src/NodeVisitor/NamespaceStmtCollection.php
  26. +86 βˆ’0 src/NodeVisitor/NewStmt/ScopeNewStmtNodeVisitor.php
  27. +86 βˆ’0 src/NodeVisitor/NewStmt/ScopeSingleLevelNewStmtNodeVisitor.php
  28. +8 βˆ’1 src/NodeVisitor/ParentNodeVisitor.php
  29. +93 βˆ’0 src/NodeVisitor/ScopeConstStmtNodeVisitor.php
  30. +9 βˆ’4 src/NodeVisitor/{FullyQualifiedNodeVisitor.php β†’ ScopeFullyQualifiedNodeVisitor.php}
  31. +15 βˆ’2 src/NodeVisitor/{NamespaceScoperNodeVisitor.php β†’ ScopeNamespaceStmtNodeVisitor.php}
  32. +15 βˆ’2 ...obalWhitelistedNamesNodeVisitor.php β†’ ScopeWhitelistedElementsFromGlobalNamespaceNodeVisitor.php}
  33. +42 βˆ’0 src/NodeVisitor/UseStmt/CollectUseStmtNodeVisitor.php
  34. +15 βˆ’15 src/NodeVisitor/{UseNamespaceScoperNodeVisitor.php β†’ UseStmt/IgnoreUseStmtNodeVisitor.php}
  35. +16 βˆ’3 src/NodeVisitor/{GroupUseNamespaceScoperNodeVisitor.php β†’ UseStmt/ScopeGroupUseStmtNodeVisitor.php}
  36. +31 βˆ’12 src/NodeVisitor/{SingleLevelUseAliasVisitor.php β†’ UseStmt/ScopeSingleLevelUseAliasVisitor.php}
  37. +166 βˆ’0 src/NodeVisitor/UseStmt/ScopeUseStmtNodeVisitor.php
  38. +75 βˆ’0 src/NodeVisitor/UseStmtCollection.php
  39. +41 βˆ’0 src/NodeVisitor/WhitelistedStatements.php
  40. +2 βˆ’1 src/Scoper.php
  41. +2 βˆ’2 src/Scoper/Composer/InstalledPackagesScoper.php
  42. +2 βˆ’2 src/Scoper/Composer/JsonFileScoper.php
  43. +1 βˆ’1 src/Scoper/NullScoper.php
  44. +2 βˆ’2 src/Scoper/PatchScoper.php
  45. +5 βˆ’31 src/Scoper/PhpScoper.php
  46. +74 βˆ’0 src/Scoper/TraverserFactory.php
  47. +11 βˆ’0 src/functions.php
  48. +6 βˆ’37 tests/Console/Command/AddPrefixCommandIntegrationTest.php
  49. +56 βˆ’0 tests/Console/Command/AddPrefixCommandTest.php
  50. +23 βˆ’11 tests/Handler/HandleAddPrefixTest.php
  51. +5 βˆ’3 tests/Scoper/Composer/InstalledPackagesScoperTest.php
  52. +5 βˆ’3 tests/Scoper/Composer/JsonFileScoperTest.php
  53. +3 βˆ’2 tests/Scoper/FakeScoper.php
  54. +3 βˆ’1 tests/Scoper/NullScoperTest.php
  55. +4 βˆ’2 tests/Scoper/PatchScoperTest.php
  56. +24 βˆ’1,038 tests/Scoper/PhpScoperTest.php
View
@@ -12,7 +12,7 @@ cache:
matrix:
include:
- php: '7.0'
- php: '7.1'
env: COVERAGE='true'
- php: '7.1'
- php: nightly
@@ -30,7 +30,8 @@ script:
- |
if [ "$COVERAGE" == "true" ]; then
make tc
make e2e
#TODO: skipped for now
#make e2e
else
make tu
fi
View
@@ -15,7 +15,7 @@ help:
##---------------------------------------------------------------------------
build: ## Build the PHAR
build: vendor vendor-bin/box/vendor
build: src vendor vendor-bin/box/vendor
# Cleanup existing artefacts
rm -f bin/php-scoper.phar
rm -rf build
@@ -46,7 +46,8 @@ tc: vendor
phpdbg -qrr -d zend.enable_gc=0 $(PHPUNIT) --coverage-html=dist/coverage --coverage-text
e2e: ## Run end-to-end tests
e2e: bin/scoper.phar fixtures/set005/vendor
e2e: bin/scoper.phar fixtures/set005/vendor fixtures/set011/vendor
# Set004
php -d zend.enable_gc=0 $(PHPSCOPER) add-prefix fixtures/set004 -o build/set004 -f
composer -d=build/set004 dump-autoload
php -d zend.enable_gc=0 -d phar.readonly=0 $(BOX) build -c build/set004/box.json.dist
@@ -55,13 +56,28 @@ e2e: bin/scoper.phar fixtures/set005/vendor
diff fixtures/set004/expected-output build/output
# Set005
php -d zend.enable_gc=0 $(PHPSCOPER) add-prefix fixtures/set005 -o build/set005 -f
composer -d=build/set005 dump-autoload
php -d zend.enable_gc=0 -d phar.readonly=0 $(BOX) build -c build/set005/box.json.dist
php build/set005/bin/greet.phar > build/output
diff fixtures/set005/expected-output build/output
# Set0011
# Skipped for now
# # TMP: replace bin/php-scoper by $(PHPSCOPER)
# php -d zend.enable_gc=0 bin/php-scoper add-prefix -o build/set011 -f -c fixtures/set011/scoper.inc.php -p PhpScoper598627d709dd4
# # Leave the `tests` autoloading of the `composer.json` intact
# cp -f build/set011/composer.json.dist build/set011/composer.json
# composer -d=build/set011 dump-autoload
# php -d zend.enable_gc=0 -d phar.readonly=0 $(BOX) build -c build/set011/box.json.dist
# cp -R fixtures/set011/tests build/set011
#
# php build/set011/bin/greet.phar > build/output
# diff fixtures/set011/expected-output build/output
tb: ## Run Blackfire profiling
tb: vendor
rm -rf build
@@ -84,6 +100,9 @@ vendor-bin/box/vendor: vendor-bin/box/composer.lock
fixtures/set005/vendor: fixtures/set005/composer.lock
composer -d=fixtures/set005 install
fixtures/set011/vendor: fixtures/set011/composer.lock
composer -d=fixtures/set011 install
composer.lock: composer.json
@echo compose.lock is not up to date.
View
@@ -10,7 +10,7 @@ branches:
environment:
matrix:
- dependencies: highest
php_ver_target: 7.0
php_ver_target: 7.1
- dependencies: highest
php_ver_target: 7.1
View
@@ -18,7 +18,8 @@
],
"require": {
"php": "^7.0",
"php": "^7.1",
"myclabs/deep-copy": "^1.6",
"nikic/php-parser": "^3.0",
"ocramius/package-versions": "^1.1",
"padraic/phar-updater": "^1.0",
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
require_once __DIR__.'/../vendor/autoload.php';
use Set011\DirectionaryLocator;
use Set011\Greeter;
use Set011\Dictionary;
$dir = Phar::running(false);
if ('' === $dir) {
// Running outside of a PHAR
$dir = __DIR__.DIRECTORY_SEPARATOR.'bin';
}
$testDir = dirname($dir).'/../tests';
$dictionaries = DirectionaryLocator::locateDictionaries($testDir);
$words = array_reduce(
$dictionaries,
function (array $words, Dictionary $dictionary): array {
$words = array_merge($words, $dictionary->provideWords());
return $words;
},
[]
);
$greeter = new Greeter($words);
foreach ($greeter->greet() as $greeting) {
echo $greeting.PHP_EOL;
}
@@ -0,0 +1,15 @@
{
"output": "build/set011/bin/greet.phar",
"main": "bin/greet.php",
"base-path": "build/set011",
"directories": [
"src",
"vendor"
],
"compression": "GZ",
"compactors": [
"Herrera\\Box\\Compactor\\Json",
"Herrera\\Box\\Compactor\\Php"
],
"stub": true
}
@@ -0,0 +1,15 @@
{
"bin": [
"bin/greet"
],
"autoload": {
"psr-4": {
"Set011\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Set011\\": "tests/"
}
}
}
@@ -0,0 +1,16 @@
{
"bin": [
"bin/greet"
],
"autoload": {
"psr-4": {
"PhpScoper598627d709dd4\\Set011\\": "src/",
"Set011\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Set011\\": "tests/"
}
}
}
@@ -0,0 +1,4 @@
Hello world!
Hi world!
Salut world!
Bonjour world!
@@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
/*
* This file is part of the humbug/php-scoper package.
*
* Copyright (c) 2017 ThΓ©o FIDRY <theo.fidry@gmail.com>,
* PΓ‘draic Brady <padraic.brady@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use Symfony\Component\Finder\Finder;
return [
'finders' => [
(new Finder())
->files()
->in(__DIR__)
->exclude('tests')
],
'whitelist' => [
\Set011\Dictionary::class,
],
];
@@ -0,0 +1,13 @@
<?php
declare(strict_types=1);
namespace Set011;
abstract class Dictionary
{
/**
* @return string[]
*/
abstract public function provideWords(): array;
}
@@ -0,0 +1,65 @@
<?php
declare(strict_types=1);
namespace Set011;
use ReflectionClass;
final class DirectionaryLocator
{
/**
* @param string $dir
*
* @return Dictionary[]
*/
public static function locateDictionaries(string $dir): array
{
$dictionaryFiles = array_values(
array_filter(
array_map(
function (string $filePath) use ($dir) {
return realpath($dir.DIRECTORY_SEPARATOR.$filePath);
},
array_filter(
scandir($dir),
function (string $file): bool {
return 1 === preg_match('/.*Dictionary\.php$/', $file);
}
)
),
function ($filePath): bool {
return false !== $filePath;
}
)
);
$classes = get_declared_classes();
foreach ($dictionaryFiles as $dictionaryFile) {
include $dictionaryFile;
}
$newClasses = array_diff(get_declared_classes(), $classes);
return array_reduce(
$newClasses,
function (array $dictionaries, string $className): array {
$class = new ReflectionClass($className);
if (false === $class->isAbstract() && $class->isSubclassOf(Dictionary::class)) {
$dictionaries[] = $class->newInstanceWithoutConstructor();
}
return $dictionaries;
},
[]
);
}
private function __construct()
{
}
}
Oops, something went wrong.

0 comments on commit d2fb4d4

Please sign in to comment.