Skip to content

#72 Add tomasvotruba/cognitive-complexity. - #95

Merged
hkirsman merged 7 commits into
mainfrom
feature/72-Re-add-code-complexity
Oct 15, 2024
Merged

hkirsman merged 7 commits into
mainfrom
feature/72-Re-add-code-complexity

Conversation

@hkirsman

@hkirsman hkirsman commented May 29, 2023 •

Copy link
Copy Markdown
Contributor

Description

2 years ago we had to remove code complexity checks https://github.com/wunderio/code-quality/pull/71/files#diff-368d327cbe54fd3c9d4537668316b016ca7aee4c2e09f60cca8d9c67f711d27eL15 when adding support for D9 for Code Quality. We were waiting for Drupal 10 to come out to add it back, but it seems there's a new package that can be used and there's no Symfony restriction on it https://github.com/TomasVotruba/cognitive-complexity

Testing notes

  1. Include this version of Code Quality

    lando composer require wunderio/code-quality:dev-feature/72-Re-add-code-complexity --dev --with-all-dependencies

  2. Edit your local phpstan.neon file and add (use tabs)

	cognitive_complexity:
		class: 50
		function: 16

OR copy over the file from this version:

cp vendor/wunderio/code-quality/config/phpstan.neon ./phpstan.neon

image

  1. Update some file in your codebase, make it lengthy, if's inside if's is good :) , or maybe just copy paste until it's like 100 lines long

OR

Create file at web/modules/custom/updates_file.php (it needs to be under web/modules/custom/)
Ask Copilot To create the code.
Create in PHP 10 foreaches inside each other in a function. Please write out each foreach.

image

  1. Stage the file to GIT

    git add web/modules/custom/updates_file.php

Try to commit and see if the you see alert about cognitive complexity

`git commit`

You should see something like this along the lines:

image

@hkirsman
hkirsman force-pushed the feature/72-Re-add-code-complexity branch 3 times, most recently from 2c9c89c to c93f7c0 Compare May 29, 2023 10:08
@hkirsman
hkirsman force-pushed the feature/72-Re-add-code-complexity branch from c93f7c0 to 11101c9 Compare May 29, 2023 10:09
@hkirsman
hkirsman requested a review from vermario May 29, 2023 12:29
Comment thread src/Drupal/DrupalAutoloader.php
Comment thread config/phpstan.neon Outdated
@hkirsman
hkirsman requested a review from Juhani-moilanen May 30, 2023 08:29
@hkirsman
hkirsman requested a review from misterjoonas May 31, 2023 04:44
@misterjoonas

Copy link
Copy Markdown
Member

I tested this in Metropolia project, got this:

php_stan
========

These files are included multiple times:
- /app/vendor/mglaman/phpstan-drupal/extension.neon
- /app/vendor/phpstan/phpstan-deprecation-rules/rules.neon

It can lead to unexpected results. If you're using phpstan/extension-installer, make sure you have removed corresponding neon files from your project config file.
To skip commit checks, add -n or --no-verify flag to commit command

@Juhani-moilanen Juhani-moilanen left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Tested in VaKe

Locally all is good -- threw an error that complexity is too high

tried the" deploy" step also -- should it "flag" all files?
IIRC then before it only searched those files that were changed in the commit and did not scan all of them

https://app.circleci.com/pipelines/github/wunderio/client-fi-vake/617/workflows/21d29aa0-7e18-488f-b8d4-3a7f610a6b54/jobs/2053

Comment thread composer.json
"mglaman/phpstan-drupal": "^1.1",
"phpstan/extension-installer": "^1.1",
"phpstan/phpstan-deprecation-rules": "^1.0",
"nette/finder": "^2.5",

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Not 100% sure if nette/finder is needed as nothing hasn't been broken before, then again it is used in https://github.com/wunderio/code-quality/blob/main/src/Drupal/DrupalAutoloader.php

@hkirsman

Copy link
Copy Markdown
Contributor Author

@misterjoonas you need to remoe these from phpstan.neon file as they are loaded automatically now. I'm soon merging this and creating 3.1 version for Code Quality.

@hkirsman
hkirsman merged commit b9acf69 into main Oct 15, 2024
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.

5 participants