oxsecurity / megalinter

🦙 MegaLinter analyzes 50 languages, 22 formats, 21 tooling formats, excessive copy-pastes, spelling mistakes and security issues in your repository sources with a GitHub Action, other CI tools or locally.
https://megalinter.io
GNU Affero General Public License v3.0
1.95k stars 237 forks source link

[Question] defining used PHP version #1575

Closed mimmi20 closed 2 years ago

mimmi20 commented 2 years ago

I am started using the megalinter when upgrading my projects to PHP 8.1.

In one project I got this error:

### Processed [PHP] files
- Using [php v7.4.26] https://megalinter.github.io/v5/descriptors/php_php
- MegaLinter key: [PHP_BUILTIN]
- Rules config: identified by [php]
[php] .php-cs-fixer.php - SUCCESS - 0 error(s)
[php] src/StreamFormatter.php - SUCCESS - 0 error(s)
[php] tests/StreamFormatterTest.php - ERROR - 1 error(s)
--Error detail:
PHP Parse error:  syntax error, unexpected ':', expecting ')' in /github/workspace/tests/StreamFormatterTest.php on line 242
Errors parsing /github/workspace/tests/StreamFormatterTest.php
❌ Linted [PHP] files with [php]: Found 1 error(s) - (0.05s)
[Text Reporter] Generated TEXT report: /github/workspace/report/linters_logs/ERROR-PHP_BUILTIN.log
### Processed [PHP] files
- Using [phplint v3.0] https://megalinter.github.io/v5/descriptors/php_phplint
- MegaLinter key: [PHP_PHPLINT]
- Rules config: [/github/workspace/.phplint.yml]
[phplint] .php-cs-fixer.php
[phplint] src/StreamFormatter.php
[phplint] tests/StreamFormatterTest.php
--Error detail:
phplint 3.0 by overtrue and contributors.
Loaded config from "/github/workspace/.phplint.yml"
.E.
Time: < 1 sec   Memory: 2.0 MiB Cache: No
FAILURES!
Files: 3, Failures: 1
There was 1 errors:
1. /github/workspace/tests/StreamFormatterTest.php:242
    239|         $formatter = new StreamFormatter();
    240| 
    241|         $record = new LogRecord(
  > 242|             datetime: $datetime,
    243|             channel: $channel,
    244|             level: \Monolog\Level::Error,
    245|             message: $message,
 unexpected ':', expecting ')' in line 242
❌ Linted [PHP] files with [phplint]: Found 1 error(s) - (0.08s)

The linter uses PHP 7.4, but the code is written for PHP 8.1.

Is there any option to define the PHP version used by the linter?

Kurt-von-Laven commented 2 years ago

@mimmi20, I have no relevant experience with PHP, but I think the short answer is no. Most likely, your best bet would be to disable phplint, and run it outside of MegaLinter. You can achieve that by adding the following to your MegaLinter config:

DISABLE_LINTERS:
  - PHP_PHPLINT

MegaLinter v6 will be released soon, but most likely without support for running phplint on PHP 8 unfortunately. My understanding is that there were some issues that are making upgrading difficult right now, although @llaville may correct me if I misunderstood.

llaville commented 2 years ago

@Kurt-von-Laven I'm away from my Laptop, but yes there is a solution to switch PHP version runtime used. I'll give the solution when I'll be back in few hours

llaville commented 2 years ago

@Kurt-von-Laven and @mimmi20 PHP switch capability was introduced in MegaLinter since version 5.3 with https://github.com/megalinter/megalinter/pull/1083

All you have to do is to use the PRE_COMMAND syntax on PHP_PHPLINT like that :

Remember that in MegaLinter v5.x, default PHP BUILTIN linter version is PHP 7 (7.4.x)

PHP_PHPLINT_PRE_COMMANDS:
    - command: "update-alternatives --set php /usr/bin/php8"
      cwd: root

NOTE: PHPLint 3.0 is compatible PHP 7.4 and 8.0 (until we use composer built-in with a PHP 7 version)

llaville commented 2 years ago

You should see in MegaLinter logs something like

[Pre][PHP_PHPLINT] run: [update-alternatives --set php /usr/bin/php8] in cwd [/]
[Pre][PHP_PHPLINT] [update-alternatives: using /usr/bin/php8 to provide /usr/bin/php (php) in manual mode
] update-alternatives: using /usr/bin/php8 to provide /usr/bin/php (php) in manual mode

### Processed [PHP] files
- Using [phplint v3.0] https://megalinter.github.io/v5/descriptors/php_phplint
- MegaLinter key: [PHP_PHPLINT]
- Rules config: [.phplint.yml]
- Number of files analyzed: [39]
✅ Linted [PHP] files with [phplint] successfully - (0.22s)
mimmi20 commented 2 years ago

@llaville Does this switch also exists for all other php related linters? As you may see in my sample, the PHP_BUILTIN linter has the same issue.

llaville commented 2 years ago

yes it exist for all PHP linters

mimmi20 commented 2 years ago

I have decided to not change my config file, to disable the php checks for megalinter and run these checks in seperate tasks.