Use PHP_CodeSniffer for modified lines only
Asked Answered
S

4

13

I am trying to build a pre-commit script in SVN, and I want to run PHP_CodeSniffer on the modified lines only (as opposed to the whole file). So far I have this script:

#!/bin/sh

REPOS="$1"
TXN="$2"

# Make sure that the log message contains some text.
SVNLOOK=/usr/bin/svnlook
$SVNLOOK log -t "$TXN" "$REPOS" | \
   grep "[a-zA-Z0-9]" > /dev/null || exit 1

# Check for code validation before commiting the script using PHP_CodeSniffer
/tmp/pear/download/PHP_CodeSniffer-1.4.3/scripts/phpcs-svn-pre-commit  "$REPOS" -t "$TXN" >&2 || exit 1

# All checks passed, so allow the commit.
exit 0
Schizophrenia answered 1/1, 2013 at 21:9 Comment(9)
You maybe should outline a little what the problem with what you've tried so far is as well. +1 For building this and asking anyway ;)Cuspidate
phpcs takes the file, parses it to AST and performs analysis on it. So on the checking step - there is no original lines number available. I hardly doubt it's possibleHarlandharle
Like @Harlandharle said, whole file must be parsed, but it might be possible to check if any of changed line is in the set of lines containing cs errors.Grenier
It seems difficult. I recomend looking at svnlook help. You will need to call svnlook changes to see which files have changed on that commit. And then you could use svnlook diff to get the changes made, and parse that to extract the new lines. The problem is that the changed line might not work alone, so you should better check the whole file.Engleman
thanks for your help every one, @El Bardo: that is exactly what am trying to achieve,you said "The problem is that the changed line might not work alone, so you should better check the whole file" i thought phpcs is validating lines of code not blocks.so how about writing these changes to a temp file and phpcs that temp file?Schizophrenia
While the content sniffer does check "lines," it has no way of knowing what's actually changed by the time it runs. Why not diff the output of the file before the changes are applied, then after? It's quick, it's dirty, and it just might work...Schlicher
@Charles: would you write that script ? thanks in advanceSchizophrenia
No, I won't do your work for you.Schlicher
This question might be related and at least worth to cross-link: Git: pre-receive hook with PHP_CodeSniffer - For the line numbers I would take the report because it contains the line numbers and then filter against changed lines in the code. and taking a look if it matches. But this needs some experimentation. The first thing to solve would be to find out which files have been edited in a commit and then also which lines in those files. That should allow one to run PHPCS on those files and then to filter.Cuspidate
P
4

In 2017, since CodeSniffer 3.0.0 release, there is new --cache option.

Or you can use EasyCodingStandard, that does this by default.

Papst answered 21/5, 2017 at 22:51 Comment(0)
N
-1

Here is how it's done in Phabricator's tool Arcanist:

  1. it runs php code sniffer on every changed file and collects all errors
  2. restricts those errors by line number, where only line numbers that were changed in this commit are allowed

This of course won't cover some specific cases, when change in LineA caused error in LineB.

Neckpiece answered 10/12, 2014 at 9:31 Comment(0)
S
-2

Conceptually it does not make sense.

phpcs needs to know the hole context in order to analyze correctly.

Scanning only the changed lines would remove context.

Superficies answered 8/3, 2013 at 15:51 Comment(2)
PHPCS ony works with context of current file only. I'm aware only about 1 sniff, that does need more intell, but it uses bit advanced technique to do so. github.com/Symplify/…Papst
I actually meant the context of the whole file, not the whole project. So the statement is still correct.Superficies
D
-3

I am aware the topic is outdated. I build my own solution to achievie this goal (scaning only recently changed lined). It is premature but works

https://github.com/ayeo/sniffer

Denier answered 15/4, 2017 at 18:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.