Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: micheh/phpcs-gitlab
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 1.0.0
Choose a base ref
...
head repository: micheh/phpcs-gitlab
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref

Commits on Jul 2, 2020

  1. Bump codecov/codecov-action from v1.0.7 to v1.0.10

    Bumps [codecov/codecov-action](https://github.com/codecov/codecov-action) from v1.0.7 to v1.0.10.
    - [Release notes](https://github.com/codecov/codecov-action/releases)
    - [Commits](codecov/codecov-action@v1.0.7...f357072)
    
    Signed-off-by: dependabot[bot] <support@github.com>
    dependabot[bot] authored Jul 2, 2020
    Copy the full SHA
    d72f5c5 View commit details

Commits on Jul 4, 2020

  1. Merge pull request #1 from micheh/dependabot/github_actions/codecov/c…

    …odecov-action-v1.0.10
    
    Bump codecov/codecov-action from v1.0.7 to v1.0.10
    micheh authored Jul 4, 2020
    Copy the full SHA
    3f571de View commit details
  2. Add PHP matrix

    micheh committed Jul 4, 2020
    Copy the full SHA
    600d044 View commit details
  3. Copy the full SHA
    06d6f2d View commit details

Commits on Jul 6, 2020

  1. Bump shivammathur/setup-php from 2.3.1 to 2.3.2

    Bumps [shivammathur/setup-php](https://github.com/shivammathur/setup-php) from 2.3.1 to 2.3.2.
    - [Release notes](https://github.com/shivammathur/setup-php/releases)
    - [Commits](shivammathur/setup-php@2.3.1...c0d425d)
    
    Signed-off-by: dependabot[bot] <support@github.com>
    dependabot[bot] authored Jul 6, 2020
    Copy the full SHA
    3fabf26 View commit details

Commits on Jul 7, 2020

  1. Merge pull request #2 from micheh/dependabot/github_actions/shivammat…

    …hur/setup-php-2.3.2
    
    Bump shivammathur/setup-php from 2.3.1 to 2.3.2
    micheh authored Jul 7, 2020
    Copy the full SHA
    4d381a7 View commit details

Commits on Jul 23, 2020

  1. Bump codecov/codecov-action from v1.0.10 to v1.0.12

    Bumps [codecov/codecov-action](https://github.com/codecov/codecov-action) from v1.0.10 to v1.0.12.
    - [Release notes](https://github.com/codecov/codecov-action/releases)
    - [Commits](codecov/codecov-action@v1.0.10...07127fd)
    
    Signed-off-by: dependabot[bot] <support@github.com>
    dependabot[bot] authored Jul 23, 2020
    Copy the full SHA
    7f83791 View commit details

Commits on Aug 4, 2020

  1. Bump shivammathur/setup-php from 2.3.2 to 2.4.1

    Bumps [shivammathur/setup-php](https://github.com/shivammathur/setup-php) from 2.3.2 to 2.4.1.
    - [Release notes](https://github.com/shivammathur/setup-php/releases)
    - [Commits](shivammathur/setup-php@2.3.2...9dae796)
    
    Signed-off-by: dependabot[bot] <support@github.com>
    dependabot[bot] authored Aug 4, 2020
    Copy the full SHA
    b9c9167 View commit details

Commits on Aug 5, 2020

  1. Merge pull request #5 from micheh/dependabot/github_actions/codecov/c…

    …odecov-action-v1.0.12
    
    Bump codecov/codecov-action from v1.0.10 to v1.0.12
    micheh authored Aug 5, 2020
    Copy the full SHA
    f919510 View commit details
  2. Merge pull request #6 from micheh/dependabot/github_actions/shivammat…

    …hur/setup-php-2.4.1
    
    Bump shivammathur/setup-php from 2.3.2 to 2.4.1
    micheh authored Aug 5, 2020
    Copy the full SHA
    c299b91 View commit details

Commits on Aug 18, 2020

  1. Bump codecov/codecov-action from v1.0.12 to v1.0.13

    Bumps [codecov/codecov-action](https://github.com/codecov/codecov-action) from v1.0.12 to v1.0.13.
    - [Release notes](https://github.com/codecov/codecov-action/releases)
    - [Commits](codecov/codecov-action@v1.0.12...6004246)
    
    Signed-off-by: dependabot[bot] <support@github.com>
    dependabot[bot] authored Aug 18, 2020
    Copy the full SHA
    ba1dda0 View commit details
  2. Bump shivammathur/setup-php from 2.4.1 to 2.4.3

    Bumps [shivammathur/setup-php](https://github.com/shivammathur/setup-php) from 2.4.1 to 2.4.3.
    - [Release notes](https://github.com/shivammathur/setup-php/releases)
    - [Commits](shivammathur/setup-php@2.4.1...9e6b9d7)
    
    Signed-off-by: dependabot[bot] <support@github.com>
    dependabot[bot] authored Aug 18, 2020
    Copy the full SHA
    02930e9 View commit details

Commits on Aug 19, 2020

  1. Merge pull request #8 from micheh/dependabot/github_actions/codecov/c…

    …odecov-action-v1.0.13
    
    Bump codecov/codecov-action from v1.0.12 to v1.0.13
    micheh authored Aug 19, 2020
    Copy the full SHA
    aaa7253 View commit details
  2. Merge pull request #9 from micheh/dependabot/github_actions/shivammat…

    …hur/setup-php-2.4.3
    
    Bump shivammathur/setup-php from 2.4.1 to 2.4.3
    micheh authored Aug 19, 2020
    Copy the full SHA
    becc472 View commit details

Commits on Dec 20, 2020

  1. Add severity to output

    micheh committed Dec 20, 2020
    Copy the full SHA
    09b870b View commit details
  2. Update dependencies

    micheh committed Dec 20, 2020
    Copy the full SHA
    fd64e65 View commit details

Commits on May 13, 2021

  1. Update dependencies

    micheh committed May 13, 2021
    Copy the full SHA
    5e678f5 View commit details
  2. Copy the full SHA
    c0b6d91 View commit details
  3. Copy the full SHA
    1acad14 View commit details

Commits on Oct 1, 2021

  1. Bump codecov/codecov-action from 1.5.0 to 2.1.0

    Bumps [codecov/codecov-action](https://github.com/codecov/codecov-action) from 1.5.0 to 2.1.0.
    - [Release notes](https://github.com/codecov/codecov-action/releases)
    - [Changelog](https://github.com/codecov/codecov-action/blob/master/CHANGELOG.md)
    - [Commits](codecov/codecov-action@v1.5.0...v2.1.0)
    
    ---
    updated-dependencies:
    - dependency-name: codecov/codecov-action
      dependency-type: direct:production
      update-type: version-update:semver-major
    ...
    
    Signed-off-by: dependabot[bot] <support@github.com>
    dependabot[bot] authored Oct 1, 2021
    Copy the full SHA
    6806d26 View commit details
  2. Bump shivammathur/setup-php from 2.11.0 to 2.15.0

    Bumps [shivammathur/setup-php](https://github.com/shivammathur/setup-php) from 2.11.0 to 2.15.0.
    - [Release notes](https://github.com/shivammathur/setup-php/releases)
    - [Commits](shivammathur/setup-php@2.11.0...2.15.0)
    
    ---
    updated-dependencies:
    - dependency-name: shivammathur/setup-php
      dependency-type: direct:production
      update-type: version-update:semver-minor
    ...
    
    Signed-off-by: dependabot[bot] <support@github.com>
    dependabot[bot] authored Oct 1, 2021
    Copy the full SHA
    6874acf View commit details

Commits on Oct 2, 2021

  1. Merge pull request #39 from micheh/dependabot/github_actions/codecov/…

    …codecov-action-2.1.0
    
    Bump codecov/codecov-action from 1.5.0 to 2.1.0
    micheh authored Oct 2, 2021
    Copy the full SHA
    25a5671 View commit details
  2. Merge pull request #40 from micheh/dependabot/github_actions/shivamma…

    …thur/setup-php-2.15.0
    
    Bump shivammathur/setup-php from 2.11.0 to 2.15.0
    micheh authored Oct 2, 2021
    Copy the full SHA
    ce7fcfb View commit details

Commits on Dec 30, 2024

  1. Update readme

    micheh committed Dec 30, 2024
    Copy the full SHA
    914c770 View commit details
  2. Use PHPStan instead of Psalm

    micheh committed Dec 30, 2024
    Copy the full SHA
    7d7f092 View commit details
  3. Update codecov action

    micheh committed Dec 30, 2024
    Copy the full SHA
    e2cfe8e View commit details
  4. Add PHP 8.4 test

    micheh committed Dec 30, 2024
    Copy the full SHA
    13fdf71 View commit details
  5. Preserve same fingerprint after issue moved to another line

    Antanas Strumila authored and micheh committed Dec 30, 2024
    Copy the full SHA
    beb79ba View commit details
  6. Add counter to fingerprint

    micheh committed Dec 30, 2024
    Copy the full SHA
    016ddc2 View commit details
  7. Test with PHPUnit 9 & 10

    micheh committed Dec 30, 2024
    Copy the full SHA
    bf91eba View commit details
  8. Add upgrade information

    micheh committed Dec 30, 2024
    Copy the full SHA
    bd60447 View commit details

Commits on Jan 1, 2025

  1. Add the line & column to the fingerprint only if the original fingerp…

    …rint appears multiple times
    micheh committed Jan 1, 2025
    Copy the full SHA
    45aeaaa View commit details
  2. Copy the full SHA
    48d42b4 View commit details
4 changes: 2 additions & 2 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -8,9 +8,9 @@ updates:
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "daily"
interval: "monthly"

- package-ecosystem: "composer"
directory: "/"
schedule:
interval: "daily"
interval: "monthly"
57 changes: 43 additions & 14 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -5,34 +5,63 @@ on: [push, pull_request]
jobs:
phpunit:
runs-on: ubuntu-latest
strategy:
matrix:
php-versions: ['7.4', '8.4']
dependencies:
- lowest
- highest

name: phpunit (${{ matrix.php-versions }}-${{ matrix.dependencies }})

steps:
- uses: actions/checkout@v2
- uses: shivammathur/setup-php@2.3.1
- uses: actions/checkout@v4.2.2

- uses: shivammathur/setup-php@2.31.1
with:
php-version: ${{ matrix.php-versions }}
coverage: none
if: matrix.php-versions != '8.4' || matrix.dependencies != 'highest'

- uses: shivammathur/setup-php@2.31.1
with:
php-version: 7.3
php-version: ${{ matrix.php-versions }}
coverage: pcov
if: matrix.php-versions == '8.4' && matrix.dependencies == 'highest'

- name: Setup problem matcher for PHPUnit
run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"

- name: Install dependencies
if: matrix.dependencies == 'lowest'
run: composer update --prefer-lowest

- name: Install dependencies
if: matrix.dependencies == 'highest'
run: composer install

- name: Run PHPUnit
run: vendor/bin/phpunit
if: matrix.php-versions != '8.4' || matrix.dependencies != 'highest'

- name: Run PHPUnit with coverage
run: vendor/bin/phpunit --coverage-clover=coverage.xml
if: matrix.php-versions == '8.4' && matrix.dependencies == 'highest'

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v1.0.7
uses: codecov/codecov-action@v5.1.2
with:
token: ${{ secrets.CODECOV_TOKEN }}
if: matrix.php-versions == '8.4' && matrix.dependencies == 'highest'

phpcs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: shivammathur/setup-php@2.3.1
- uses: actions/checkout@v4.2.2
- uses: shivammathur/setup-php@2.31.1
with:
php-version: 7.3
php-version: 7.4
coverage: none
tools: cs2pr

@@ -42,18 +71,18 @@ jobs:
- name: Run PHP Codesniffer
run: vendor/bin/phpcs --report=checkstyle -q | cs2pr --graceful-warnings

psalm:
phpstan:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: shivammathur/setup-php@2.3.1
- uses: actions/checkout@v4.2.2
- uses: shivammathur/setup-php@2.31.1
with:
php-version: 7.3
php-version: 7.4
coverage: none

- name: Install dependencies
run: composer install

- name: Run Psalm
run: vendor/bin/psalm --output-format=github
- name: Run PHPStan
run: vendor/bin/phpstan
40 changes: 28 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@

Gitlab Report for PHP_CodeSniffer
GitLab Report for PHP_CodeSniffer
---------------------------------
![Main workflow](https://github.com/micheh/phpcs-gitlab/workflows/Main%20workflow/badge.svg)
[![codecov](https://codecov.io/gh/micheh/phpcs-gitlab/branch/master/graph/badge.svg)](https://codecov.io/gh/micheh/phpcs-gitlab)
![Main workflow](https://github.com/micheh/phpcs-gitlab/actions/workflows/main.yml/badge.svg)
[![codecov](https://codecov.io/github/micheh/phpcs-gitlab/graph/badge.svg?token=02FSF3TT0T)](https://codecov.io/github/micheh/phpcs-gitlab)


This library adds a custom report to [PHP_CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer) (phpcs) to generate a codequality artifact, which can be used by Gitlab CI/CD.
The custom report will be generated in the Code Climate format and allows Gitlab CI/CD to display the violations in the Code Quality report.
This library adds a custom report to [PHP_CodeSniffer](https://github.com/PHPCSStandards/PHP_CodeSniffer/) (phpcs) to generate a codequality artifact that can be used by GitLab CI/CD.
The custom report is generated in Code Climate format and allows GitLab CI/CD to display the violations in the Code Quality report.

## Installation

@@ -16,26 +16,42 @@ Install this library using [Composer](https://getcomposer.org):
composer require --dev micheh/phpcs-gitlab
```

Then adjust your `.gitlab-ci.yml` to run PHP_CodeSniffer with the custom reporter and to gather the codequality artifacts:
Then adjust your `.gitlab-ci.yml` to run PHP_CodeSniffer with the custom reporter and to collect the codequality artifacts:

```yaml
phpcs:
script: vendor/bin/phpcs --report=emacs --report-\\Micheh\\PhpCodeSniffer\\Report\\Gitlab=phpcs-quality-report.json
script: vendor/bin/phpcs --report=full --report-\\Micheh\\PhpCodeSniffer\\Report\\Gitlab=phpcs-quality-report.json
artifacts:
reports:
codequality: phpcs-quality-report.json
```
The example above uses two reports, one to display in the build log (emacs) and one to generate the codequality artifact file in the Code Climate format.
The example above uses two reports, one to display in the build log (full) and one to generate the codequality artifact file in Code Climate format.
> **Note:** Gitlab currently does not support multiple codequality artifacts.
> You will not be able to display the violations of multiple tools (e.g. PHP Code Sniffer & PHPStan) in the Code Quality report.
> **Note:** GitLab did not support multiple codequality artifacts before version 15.7.
> If you are using an earlier version of GitLab, you will not be able to see the violations from multiple tools (e.g. PHP Code Sniffer & PHPStan) in the Code Quality report.
Inside the codequality artifact, GitLab expects relative paths to the files with violations.
To generate relative paths with PHP Code Sniffer, set the `basepath` argument in your `phpcs.xml.dist` configuration file with `<arg name="basepath" value="."/>` or run phpcs with `--basepath=.` (adjust the base path as needed).

It is also possible to specify the reports to be used in the `phpcs.xml.dist` file:

```xml
<arg name="report" value="full"/>
<arg name="report-\Micheh\PhpCodeSniffer\Report\Gitlab" value="phpcs-quality-report.json"/>
```

## Upgrade from version 1 to 2

The usage of this package remains the same.
However, the calculation of the fingerprint has been updated and is now based on the content instead of the line number.
This has the advantage that lines with violations can move up or down and GitLab will not report them as new violations.
When upgrading to version 2, it is likely that all violations will show up as changed once, since all fingerprints are new.

## References

- [PHP_CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer)
- [Gitlab CI/CD Code Quality](https://docs.gitlab.com/ee/user/project/merge_requests/code_quality.html)
- [PHP_CodeSniffer](https://github.com/PHPCSStandards/PHP_CodeSniffer/)
- [GitLab CI/CD Code Quality](https://docs.gitlab.com/ee/ci/testing/code_quality.html)
- [Code Climate Specification](https://github.com/codeclimate/platform/blob/master/spec/analyzers/SPEC.md#data-types)


9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "micheh/phpcs-gitlab",
"description": "Gitlab Report for PHP_CodeSniffer (display the violations in the Gitlab CI/CD Code Quality Report)",
"description": "GitLab Report for PHP_CodeSniffer (display the violations in the GitLab CI/CD Code Quality Report)",
"type": "library",
"license": "BSD-3-Clause",
"authors": [
@@ -14,16 +14,17 @@
"php_codesniffer",
"gitlab",
"code quality",
"code climate",
"report"
],
"minimum-stability": "stable",
"require": {
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "^9.0",
"squizlabs/php_codesniffer": "^3.5",
"vimeo/psalm": "^3.12"
"phpstan/phpstan": "^2.0",
"phpunit/phpunit": "^9.3 || ^10.0",
"squizlabs/php_codesniffer": "^3.5.0"
},
"autoload": {
"psr-4": {
1 change: 1 addition & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -15,4 +15,5 @@

<file>src</file>
<file>tests</file>
<exclude-pattern>tests/_files/*</exclude-pattern>
</ruleset>
10 changes: 10 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

parameters:
level: 8
paths:
- src
- tests
excludePaths:
- tests/_files/
scanDirectories:
- vendor/squizlabs/php_codesniffer
32 changes: 18 additions & 14 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>

<!-- https://phpunit.readthedocs.io/en/latest/configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/squizlabs/php_codesniffer/autoload.php"
colors="true"
forceCoversAnnotation="true"
>
<testsuites>
<testsuite name="Project Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
colors="true">

<php>
<const name="PHP_CODESNIFFER_IN_TESTS" value="true"/>
<const name="PHP_CODESNIFFER_CBF" value="false"/>
<const name="PHP_CODESNIFFER_VERBOSITY" value="0"/>
</php>

<testsuites>
<testsuite name="Project Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>

<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
</whitelist>
</filter>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
</phpunit>
16 changes: 0 additions & 16 deletions psalm.xml.dist

This file was deleted.

54 changes: 47 additions & 7 deletions src/Report/Gitlab.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/**
* @copyright Copyright (c) 2020, Michel Hunziker <info@michelhunziker.com>
* @copyright Copyright (c) 2025, Michel Hunziker <info@michelhunziker.com>
* @license http://www.opensource.org/licenses/BSD-3-Clause The BSD-3-Clause License
*/

@@ -11,8 +11,12 @@

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Reports\Report;
use SplFileObject;

use function count;
use function is_string;
use function md5;
use function preg_replace;
use function rtrim;
use function str_replace;

@@ -21,20 +25,26 @@
class Gitlab implements Report
{
/**
* @psalm-suppress ImplementedParamTypeMismatch PHP_CodeSniffer has a wrong docblock
* @param array{filename: string, errors: int, warnings: int, fixable: int, messages: array<mixed>} $report
*/
public function generateFileReport($report, File $phpcsFile, $showSources = false, $width = 80)
{
$hasOutput = false;
$violations = [];

foreach ($report['messages'] as $line => $lineErrors) {
foreach ($lineErrors as $column => $colErrors) {
$lineContent = $this->getContentOfLine($phpcsFile->getFilename(), $line);

foreach ($lineErrors as $col => $colErrors) {
foreach ($colErrors as $error) {
$issue = [
$fingerprint = md5($report['filename'] . $lineContent . $error['source']);

$violations[$fingerprint][$line . $col] = [
'type' => 'issue',
'categories' => ['Style'],
'check_name' => $error['source'],
'fingerprint' => md5($report['filename'] . $error['message'] . $line . $column),
'fingerprint' => $fingerprint,
'severity' => $error['type'] === 'ERROR' ? 'major' : 'minor',
'description' => str_replace(["\n", "\r", "\t"], ['\n', '\r', '\t'], $error['message']),
'location' => [
'path' => $report['filename'],
@@ -44,10 +54,19 @@ public function generateFileReport($report, File $phpcsFile, $showSources = fals
]
],
];
}
}
}

echo json_encode($issue, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . ',';
$hasOutput = true;
foreach ($violations as $fingerprints) {
$hasMultiple = count($fingerprints) > 1;
foreach ($fingerprints as $lineColumn => $issue) {
if ($hasMultiple) {
$issue['fingerprint'] = md5($issue['fingerprint'] . $lineColumn);
}

echo json_encode($issue, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . ',';
$hasOutput = true;
}
}

@@ -67,4 +86,25 @@ public function generate(
) {
echo '[' . rtrim($cachedData, ',') . ']' . PHP_EOL;
}

/**
* @param string $filename
* @param int $line
* @return string
*/
private function getContentOfLine($filename, $line)
{
$file = new SplFileObject($filename);

if (!$file->eof()) {
$file->seek($line - 1);
$contents = $file->current();

if (is_string($contents)) {
return (string) preg_replace('/\s+/', '', $contents);
}
}

return '';
}
}
Loading