Skip to content

Commit

Permalink
Qualify global functions to avoid collisions
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonmccreary committed Sep 27, 2023
1 parent abd9f09 commit b8b9f14
Show file tree
Hide file tree
Showing 24 changed files with 192 additions and 168 deletions.
1 change: 1 addition & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
mv composer-dev.json composer.json
rm -rf vendor composer.lock
composer update --no-dev
rm vendor/shift-tasks.php
sed -i '' -e "s/Shift CLI', '.*'/Shift CLI', '$1'/" shift-cli
box compile --no-parallel
git add builds/shift-cli shift-cli
Expand Down
5 changes: 5 additions & 0 deletions pint.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
"rules": {
"concat_space": {
"spacing": "one"
},
"native_function_invocation": {
"include": [
"@all"
]
}
},
"exclude": [
Expand Down
26 changes: 22 additions & 4 deletions scoper.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,30 @@
declare(strict_types=1);

return [
'expose-global-functions' => false,
// 'exclude-functions' => ['app'],
'patchers' => [
function (string $filePath, string $prefix, string $content): string {
if (str_starts_with($filePath, 'src/Tasks/')) {
$content = str_replace(
if (\str_contains($content, $prefix . '\\app')) {
$content = \preg_replace(
'/' . $prefix . '[\\\\]+app' . '/',
'app',
$content
);
}

if (\str_starts_with($filePath, 'src/Tasks/')) {
$content = \str_replace(
$prefix . '\\\\Illuminate\\\\',
'Illuminate\\\\',
$content
);
$content = str_replace(
$content = \str_replace(
$prefix . '\\\\App\\\\',
'App\\\\',
$content
);
$content = str_replace(
$content = \str_replace(
$prefix . '\\\\PHPUnit\\\\Framework\\\\',
'PHPUnit\\\\Framework\\\\',
$content
Expand All @@ -28,6 +38,14 @@ function (string $filePath, string $prefix, string $content): string {
],
'exclude-files' => [
'vendor/symfony/polyfill-php80/bootstrap.php',
'vendor/symfony/polyfill-ctype/bootstrap80.php',
'vendor/symfony/polyfill-ctype/bootstrap.php',
'vendor/symfony/polyfill-intl-normalizer/bootstrap80.php',
'vendor/symfony/polyfill-intl-normalizer/bootstrap.php',
'vendor/symfony/polyfill-mbstring/bootstrap80.php',
'vendor/symfony/polyfill-mbstring/bootstrap.php',
'vendor/symfony/polyfill-intl-grapheme/bootstrap80.php',
'vendor/symfony/polyfill-intl-grapheme/bootstrap.php',
],
'exclude-namespaces' => [
'Symfony\Polyfill\*',
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/DiscoverCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$this->taskManifest->build();

collect($this->taskManifest->list())
\collect($this->taskManifest->list())
->keys()
->each(fn ($task) => $output->writeln($task))
->whenNotEmpty(fn () => $output->writeln(''));
Expand Down
4 changes: 2 additions & 2 deletions src/Commands/PublishCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ protected function configure(): void

protected function execute(InputInterface $input, OutputInterface $output): int
{
if (file_exists('shift-cli.json') && ! $input->getOption('force')) {
if (\file_exists('shift-cli.json') && ! $input->getOption('force')) {
$output->writeln('<comment>A configuration file already exists.</comment> Use the `--force` option to overwrite yours.');

return 1;
}

file_put_contents('shift-cli.json', json_encode(Configuration::defaults(), JSON_PRETTY_PRINT));
\file_put_contents('shift-cli.json', \json_encode(Configuration::defaults(), JSON_PRETTY_PRINT));

return 0;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Commands/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ private function createTask(string $name, InputInterface $input): Task
{
$task = new $name;

if (in_array(FindsFiles::class, class_uses_recursive($task))) {
if (\in_array(FindsFiles::class, \class_uses_recursive($task))) {
if (! empty($input->getOption('path'))) {
$task->setPaths($input->getOption('path'));
}
Expand Down Expand Up @@ -113,7 +113,7 @@ private function listTasks(OutputInterface $output)
{
$output->writeln('<comment>Available tasks:</comment>');

$tasks = collect($this->taskManifest->list())
$tasks = \collect($this->taskManifest->list())
->map(fn ($fqcn) => [' <info>' . $fqcn::$name . '</info> ', $fqcn::$description])
->all();

Expand Down
18 changes: 9 additions & 9 deletions src/Support/TaskManifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function list(): array
return $this->tasks;
}

if (! is_file($this->manifestPath)) {
if (! \is_file($this->manifestPath)) {
$this->build();
}

Expand All @@ -48,18 +48,18 @@ public function build(): void
$packages = [];
$path = $this->vendorPath . DIRECTORY_SEPARATOR . 'composer' . DIRECTORY_SEPARATOR . 'installed.json';

if (file_exists($path)) {
$installed = json_decode(file_get_contents($path), true);
if (\file_exists($path)) {
$installed = \json_decode(\file_get_contents($path), true);

$packages = $installed['packages'] ?? $installed;
}

$this->write(collect($packages)
$this->write(\collect($packages)
->mapWithKeys(function ($package) {
return collect($package['extra']['shift']['tasks'] ?? [])->mapWithKeys(fn ($task) => [$task::$name => $task]);
return \collect($package['extra']['shift']['tasks'] ?? [])->mapWithKeys(fn ($task) => [$task::$name => $task]);
})
->filter()
->merge(collect($this->defaultTasks)->mapWithKeys(fn ($task) => [$task::$name => $task]))
->merge(\collect($this->defaultTasks)->mapWithKeys(fn ($task) => [$task::$name => $task]))
->all());
}

Expand All @@ -75,7 +75,7 @@ private function isStale(array $manifest): bool

protected function write(array $tasks): void
{
if (! is_writable($dirname = dirname($this->manifestPath))) {
if (! \is_writable($dirname = \dirname($this->manifestPath))) {
throw new \Exception("The {$dirname} directory must be present and writable.");
}

Expand All @@ -84,8 +84,8 @@ protected function write(array $tasks): void
'tasks' => $tasks,
];

file_put_contents(
$this->manifestPath, '<?php return ' . var_export($manifest, true) . ';'
\file_put_contents(
$this->manifestPath, '<?php return ' . \var_export($manifest, true) . ';'
);
}
}
30 changes: 15 additions & 15 deletions src/Tasks/AnonymousMigrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ public function perform(): int
private function updateMigrations(): void
{
foreach ($this->findFilesContaining('/\bclass\s+\S+\s+extends\s+Migration\s/') as $path) {
if (str_starts_with($path, 'stubs/')) {
if (\str_starts_with($path, 'stubs/')) {
continue;
}

$contents = $this->convertClassDefinition(file_get_contents($path));
if (is_null($contents)) {
$contents = $this->convertClassDefinition(\file_get_contents($path));
if (\is_null($contents)) {
continue;
}

file_put_contents($path, $contents);
\file_put_contents($path, $contents);
}
}

Expand All @@ -47,43 +47,43 @@ private function updateStubs(): void
];

foreach ($stubs as $stub) {
if (! file_exists('stubs/' . $stub)) {
if (! \file_exists('stubs/' . $stub)) {
continue;
}

$contents = file_get_contents('stubs/' . $stub);
$contents = str_replace(
$contents = \file_get_contents('stubs/' . $stub);
$contents = \str_replace(
['DummyClass', '{{ class }}', '{{class}}'],
'ShiftTemporaryClassNamePlaceholder',
$contents
);
$contents = str_replace(
$contents = \str_replace(
['DummyTable', '{{ table }}', '{{table}}'],
'ShiftTemporaryTableNamePlaceholder',
$contents
);

$contents = $this->convertClassDefinition($contents);
if (is_null($contents)) {
if (\is_null($contents)) {
continue;
}

$contents = str_replace('ShiftTemporaryClassNamePlaceholder', '{{ class }}', $contents);
$contents = str_replace('ShiftTemporaryTableNamePlaceholder', '{{ table }}', $contents);
$contents = \str_replace('ShiftTemporaryClassNamePlaceholder', '{{ class }}', $contents);
$contents = \str_replace('ShiftTemporaryTableNamePlaceholder', '{{ table }}', $contents);

file_put_contents('stubs/' . $stub, $contents);
\file_put_contents('stubs/' . $stub, $contents);
}
}

private function convertClassDefinition($contents): ?string
{
$found = preg_match('/^class\s+(\S+)\s+extends\s+Migration(\s+)/m', $contents, $matches);
$found = \preg_match('/^class\s+(\S+)\s+extends\s+Migration(\s+)/m', $contents, $matches);
if (! $found) {
return null;
}

$contents = str_replace(rtrim($matches[0]), 'return new class extends Migration', $contents);
$contents = preg_replace('/\b' . preg_quote($matches[1], '/') . '::/', 'self::', $contents);
$contents = \str_replace(\rtrim($matches[0]), 'return new class extends Migration', $contents);
$contents = \preg_replace('/\b' . \preg_quote($matches[1], '/') . '::/', 'self::', $contents);

return Str::replaceLast('}', '};', $contents);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Tasks/CheckLint.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function perform(): int
foreach ($files as $file) {
$output = [];
$exit_code = 0;
exec('php -l ' . $file . ' 2>&1', $output, $exit_code);
\exec('php -l ' . $file . ' 2>&1', $output, $exit_code);

if ($exit_code !== 0) {
[$line, $error] = $this->parseError($output);
Expand All @@ -39,7 +39,7 @@ public function perform(): int

private function parseError(array $lines): array
{
preg_match('/PHP (?:Fatal|Parse) error:\s+(?:syntax error, )?(.+?)\s+in\s+.+?\.php\s+on\s+line\s+(\d+)/', $lines[0], $matches);
\preg_match('/PHP (?:Fatal|Parse) error:\s+(?:syntax error, )?(.+?)\s+in\s+.+?\.php\s+on\s+line\s+(\d+)/', $lines[0], $matches);

return [$matches[2], $matches[1]];
}
Expand Down
24 changes: 12 additions & 12 deletions src/Tasks/ClassStrings.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,28 @@ public function perform(): int
return 0;
}

$pattern = '/[\'"]' . $this->patternForNamespaces(array_keys($namespaces)) . '/';
$pattern = '/[\'"]' . $this->patternForNamespaces(\array_keys($namespaces)) . '/';
$files = $this->findFilesContaining($pattern);

if (empty($files)) {
return 0;
}

$pattern = '/([\'"])' . $this->patternForNamespaces(array_keys($namespaces)) . '([\w\\\\]+)\1/';
$pattern = '/([\'"])' . $this->patternForNamespaces(\array_keys($namespaces)) . '([\w\\\\]+)\1/';

foreach ($files as $file) {
$contents = preg_replace_callback(
$contents = \preg_replace_callback(
$pattern,
function ($matches) use ($namespaces) {
if (! $this->classExists($namespaces, $matches[2], preg_replace('/\\\\+/', '\\', $matches[3]))) {
if (! $this->classExists($namespaces, $matches[2], \preg_replace('/\\\\+/', '\\', $matches[3]))) {
return $matches[0];
}

return sprintf('\\%s\\%s::class', $matches[2], preg_replace('/\\\\+/', '\\', $matches[3]));
return \sprintf('\\%s\\%s::class', $matches[2], \preg_replace('/\\\\+/', '\\', $matches[3]));
},
file_get_contents($file));
\file_get_contents($file));

file_put_contents($file, $contents);
\file_put_contents($file, $contents);
}

return 0;
Expand All @@ -51,14 +51,14 @@ function ($matches) use ($namespaces) {
private function classExists(mixed $namespaces, mixed $namespace, mixed $class): bool
{
$key = $namespace . '\\';
if (! array_key_exists($key, $namespaces)) {
if (! \array_key_exists($key, $namespaces)) {
return false;
}

foreach (Arr::wrap($namespaces[$key]) as $path) {
$file = str_replace([$key, '\\'], [$path, DIRECTORY_SEPARATOR], $key . $class . '.php');
$file = \str_replace([$key, '\\'], [$path, DIRECTORY_SEPARATOR], $key . $class . '.php');

if (file_exists($file)) {
if (\file_exists($file)) {
return true;
}
}
Expand All @@ -68,12 +68,12 @@ private function classExists(mixed $namespaces, mixed $namespace, mixed $class):

private function patternForNamespaces(array $namespaces): string
{
return '(?:[\\\\]+)?(' . implode('|', array_map(fn ($namespace) => preg_quote(rtrim($namespace, '\\'), '/'), $namespaces)) . ')\\\\+';
return '(?:[\\\\]+)?(' . \implode('|', \array_map(fn ($namespace) => \preg_quote(\rtrim($namespace, '\\'), '/'), $namespaces)) . ')\\\\+';
}

private function psr4Namespaces(): array
{
$composer = json_decode(file_get_contents('composer.json'), true);
$composer = \json_decode(\file_get_contents('composer.json'), true);

return $composer['autoload']['psr-4'] ?? [];
}
Expand Down
14 changes: 7 additions & 7 deletions src/Tasks/DebugCalls.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public function perform(): int
$failure = false;

foreach ($files as $file) {
$contents = file_get_contents($file);
$contents = \file_get_contents($file);

if (! preg_match('/\b(print_r|var_dump|var_export|dd)\(/', $contents)) {
if (! \preg_match('/\b(print_r|var_dump|var_export|dd)\(/', $contents)) {
continue;
}

Expand All @@ -40,25 +40,25 @@ public function perform(): int
$failure = true;
$this->leaveComment($file, $instances);

foreach (array_reverse($instances) as $instance) {
$contents = substr_replace(
foreach (\array_reverse($instances) as $instance) {
$contents = \substr_replace(
$contents,
'',
$instance['offset']['start'],
$instance['offset']['end'] - $instance['offset']['start'] + 1
);
}

file_put_contents($file, $contents);
\file_put_contents($file, $contents);
}

return $failure ? 1 : 0;
}

private function leaveComment(string $path, array $calls): void
{
$instances = array_map(
fn ($call) => sprintf('Line %d: contains call to `%s`', $call['line']['start'], $call['function']),
$instances = \array_map(
fn ($call) => \sprintf('Line %d: contains call to `%s`', $call['line']['start'], $call['function']),
$calls
);

Expand Down
Loading

0 comments on commit b8b9f14

Please sign in to comment.