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

Add phpstan command. #14

Merged
merged 14 commits into from
Aug 5, 2024
Merged

Add phpstan command. #14

merged 14 commits into from
Aug 5, 2024

Conversation

markdorison
Copy link
Contributor

The Issue

Adds support for phpstan command to more closely mimic GitLab CI configuration/runs.

How This PR Solves The Issue

  • Adds the phpstan command.
  • Updates README documentation to include command.

Manual Testing Instructions

  1. Run ddev phpstan
  2. PHPStan should run as expected.

Automated Testing Overview

N/A

Related Issue Link(s)

Closes #13

Release/Deployment Notes

N/A

@markdorison
Copy link
Contributor Author

I am currently getting the following error when I attempt to run the command. 🤔

RuntimeException thrown in /var/www/html/vendor/mglaman/phpstan-drupal/src/Drupal/DrupalAutoloader.php on line 68 while loading bootstrap file /var/www/html/vendor/mglaman/phpstan-drupal/drupal-autoloader.php: Unable to detect Drupal at 
Failed to run phpstan : exit status 1

@weitzman
Copy link
Collaborator

weitzman commented Nov 6, 2023

It might be because drupalfinder can't find Drupal. Drush no longer uses drupalfinder as it has not had a release in like 5 years. Composer can now find Drupal root. I'll talk to mgalalm

@weitzman
Copy link
Collaborator

You can work around this by setting up the config.extra key in composer.json as usual.

"installer-paths": {
            "web/core": [
                "type:drupal-core"
            ],
            "web/libraries/{$name}": [
                "type:drupal-library"
            ],
            "web/modules/contrib/{$name}": [
                "type:drupal-module"
            ],
            "web/profiles/contrib/{$name}": [
                "type:drupal-profile"
            ],
            "web/themes/contrib/{$name}": [
                "type:drupal-theme"
            ],
            "drush/contrib/{$name}": [
                "type:drupal-drush"
            ]
        }

@weitzman
Copy link
Collaborator

Will be fixed by webflo/drupal-finder#64 if that gets merged

commands/web/phpstan Outdated Show resolved Hide resolved
@bimsonz
Copy link

bimsonz commented Feb 16, 2024

  • 🚀 would love to see this get in

@IT-Cru
Copy link

IT-Cru commented Mar 15, 2024

Would be also nice to add an example in README how to create a PHPStan baseline file for your contrib module via this DDEV phpstan command to help module maintainers and contributors with it :)

@jameswilson
Copy link

jameswilson commented May 22, 2024

From above on this issue:

Will be fixed by webflo/drupal-finder#64 if that gets merged

That got merged 2 weeks ago, but I'm still seeing issues with the current PR as it is coded.

But there is good news.

While looking into this, I stumbled across this key information:

Not sure about the implication, but running ddev expand-composer-json allows me to run PHPStan

Originally posted by @gardon in #42 (comment), closed as a dupe of this issue

If I copy the first two lines from .ddev/commands/web/poser into .ddev/commands/web/phpstan, it works!

These lines:

export COMPOSER=composer.contrib.json
.ddev/commands/web/expand-composer-json "$DDEV_PROJECT_NAME"

The full contents of .ddev/commands/web/phpstan:

#!/bin/bash

#ddev-generated
## Command provided by https://github.com/ddev/ddev-drupal-contrib
## Description: Run phpstan inside the web container
## Usage: phpstan [flags] [args]
## Example: "ddev phpstan" or "ddev phpstan -n"
## ProjectTypes: drupal,drupal8,drupal9,drupal10
## ExecRaw: true

if ! command -v phpstan >/dev/null; then
  echo "phpstan is not available. You may need to 'ddev composer install'"
  exit 1
fi
export COMPOSER=composer.contrib.json
.ddev/commands/web/expand-composer-json "$DDEV_PROJECT_NAME"
phpstan analyse $DDEV_DOCROOT/modules/custom "$@"

I guess I don't have permission to update a PR from someone else. (I miss the "Get Push Access" we have on Drupal MRs here).

@jameswilson
Copy link

So ddev phpstan is turning up different results than Drupal's GitLabCI.

Fixed by pulling in phpstan.neon file from gitlab_templates project:

Updated working command:

#!/bin/bash

#ddev-generated
## Command provided by https://github.com/ddev/ddev-drupal-contrib
## Description: Run phpstan inside the web container
## Usage: phpstan [flags] [args]
## Example: "ddev phpstan" or "ddev phpstan -n"
## ProjectTypes: drupal,drupal8,drupal9,drupal10
## ExecRaw: true

if ! command -v phpstan >/dev/null; then
  echo "phpstan is not available. You may need to 'ddev composer install'"
  exit 1
fi
test -e phpstan.neon || curl -OL https://git.drupalcode.org/project/gitlab_templates/-/raw/default-ref/assets/phpstan.neon
export COMPOSER=composer.contrib.json
.ddev/commands/web/expand-composer-json "$DDEV_PROJECT_NAME"
phpstan analyse $DDEV_DOCROOT/modules/custom "$@"

@jameswilson jameswilson mentioned this pull request May 22, 2024
@jameswilson
Copy link

jameswilson commented May 22, 2024

I created a new PR #53 building on the work here following methodology from https://stackoverflow.com/a/39807499/413538. I fixed merge conflicts and created separate commits for the different pieces I've mentioned above in previous comments.

@mbomb007
Copy link

Will be fixed by webflo/drupal-finder#64 if that gets merged

This was merged, and released in 1.3.0

@rfay
Copy link
Member

rfay commented Jul 11, 2024

Closing then, thanks.

@rfay rfay closed this Jul 11, 2024
@markdorison
Copy link
Contributor Author

@rfay webflo/drupal-finder#64 was blocking this but doesn't replace it (AFAIK), unless you are closing it to go with #53?

@rfay
Copy link
Member

rfay commented Jul 11, 2024

OK then :)

@rfay rfay reopened this Jul 11, 2024
@rfay
Copy link
Member

rfay commented Jul 11, 2024

If you want to continue with it, please rebase and solve the conflicts.

@markdorison
Copy link
Contributor Author

Rebased main and improvements from PR #53.

@markdorison markdorison marked this pull request as ready for review July 15, 2024 16:43
@claudiu-cristea
Copy link
Contributor

With the current state of the PR I'm getting:

$ ddev phpstan
...
Note: Using configuration file /var/www/html/phpstan.neon.
File '/var/www/html/phpstan-baseline.neon' is missing or is not readable.

The problem is here https://git.drupalcode.org/project/gitlab_templates/-/blob/main/includes/include.drupalci.main.yml#L516

I've created markdorison#1 to improve the current PR

@weitzman
Copy link
Collaborator

lets add a line to the tests so that we know it doesnt error.

Comment on lines 18 to 19
export COMPOSER=composer.contrib.json
.ddev/commands/web/expand-composer-json "$DDEV_PROJECT_NAME"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This command should assume that composer has already been run. Thats how phpunit command works, for example. So we can remove these lines.

@weitzman weitzman merged commit e284f0d into ddev:main Aug 5, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for phpstan
9 participants