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

Make plugin errors more verbose (add console output) #296

Merged
merged 2 commits into from
Jan 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# About plugin

The plugin helps Psalm to understand Laravel’s code (which uses a lot of magic) better.
There are 2 main ways how it does it:
- **easy**: by providing stub files (you can find them in `/stubs` dir)
- **medium+**: using custom Handlers (see `/src/Handlers` dir)


## How it works

A single callstack looks like:

```
Plugin::__invoke
Providers\ApplicationProvider::bootApp
{instantiate Laravel Application}
Plugin::generateStubFiles
Providers\FacadeStubProvider::generateStubFile
{call `ide-helper:generate` command} // generates "facades.stubphp"
Providers\ModelStubProvider::generateStubFile
Fakes\FakeModelsCommand::run(schema_aggregator(migrations))
- override parent ModelsCommand::getPropertiesFromTable (extract info from migration files instead using DB connection)
- {call `ide-helper:models --nowrite --reset`} // generates "models.stubphp"
Plugin::registerHandlers
- Container
- Eloquent
- Helpers (that not covered by stubs)
Plugin::registerStubs
- common
- for speficic laravel version
- facades.stubphp (generated)
- models.stubphp (generated)
```

## Materials

- [Authoring Psalm Plugins](https://psalm.dev/docs/running_psalm/plugins/authoring_plugins/)
15 changes: 14 additions & 1 deletion src/Fakes/FakeModelsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use function get_class;
use function in_array;
use function implode;
use function method_exists;
use function sprintf;

/** @psalm-suppress PropertyNotSetInConstructor */
class FakeModelsCommand extends ModelsCommand
Expand All @@ -35,12 +37,23 @@ public function getModels(): array
}

/**
* Load the properties from the database table.
* Load Model's properties.
* Overrides {@see \Barryvdh\LaravelIdeHelper\Console\ModelsCommand::getPropertiesFromTable}
* in order to avoid using DB connection and use SchemaAggregator instead.
*
* @param Model $model
*/
public function getPropertiesFromTable($model): void
{
$is_parent_method_still_exist = method_exists(ModelsCommand::class, __METHOD__);
if (! $is_parent_method_still_exist) {
throw new \BadMethodCallException(sprintf(
'Method %s::%s() does not exist anymore. Please rename overridden method accordingly.',
ModelsCommand::class,
__METHOD__
));
}

$table_name = $model->getTable();

if (!isset($this->schema->tables[$table_name])) {
Expand Down
12 changes: 4 additions & 8 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,24 @@
use Psalm\LaravelPlugin\Handlers\Eloquent\RelationsMethodHandler;
use Psalm\LaravelPlugin\Handlers\Helpers\CacheHandler;
use Psalm\LaravelPlugin\Handlers\Helpers\PathHandler;
use Psalm\LaravelPlugin\Handlers\Helpers\RedirectHandler;
use Psalm\LaravelPlugin\Handlers\Helpers\TransHandler;
use Psalm\LaravelPlugin\Handlers\Helpers\UrlHandler;
use Psalm\LaravelPlugin\Handlers\Helpers\ViewHandler;
use Psalm\LaravelPlugin\Handlers\SuppressHandler;
use Psalm\LaravelPlugin\Providers\ApplicationProvider;
use Psalm\LaravelPlugin\Providers\FacadeStubProvider;
use Psalm\LaravelPlugin\Providers\ModelStubProvider;
use Psalm\Plugin\PluginEntryPointInterface;
use Psalm\Plugin\RegistrationInterface;
use SimpleXMLElement;
use Throwable;

use function array_merge;
use function dirname;
use function fwrite;
use function explode;
use function glob;

/**
* @psalm-suppress UnusedClass
* @internal
*/
class Plugin implements PluginEntryPointInterface
{
Expand All @@ -39,7 +37,8 @@ public function __invoke(RegistrationInterface $registration, ?SimpleXMLElement
try {
ApplicationProvider::bootApp();
$this->generateStubFiles();
} catch (Throwable $t) {
} catch (\Throwable $t) {
fwrite(\STDERR, "Laravel plugin error: “{$t->getMessage()}”\n");
return;
}

Expand Down Expand Up @@ -77,9 +76,6 @@ private function registerStubs(RegistrationInterface $registration): void
$registration->addStubFile(ModelStubProvider::getStubFileLocation());
}

/**
* @param RegistrationInterface $registration
*/
private function registerHandlers(RegistrationInterface $registration): void
{
require_once 'Handlers/Application/ContainerHandler.php';
Expand Down
5 changes: 2 additions & 3 deletions src/Providers/ModelStubProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,14 @@ public static function generateStubFile(): void
throw new \RuntimeException('Unsupported Application type.');
}

$migrations_folder = $app->databasePath('migrations/');
$migrations_directory = $app->databasePath('migrations/');

$project_analyzer = ProjectAnalyzer::getInstance();
$codebase = $project_analyzer->getCodebase();

$schema_aggregator = new SchemaAggregator();

foreach (glob($migrations_folder . '*.php') as $file) {
//echo $file . "\n";
foreach (glob($migrations_directory . '*.php') as $file) {
$schema_aggregator->addStatements($codebase->getStatementsForFile($file));
}

Expand Down