Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve autoloading #50

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 27 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,31 @@ All the following options are part of the `parameters` section.
PHPStan uses Composer autoloader so the easiest way how to autoload classes
is through the `autoload`/`autoload-dev` sections in composer.json.

#### Specify autoloading path

If you install PHPStan with composer global or if you change the `vendor-dir` of your `composer.json`, you should help it to find your composer autoloader.

**Two solutions** :

You could specify `--autoload-file|-a` option during execution :

```
vendor/bin/phpstan analyse --autoload-file=/path/to/autoload.php src tests
```

Or you could add a `vendor/autoload.php` file next to the folder where you run PHPStan.

```shell
# /home/www/myapp
pwd
echo '<?php require_once("the/path/to/my/app/autoload.php")' > 'vendor/autoload.php'

# Now this call will require the above autoload.php
/home/www/myapp/phpstan analyse
```

#### Your own autoloading

If PHPStan complains about some nonexistent classes and you're sure the classes
exist in the codebase AND you don't want to use Composer autoloader for some reason,
you can specify directories to scan and concrete files to include using
Expand Down Expand Up @@ -315,7 +340,7 @@ interface PropertyReflection
{

public function getType(): Type;

public function getDeclaringClass(): ClassReflection;

public function isStatic(): bool;
Expand Down Expand Up @@ -369,7 +394,7 @@ interface MethodReflection
public function isPrivate(): bool;

public function isPublic(): bool;

public function getName(): string;

/**
Expand Down
14 changes: 10 additions & 4 deletions bin/phpstan
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@ use PHPStan\Command\AnalyseCommand;

gc_disable(); // performance boost

$composerAutoloadFile = __DIR__ . '/../vendor/autoload.php';
if (!is_file($composerAutoloadFile)) {
$composerAutoloadFile = __DIR__ . '/../../../autoload.php';
if (is_file($autoload = getcwd() . '/vendor/autoload.php')) {
require_once($autoload);
}

require_once($composerAutoloadFile);
if (!class_exists('PHPStan\Command\AnalyseCommand', true)) {
$composerAutoloadFile = __DIR__ . '/../vendor/autoload.php';
if (!is_file($composerAutoloadFile)) {
$composerAutoloadFile = __DIR__ . '/../../../autoload.php';
}

require_once($composerAutoloadFile);
}

$application = new \Symfony\Component\Console\Application('PHPStan - PHP Static Analysis Tool');
$application->setCatchExceptions(false);
Expand Down
6 changes: 6 additions & 0 deletions src/Command/AnalyseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ protected function configure()
new InputOption('configuration', 'c', InputOption::VALUE_REQUIRED, 'Path to project configuration file'),
new InputOption(self::OPTION_LEVEL, 'l', InputOption::VALUE_REQUIRED, 'Level of rule options - the higher the stricter'),
new InputOption(ErrorsConsoleStyle::OPTION_NO_PROGRESS, null, InputOption::VALUE_NONE, 'Do not show progress bar, only results'),
new InputOption('autoload-file', 'a', InputOption::VALUE_OPTIONAL, 'Project\'s additional autoload file path'),
]);
}

Expand All @@ -39,6 +40,11 @@ public function getAliases(): array

protected function execute(InputInterface $input, OutputInterface $output): int
{
$autoloadFile = $input->getOption('autoload-file');
if ($autoloadFile !== null && is_file($autoloadFile)) {
require_once $autoloadFile;
}

$rootDir = realpath(__DIR__ . '/../..');
$tmpDir = $rootDir . '/tmp';
$confDir = $rootDir . '/conf';
Expand Down