Problem/Motivation

Regenerating the baseline is documented here but I always forget the exact commands and have to look them up.

Steps to reproduce

Proposed resolution

  • Add a phpstan-baseline script to the root composer.json so you can just run composer phpstan-baseline when needed.
  • Also add phpstan, so you can run composer phpstan

Remaining tasks

  1. Decide if there are other PHPStan commands we should add to the root composer.json.

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

Issue fork drupal-3485233

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

longwave created an issue. See original summary.

nicxvan’s picture

Is there a way to overwrite the current baseline with this starter file?

It runs much more consistently and with lower memory starting empty:

<?php declare(strict_types = 1);

$ignoreErrors = [];


return ['parameters' => ['ignoreErrors' => $ignoreErrors]];
berdir’s picture

This would definitely be useful. I've added ddev commands for now:

$ cat .ddev/commands/web/phpstan-generate
#!/usr/bin/env bash

./vendor/bin/phpstan analyze  --configuration=./core/phpstan.neon.dist --generate-baseline=core/.phpstan-baseline.php

And a phpstan without the --generate-baseline.

mstrelan’s picture

I think we should drop the word "generate" and just have:

composer phpstan
composer phpstan-baseline

I think we should also use these commands in ci for consistency. Then if we need to tweak it in future it will be updated in both places.

berdir’s picture

the CI job runs phpstan like 3 times with different output formats and arguments, so I don't think we can reuse that.

nicxvan’s picture

I figured out why I needed the empty baseline.

It was because during the conversion there were thousands of changes. A normal baseline process doesn't need that.

mondrake’s picture

Adding related issue

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

andypost’s picture

it also require to set memory limit

longwave’s picture

Status: Active » Needs review

After copy-pasting for the nth time I remembered about this issue.

You can now run phpstan on file(s)

$ composer phpstan core/modules/node/tests/src/Functional/NodeTitleTest.php
 1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%



 [OK] No errors

or generate the baseline:

$ composer phpstan-baseline
 10132/10132 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%



 [OK] Baseline generated with 6695 errors.
mstrelan’s picture

For my setup I need to also set the memory limit to -1. Should we include that here too?

longwave’s picture

OK, done.

longwave’s picture

Title: Add phpstan-generate-baseline Composer script to root composer.json » Add phpstan-baseline Composer script to root composer.json
longwave’s picture

I didn't realise phpstan had the option itself, thanks!

mstrelan’s picture

I would RTBC but can't do the T part until next week

dcam’s picture

Status: Needs review » Reviewed & tested by the community

This is kind of funny. First, because I had no idea the phpcs/phpcbf commands existed in the composer.json file. I guess I don't spend much time looking at it. Second, because I just use good ol' bash aliases. The recent mock object issues prompted me to finally add entries for PHPStan alongside the aliases for other CLI utilities when I had to regenerate baselines so often. So this is pretty fresh in my mind.

alias phpstan='ddev exec vendor/bin/phpstan analyze --configuration=core/phpstan.neon.dist --memory-limit=-1'
alias baseline='ddev exec vendor/bin/phpstan analyze --configuration=./core/phpstan.neon.dist --memory-limit=-1 --generate-baseline=core/.phpstan-baseline.php'

But putting them in Composer is good! I'm sure it will save someone time down the road. Especially if anyone ends up on a fresh dev environment for any reason (I'm thinking ahead to when this laptop needs to be replaced). They'll always be here for us.

Anyway, the new commands exactly match the examples given in our own PHPStan in Core documentation. I tested them all (including the old ones) out on my local environment. They worked just like I expected based on my experience with my old aliases. LGTM.

benjifisher’s picture

Issue summary: View changes

I am updating the issue summary to match the MR that is now RTBC.

quietone’s picture

Everything has been addressed here and I have updated credit.

alexpott’s picture

Version: main » 11.x-dev
Status: Reviewed & tested by the community » Fixed

Committed and pushed fd70826e021 to main and 567eff6ff11 to 11.x. Thanks!

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

  • alexpott committed 567eff6f on 11.x
    task: #3485233 Add phpstan-baseline Composer script to root composer....

  • alexpott committed fd70826e on main
    task: #3485233 Add phpstan-baseline Composer script to root composer....
wim leers’s picture

FYI (in case this helps some contrib module maintainer): Canvas did something similar a few months ago over at #3575399: DX+CI: Add `composer run phpstan-regenerate-ignores-for-11.3-and-higher` — to simplify updating baseline errors due to PHPStan running on 11.2, fail CI job if out of sync — although Canvas' baseline is intentionally different: Canvas started with PHPStan level 8 (so in principle: empty baseline).

Many contrib modules — including Canvas — target some core minor as the minimum supported version. For Canvas, that's 11.2.x. And of course, like any contrib module, we try to be as forward-compatible as possible. That means for example already providing a default_content event subscriber. Which in turn triggers PHPStan errors (because we execute PHPStan against the minimum supported core version — 11.2.x in our case).

So, Canvas' baseline is specifically for PHPStan errors specific to upcoming Drupal core versions. For example:

		-
			message: '#^Access to property \$metadata on an unknown class Drupal\\Core\\DefaultContent\\PreEntityImportEvent\.$#'
			identifier: class.notFound
			count: 4
			path: src/EventSubscriber/DefaultContentSubscriber.php

Canvas provides composer run phpstan-regenerate-ignores-for-11.3-and-higher to simplify keeping this up-to-date.

See #3575399: DX+CI: Add `composer run phpstan-regenerate-ignores-for-11.3-and-higher` — to simplify updating baseline errors due to PHPStan running on 11.2, fail CI job if out of sync.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.