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

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft

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
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.

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
6 participants