https://github.com/codacy/codacy-codesniffer
Codacy tool for PHP Codesniffer
https://github.com/codacy/codacy-codesniffer
codacy codesniffer codesniffer-rules docker php php-code-sniffer php-codesniffer review scala
Last synced: 9 months ago
JSON representation
Codacy tool for PHP Codesniffer
- Host: GitHub
- URL: https://github.com/codacy/codacy-codesniffer
- Owner: codacy
- License: other
- Created: 2016-08-05T10:56:36.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2025-04-16T21:12:30.000Z (9 months ago)
- Last Synced: 2025-04-20T11:43:52.232Z (9 months ago)
- Topics: codacy, codesniffer, codesniffer-rules, docker, php, php-code-sniffer, php-codesniffer, review, scala
- Language: Scala
- Homepage:
- Size: 1.48 MB
- Stars: 7
- Watchers: 14
- Forks: 15
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# Codacy PHP_CodeSniffer
This is the docker engine we use at Codacy to have [PHP_CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer) support.
You can also create a docker to integrate the tool and language of your choice!
See the [codacy-engine-scala-seed](https://github.com/codacy/codacy-engine-scala-seed) repository for more information.
[](https://www.codacy.com/gh/codacy/codacy-codesniffer?utm_source=github.com&utm_medium=referral&utm_content=codacy/codacy-codesniffer&utm_campaign=Badge_Grade)
[](https://circleci.com/gh/codacy/codacy-codesniffer)
## Usage
You can create the docker by doing:
```bash
docker build -t codacy-codesniffer .
```
The docker is ran with the following command:
```bash
docker run -it -v $srcDir:/src :
```
## Test
We use the [codacy-plugins-test](https://github.com/codacy/codacy-plugins-test) to test our external tools integration.
You can follow the instructions there to make sure your tool is working as expected.
## Generating the documentation
Requires `phpdoc`
```bash
wget https://github.com/phpDocumentor/phpDocumentor/releases/download/v3.4.3/phpDocumentor.phar
sudo mv phpDocumentor.phar /usr/local/bin/phpdoc
sudo chmod +x /usr/local/bin/phpdoc
```
Update the versions in `composer.json` and run
```bash
sbt "doc-generator/runMain codacy.codesniffer.docsgen.GeneratorMain"
```
This will create updated `patterns.json`, `description.json` and the individual documentation Markdown files.
## CodeSniffer configuration file
PHP CodeSniffer can be configured by adding a `phpcs.xml` file to the source code.
Currently, the tool supports this configuration file except from one feature - setting installed_paths: ``
## Add new Codesniffer plugin
To add a new plugin to Codesniffer:
1. Add the plugin dependency to `require` inside `composer.json`. This depedency must be available on [Packagist](https://packagist.org/)
2. Add to `src/main/scala/codacy/codesniffer/docsgen/VersionsHelper.scala` the plugin version:
```scala
lazy val newPlugin = properties("pluginNamespace/pluginName").str.replace("^","")
```
3. Implement the plugin documentation parser inside `src/main/scala/codacy/codesniffer/docsgen/parsers`. The parser must extend `DocsParser` and override the following:
| Name | Description |
| ------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------- |
| `override val repositoryURL` | Plugin's git repository |
| `override val checkoutCommit` | Commit related to the version on plugin's git repository (you can use the version tag for that) |
| `override val sniffRegex` | Regex to get all Sniffs implementation files |
| `override def patternIdPartsFor(relativizedFilePath: String): PatternIdParts` | Get the pattern id parts from pattern path |
| `override def descriptionWithDocs(rootDir: File, patternIdParts: PatternIdParts, patternFile: File): (Pattern.Description, Option[String])` | Get tuple with small and extended description for pattern |
Example:
```scala
class DrupalCoderDocsParser extends DocsParser {
override val repositoryURL = "https://github.com/pfrenssen/coder.git"
override val checkoutCommit: String = VersionsHelper.drupalCoder
override val sniffRegex: Regex = """.*coder_sniffer\/(Drupal)\/Sniffs\/(.*?)\/(.*?)Sniff.php""".r
override def patternIdPartsFor(relativizedFilePath: String): PatternIdParts = {
val sniffRegex(drupalCoderVersion, sniffType, patternName) = relativizedFilePath
PatternIdParts(drupalCoderVersion, sniffType, patternName)
}
override def descriptionWithDocs(rootDir: File,
patternIdParts: PatternIdParts,
patternFile: File): (Pattern.Description, Option[String]) = {
(description(patternIdParts, rootDir), this.parseExtendedDescription("Drupal\\Sniffs", patternIdParts, rootDir))
}
private[this] def description(patternIdParts: PatternIdParts, rootDir: File): Pattern.Description = {
val caseRegexPattern = """((?<=\p{Ll})\p{Lu}|\p{Lu}(?=\p{Ll}))""".r
val patternName = caseRegexPattern.replaceAllIn(patternIdParts.patternName, " $1").trim
val sniffName = caseRegexPattern.replaceAllIn(patternIdParts.sniffType, " $1").trim
val title = Pattern.Title(s"$sniffName: $patternName")
val extended = this.parseDescription("Drupal\\Sniffs", patternIdParts, rootDir)
Pattern.Description(patternIdParts.patternId, title, extended, None, None)
}
}
```
4. Add the new parser to the list of parsers inside `src/main/scala/codacy/codesniffer/docsgen/Generator.scala`.
```scala
private[this] val parsers: List[DocsParser] = List(new PHPCSDocsParser(), ...)
```
5. [Generate the documentation](#generating-the-documentation)
## What is Codacy?
[Codacy](https://www.codacy.com/) is an Automated Code Review Tool that monitors your technical debt, helps you improve your code quality, teaches best practices to your developers, and helps you save time in Code Reviews.
### Among Codacy’s features
- Identify new Static Analysis issues
- Commit and Pull Request Analysis with GitHub, BitBucket/Stash, GitLab (and also direct git repositories)
- Auto-comments on Commits and Pull Requests
- Integrations with Slack, HipChat, Jira, YouTrack
- Track issues in Code Style, Security, Error Proneness, Performance, Unused Code and other categories
Codacy also helps keep track of Code Coverage, Code Duplication, and Code Complexity.
Codacy supports PHP, Python, Ruby, Java, JavaScript, and Scala, among others.
### Free for Open Source
Codacy is free for Open Source projects.