Skip to content

Commit

Permalink
PHPStanBear: Add PHPStanBear
Browse files Browse the repository at this point in the history
Introduce PHPStanBear
Checks the code with ``phpstan analyze``.
Closes coala#1426
  • Loading branch information
damngamerz committed Feb 28, 2017
1 parent 8575b3f commit 084b600
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
37 changes: 37 additions & 0 deletions bears/php/PHPStanBear.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import os

from coalib.bearlib.abstractions.Linter import linter


@linter(executable='phpstan',
output_format='regex',
output_regex=r'(?P<line>\d+) (?P<message>.*)')
class PHPStanBear:
"""
Checks the code with ``phpstan analyze``.
This can run it on multiple files and folders.
See <https://github.com/phpstan/phpstan> for more information.
"""
LANGUAGES = {'PHP'}
AUTHORS = {'The coala developers'}
AUTHORS_EMAILS = {'[email protected]'}
LICENSE = 'AGPL-3.0'
CAN_DETECT = {'Syntax', 'Unused Code', 'Variable Misuse',
'Undefined Element', 'Missing Import', 'Spelling'}

@staticmethod
def create_arguments(filename, file, config_file, phpstan_level: str='0', phpstan_config: str=''):
"""
:param phpstan_config:
path to a custom configuration file.
When using a custom project config file, you have to pass the phpstan_level argument
(default value 0 does not apply here).
:param phpstan_level:
To set rule levels.
0 is the loosest and 4 is the strictest.
See <https://github.com/phpstan/phpstan> for more information.
"""
args = ('analyse', '--level='+phpstan_level, filename)
if phpstan_config:
args += ('-c '+phpstan_config,)
return args
23 changes: 23 additions & 0 deletions tests/php/PHPStanBearTest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from bears.php.PHPStanBear import PHPStanBear
from coalib.testing.LocalBearTestHelper import verify_local_bear


good_file = """
<?php
$a = 1;
?>
"""


bad_file = """
<?php
$a =
?>
"""


PHPStanBearTest = verify_local_bear(
PHPStanBear,
valid_files=(good_file,),
invalid_files=(bad_file,),
tempfile_kwargs={'suffix': '.php'})

0 comments on commit 084b600

Please sign in to comment.