Skip to content

Commit f595614

Browse files
authored
Use an application factory (#260)
1 parent d458756 commit f595614

File tree

3 files changed

+139
-87
lines changed

3 files changed

+139
-87
lines changed

src/Console/ApplicationFactory.php

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the humbug/php-scoper package.
7+
*
8+
* Copyright (c) 2017 Théo FIDRY <[email protected]>,
9+
* Pádraic Brady <[email protected]>
10+
*
11+
* For the full copyright and license information, please view the LICENSE
12+
* file that was distributed with this source code.
13+
*/
14+
15+
namespace Humbug\PhpScoper\Console;
16+
17+
use Humbug\PhpScoper\Console\Command\AddPrefixCommand;
18+
use Humbug\PhpScoper\Console\Command\InitCommand;
19+
use Humbug\PhpScoper\PhpParser\TraverserFactory;
20+
use Humbug\PhpScoper\Reflector;
21+
use Humbug\PhpScoper\Scoper;
22+
use Humbug\PhpScoper\Scoper\Composer\InstalledPackagesScoper;
23+
use Humbug\PhpScoper\Scoper\Composer\JsonFileScoper;
24+
use Humbug\PhpScoper\Scoper\NullScoper;
25+
use Humbug\PhpScoper\Scoper\PatchScoper;
26+
use Humbug\PhpScoper\Scoper\PhpScoper;
27+
use PackageVersions\Versions;
28+
use PhpParser\Parser;
29+
use PhpParser\ParserFactory;
30+
use Roave\BetterReflection\Reflector\ClassReflector;
31+
use Roave\BetterReflection\Reflector\FunctionReflector;
32+
use Roave\BetterReflection\SourceLocator\Ast\Locator;
33+
use Roave\BetterReflection\SourceLocator\Type\MemoizingSourceLocator;
34+
use Roave\BetterReflection\SourceLocator\Type\PhpInternalSourceLocator;
35+
use Symfony\Component\Filesystem\Filesystem;
36+
37+
class ApplicationFactory
38+
{
39+
public function create(): Application
40+
{
41+
$app = new Application('PHP Scoper', static::getVersion());
42+
43+
$app->addCommands([
44+
new AddPrefixCommand(
45+
new Filesystem(),
46+
static::createScoper()
47+
),
48+
new InitCommand(),
49+
]);
50+
51+
return $app;
52+
}
53+
54+
protected static function getVersion(): string
55+
{
56+
if (0 === strpos(__FILE__, 'phar:')) {
57+
return '@git_version_placeholder@';
58+
}
59+
60+
$rawVersion = Versions::getVersion('humbug/php-scoper');
61+
62+
[$prettyVersion, $commitHash] = explode('@', $rawVersion);
63+
64+
return (1 === preg_match('/9{7}/', $prettyVersion)) ? $commitHash : $prettyVersion;
65+
}
66+
67+
protected static function createScoper(): Scoper
68+
{
69+
return new PatchScoper(
70+
new PhpScoper(
71+
static::createParser(),
72+
new JsonFileScoper(
73+
new InstalledPackagesScoper(
74+
new NullScoper()
75+
)
76+
),
77+
new TraverserFactory(static::createReflector())
78+
)
79+
);
80+
}
81+
82+
protected static function createParser(): Parser
83+
{
84+
return (new ParserFactory())->create(ParserFactory::ONLY_PHP7);
85+
}
86+
87+
protected static function createReflector(): Reflector
88+
{
89+
$phpParser = static::createParser();
90+
$astLocator = new Locator($phpParser);
91+
92+
$sourceLocator = new MemoizingSourceLocator(
93+
new PhpInternalSourceLocator($astLocator)
94+
);
95+
$classReflector = new ClassReflector($sourceLocator);
96+
97+
return new Reflector(
98+
$classReflector,
99+
new FunctionReflector($sourceLocator, $classReflector)
100+
);
101+
}
102+
}

src/functions.php

Lines changed: 22 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -15,108 +15,43 @@
1515
namespace Humbug\PhpScoper;
1616

1717
use Humbug\PhpScoper\Console\Application;
18-
use Humbug\PhpScoper\Console\Command\AddPrefixCommand;
19-
use Humbug\PhpScoper\Console\Command\InitCommand;
20-
use Humbug\PhpScoper\PhpParser\TraverserFactory;
21-
use Humbug\PhpScoper\Scoper\Composer\InstalledPackagesScoper;
22-
use Humbug\PhpScoper\Scoper\Composer\JsonFileScoper;
23-
use Humbug\PhpScoper\Scoper\NullScoper;
24-
use Humbug\PhpScoper\Scoper\PatchScoper;
25-
use Humbug\PhpScoper\Scoper\PhpScoper;
18+
use Humbug\PhpScoper\Console\ApplicationFactory;
2619
use Iterator;
27-
use PackageVersions\Versions;
2820
use PhpParser\Node;
2921
use PhpParser\Node\Identifier;
3022
use PhpParser\Node\Name;
3123
use PhpParser\Parser;
32-
use PhpParser\ParserFactory;
33-
use Roave\BetterReflection\Reflector\ClassReflector;
34-
use Roave\BetterReflection\Reflector\FunctionReflector;
35-
use Roave\BetterReflection\SourceLocator\Ast\Locator;
36-
use Roave\BetterReflection\SourceLocator\Type\MemoizingSourceLocator;
37-
use Roave\BetterReflection\SourceLocator\Type\PhpInternalSourceLocator;
38-
use Symfony\Component\Console\Application as SymfonyApplication;
39-
use Symfony\Component\Filesystem\Filesystem;
24+
use function array_map;
25+
use function count;
26+
use function is_array;
4027
use function is_object;
28+
use function is_scalar;
4129
use function is_string;
4230
use function method_exists;
31+
use function serialize;
32+
use function strlen;
33+
use function strpos;
34+
use function substr;
35+
use function unserialize;
4336

44-
// TODO: register this file to the list of functions if possible to be autoloaded
45-
46-
/**
47-
* @private
48-
*/
49-
function create_application(): SymfonyApplication
37+
function create_application(): Application
5038
{
51-
$app = new Application('PHP Scoper', get_version());
52-
53-
$app->addCommands([
54-
new AddPrefixCommand(
55-
new Filesystem(),
56-
create_scoper()
57-
),
58-
new InitCommand(),
59-
]);
60-
61-
return $app;
62-
}
63-
64-
/**
65-
* @private
66-
*/
67-
function get_version(): string
68-
{
69-
if ('phar:' === substr(__FILE__, 0, 5)) {
70-
return '@git_version_placeholder@';
71-
}
72-
73-
$rawVersion = Versions::getVersion('humbug/php-scoper');
74-
75-
[$prettyVersion, $commitHash] = explode('@', $rawVersion);
76-
77-
return (1 === preg_match('/9{7}/', $prettyVersion)) ? $commitHash : $prettyVersion;
39+
return (new ApplicationFactory())->create();
7840
}
7941

8042
/**
8143
* @private
44+
*
45+
* @deprecated Will be removed in future releases.
8246
*/
8347
function create_scoper(): Scoper
8448
{
85-
return new PatchScoper(
86-
new PhpScoper(
87-
create_parser(),
88-
new JsonFileScoper(
89-
new InstalledPackagesScoper(
90-
new NullScoper()
91-
)
92-
),
93-
new TraverserFactory(create_reflector())
94-
)
95-
);
96-
}
97-
98-
/**
99-
* @private
100-
*/
101-
function create_parser(): Parser
102-
{
103-
return (new ParserFactory())->create(ParserFactory::ONLY_PHP7);
104-
}
105-
106-
function create_reflector(): Reflector
107-
{
108-
$phpParser = create_parser();
109-
$astLocator = new Locator($phpParser);
110-
111-
$sourceLocator = new MemoizingSourceLocator(
112-
new PhpInternalSourceLocator($astLocator)
113-
);
114-
$classReflector = new ClassReflector($sourceLocator);
115-
116-
return new Reflector(
117-
$classReflector,
118-
new FunctionReflector($sourceLocator, $classReflector)
119-
);
49+
return (new class() extends ApplicationFactory {
50+
public static function createScoper(): Scoper
51+
{
52+
return parent::createScoper();
53+
}
54+
})::createScoper();
12055
}
12156

12257
/**
@@ -140,7 +75,7 @@ function get_common_path(array $paths): string
14075
foreach ($paths as $path) {
14176
if (substr($path, $lastOffset, $dirLen) !== $dir) {
14277
if (0 < strlen($common) && DIRECTORY_SEPARATOR === $common[strlen($common) - 1]) {
143-
$common = substr($common, 0, strlen($common) - 1);
78+
$common = substr($common, 0, -1);
14479
}
14580

14681
return $common;
@@ -154,7 +89,7 @@ function get_common_path(array $paths): string
15489
$common = substr($common, 0, -1);
15590

15691
if (0 < strlen($common) && DIRECTORY_SEPARATOR === $common[strlen($common) - 1]) {
157-
$common = substr($common, 0, strlen($common) - 1);
92+
$common = substr($common, 0, -1);
15893
}
15994

16095
return $common;

tests/functions.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
namespace Humbug\PhpScoper;
1616

1717
use Closure;
18+
use Humbug\PhpScoper\Console\ApplicationFactory;
1819
use LogicException;
20+
use PhpParser\Parser;
1921
use Symfony\Component\Filesystem\Filesystem;
2022

2123
/**
@@ -72,3 +74,16 @@ function create_fake_patcher(): Closure
7274
throw new LogicException('Did not expect to be called');
7375
};
7476
}
77+
78+
/**
79+
* @private
80+
*/
81+
function create_parser(): Parser
82+
{
83+
return (new class() extends ApplicationFactory {
84+
public static function createParser(): Parser
85+
{
86+
return parent::createParser();
87+
}
88+
})::createParser();
89+
}

0 commit comments

Comments
 (0)