Skip to content

Commit

Permalink
Add getNewPhpcsMessages to README also
Browse files Browse the repository at this point in the history
  • Loading branch information
sirbrillig committed Nov 27, 2018
1 parent 9f9075a commit fd6e4c4
Showing 1 changed file with 33 additions and 24 deletions.
57 changes: 33 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,20 @@ Using this script you can get phpcs output which applies only to the changes you

## 🐘 PHP Library

This library exposes a function `getNewPhpcsMessagesFromFiles()` which takes three arguments:
### getNewPhpcsMessagesFromFiles

This library exposes a function `PhpcsMessages\getNewPhpcsMessagesFromFiles()` which takes three arguments:

- A file path containing the full unified diff of a single file.
- A file path containing the messages resulting from running phpcs on the file before your recent changes.
- A file path containing the messages resulting from running phpcs on the file after your recent changes.

It will return an instance of `PhpcsMessages` which is a filtered list of the third argument above where every line that was present in the second argument has been removed.

### PhpcsMessages

This represents the output of running phpcs.
`PhpcsMessages` represents the output of running phpcs.

To read the phpcs JSON output from an instance of `PhpcsMessages`, you can run `$messages->toPhpcsJson()`.

### PHP Usage

```php
use function PhpcsChanged\getNewPhpcsMessagesFromFiles;
$changedMessages = getNewPhpcsMessagesFromFiles(
Expand Down Expand Up @@ -58,22 +56,42 @@ This will output something like:
"column": 5,
"source": "ImportDetection.Imports.RequireImports.Import",
"message": "Found unused symbol Foobar."
},
{
"line": 21,
"type": "WARNING",
"severity": 5,
"fixable": false,
"column": 5,
"source": "ImportDetection.Imports.RequireImports.Import",
"message": "Found unused symbol Foobar."
}
]
}
}
}
```


### getNewPhpcsMessages

If the previous function is not sufficient, this library exposes a lower-level function `PhpcsMessages\getNewPhpcsMessages()` which takes three arguments:

- (string) The full unified diff of a single file.
- (PhpcsMessages) The messages resulting from running phpcs on the file before your recent changes.
- (PhpcsMessages) The messages resulting from running phpcs on the file after your recent changes.

It will return an instance of `PhpcsMessages` which is a filtered list of the third argument above where every line that was present in the second argument has been removed.

You can create an instance of `PhpcsMessages` from real phpcs JSON output by using `PhpcsMessages::fromPhpcsJson()`.

```php
use function PhpcsChanged\getNewPhpcsMessages;
use PhpcsChanged\PhpcsMessages;
$changedMessages = getNewPhpcsMessages(
$unifiedDiff,
PhpcsMessages::fromPhpcsJson($oldFilePhpcsOutput),
PhpcsMessages::fromPhpcsJson($newFilePhpcsOutput)
use function PhpcsChanged\getNewPhpcsMessagesFromFiles;
$changedMessages = getNewPhpcsMessagesFromFiles(
$unifiedDiffFileName,
$oldFilePhpcsOutputFileName,
$newFilePhpcsOutputFileName
);
echo $changedMessages->toPhpcsJson();
```

## 👩‍💻 CLI Usage

To use this, you'll need data from your version control system and from phpcs.
Expand Down Expand Up @@ -115,15 +133,6 @@ Both will output something like:
"column": 5,
"source": "ImportDetection.Imports.RequireImports.Import",
"message": "Found unused symbol Foobar."
},
{
"line": 21,
"type": "WARNING",
"severity": 5,
"fixable": false,
"column": 5,
"source": "ImportDetection.Imports.RequireImports.Import",
"message": "Found unused symbol Foobar."
}
]
}
Expand Down

0 comments on commit fd6e4c4

Please sign in to comment.