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 Apr 25, 2017
1 parent 41e16ed commit b025699
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
49 changes: 49 additions & 0 deletions bears/php/PHPStanBear.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from coalib.bearlib.abstractions.Linter import linter
from dependency_management.requirements.ComposerRequirement import (
ComposerRequirement)


@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'}
REQUIREMENTS = {ComposerRequirement('phpstan/phpstan')}
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,
phpstan_level is set to 1
(as 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',)
if phpstan_config != '' and phpstan_level != '0':
args += ('--level='+phpstan_level, '-c ' +
phpstan_config, filename,)
elif phpstan_config:
phpstan_level = '1'
args += ('--level='+phpstan_level, '-c ' +
phpstan_config, filename,)
else:
args += ('--level='+phpstan_level, filename,)
return args
59 changes: 59 additions & 0 deletions tests/php/PHPStanBearTest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import os
from queue import Queue
from shutil import which
from unittest.case import skipIf

from bears.php.PHPStanBear import PHPStanBear
from coalib.testing.LocalBearTestHelper import LocalBearTestHelper
from coalib.settings.Section import Section
from coalib.results.Result import RESULT_SEVERITY, Result
from coalib.settings.Setting import Setting


@skipIf(which('phpstan') is None, 'PHPStan is not installed')
class PHPStanTest(LocalBearTestHelper):

def setUp(self):
self.section = Section('test section')
self.uut = PHPStanBear(self.section, Queue())
self.test_file1 = os.path.join(os.path.dirname(__file__),
'test_files',
'phplint_test1.php')
self.test_file2 = os.path.join(os.path.dirname(__file__),
'test_files',
'phplint_test2.php')
self.config_file = os.path.join(os.path.dirname(__file__),
'test_files',
'phpstan.neon')

def test_run(self):
# Test for a particular output
self.check_results(
self.uut,
[],
[Result.from_values(self.uut,
"Syntax error, unexpected ';' on line 3",
line=3,
severity=RESULT_SEVERITY.NORMAL,
file=self.test_file1)],
filename=self.test_file1)

# Test a file with errors and warnings
self.check_validity(
self.uut,
[],
self.test_file1,
valid=False)
self.section.append(Setting('phpstan_config', 'phpstan.neon'))
# Test a file without any issues with a config file
self.check_validity(
self.uut,
[],
self.test_file2)
self.section.append(Setting('phpstan_level', '3'))
self.section.append(Setting('phpstan_config', 'phpstan.neon'))
# Test a file without any issues with level set and a config file
self.check_validity(
self.uut,
[],
self.test_file2)
2 changes: 2 additions & 0 deletions tests/php/test_files/phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
parameters:
Dirset: %rootDir%/../coala-bears/tests/php/test_files/

0 comments on commit b025699

Please sign in to comment.