Skip to content

Commit

Permalink
- Implemented: PHP_CodeSniffer check
Browse files Browse the repository at this point in the history
  • Loading branch information
kore committed May 17, 2010
1 parent c5445a1 commit f5728b9
Show file tree
Hide file tree
Showing 11 changed files with 219 additions and 2 deletions.
114 changes: 114 additions & 0 deletions src/classes/check/code_sniffer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php
/**
* Copyright (c) <2009>, all contributors
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name of the project nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* This software is provided by the copyright holders and contributors "as is"
* and any express or implied warranties, including, but not limited to, the
* implied warranties of merchantability and fitness for a particular purpose
* are disclaimed. in no event shall the copyright owner or contributors be
* liable for any direct, indirect, incidental, special, exemplary, or
* consequential damages (including, but not limited to, procurement of
* substitute goods or services; loss of use, data, or profits; or business
* interruption) however caused and on any theory of liability, whether in
* contract, strict liability, or tort (including negligence or otherwise)
* arising in any way out of the use of this software, even if advised of the
* possibility of such damage.
*
* @package php-commit-hooks
* @version $Revision$
* @license http://www.opensource.org/licenses/bsd-license.html New BSD license
*/

include_once 'PHP/CodeSniffer.php';

/**
* Check for valid files
*
* Implements linter checks for all added or modified files.
*
* @package php-commit-hooks
* @version $Revision$
* @license http://www.opensource.org/licenses/bsd-license.html New BSD license
*/
class pchCodeSnifferCheck extends pchCheck
{
/**
* Configured standard to check files with
*
* @var array
*/
protected $standard;

/**
* Construct from CodeSniffer standard name
*
* @param string $standard
* @return void
*/
public function __construct( $standard )
{
$this->standard = $standard;
}

/**
* Validate the current check
*
* Validate the check on the specified repository. Returns an array of
* found issues.
*
* @param pchRepository $repository
* @return void
*/
public function validate( pchRepository $repository )
{
$cs = new PHP_CodeSniffer();
$cs->process( array(), $this->standard );

foreach ( $this->getChangedFiles( $repository ) as $file )
{
$cs->processFile( $file, stream_get_contents( $this->getFileContents( $repository, $file ) ) );
}

$issues = array();
foreach ( $cs->getFilesErrors() as $file => $messages )
{
foreach ( $messages['errors'] as $errors )
{
foreach ( $errors as $subErrors )
{
foreach ( $subErrors as $error )
{
$issues[] = new pchIssue( E_ERROR, $file, null, $error['source'] . ': ' . $error['message'] );
}
}
}

foreach ( $messages['warnings'] as $errors )
{
foreach ( $errors as $subErrors )
{
foreach ( $subErrors as $error )
{
$issues[] = new pchIssue( E_WARNING, $file, null, $error['source'] . ': ' . $error['message'] );
}
}
}
}

return $issues;
}
}

1 change: 1 addition & 0 deletions src/environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
require __DIR__ . '/classes/reporter/mail.php';

require __DIR__ . '/classes/check.php';
require __DIR__ . '/classes/check/code_sniffer.php';
require __DIR__ . '/classes/check/commit_message.php';
require __DIR__ . '/classes/check/lint.php';
require __DIR__ . '/classes/check/lint/base.php';
Expand Down
72 changes: 72 additions & 0 deletions tests/check/code_sniffer_tests.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* Copyright (c) <2009>, all contributors
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name of the project nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* This software is provided by the copyright holders and contributors "as is"
* and any express or implied warranties, including, but not limited to, the
* implied warranties of merchantability and fitness for a particular purpose
* are disclaimed. in no event shall the copyright owner or contributors be
* liable for any direct, indirect, incidental, special, exemplary, or
* consequential damages (including, but not limited to, procurement of
* substitute goods or services; loss of use, data, or profits; or business
* interruption) however caused and on any theory of liability, whether in
* contract, strict liability, or tort (including negligence or otherwise)
* arising in any way out of the use of this software, even if advised of the
* possibility of such damage.
*
* @package php-commit-hooks
* @version $Revision$
* @license http://www.opensource.org/licenses/bsd-license.html New BSD license
*/

/**
* Tests for the commit message parser
*/
class pchCodeSnifferCheckTests extends PHPUnit_Framework_TestCase
{
/**
* Return test suite
*
* @return PHPUnit_Framework_TestSuite
*/
public static function suite()
{
return new PHPUnit_Framework_TestSuite( __CLASS__ );
}

public function testValidLints()
{
$parser = new pchCodeSnifferCheck( 'Arbit' );

$this->assertEquals(
array(
new pchIssue( E_ERROR, 'cs_errors.php', null, 'PEAR.Classes.ClassDeclaration: Opening brace of a class must be on the line after the definition' ),
),
$parser->validate( new pchRepositoryVersion( __DIR__ . '/../data/', 4 ) )
);
}

public function testNoErrors()
{
$parser = new pchCodeSnifferCheck( 'Arbit' );

$this->assertEquals(
array(),
$parser->validate( new pchRepositoryVersion( __DIR__ . '/../data/', 5 ) )
);
}
}

2 changes: 2 additions & 0 deletions tests/check_suite.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
* Commit message parser tests
*/
require 'check/lint_tests.php';
require 'check/code_sniffer_tests.php';

/**
* Test suite for pch
Expand All @@ -60,6 +61,7 @@ public function __construct()
$this->setName( 'php-commit-hooks - checks' );

$this->addTest( pchLintCheckTests::suite() );
$this->addTest( pchCodeSnifferCheckTests::suite() );
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/data/db/current
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3
5
Binary file modified tests/data/db/rep-cache.db
Binary file not shown.
14 changes: 14 additions & 0 deletions tests/data/db/revprops/0/4
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
K 10
svn:author
V 4
kore
K 8
svn:date
V 27
2010-05-17T14:24:47.801407Z
K 7
svn:log
V 44
- Added: File with one intentional CS error

END
14 changes: 14 additions & 0 deletions tests/data/db/revprops/0/5
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
K 10
svn:author
V 4
kore
K 8
svn:date
V 27
2010-05-17T14:25:52.484765Z
K 7
svn:log
V 17
# Fixed CS error

END
Binary file added tests/data/db/revs/0/4
Binary file not shown.
Binary file added tests/data/db/revs/0/5
Binary file not shown.
2 changes: 1 addition & 1 deletion tests/data/db/txn-current
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3
5

0 comments on commit f5728b9

Please sign in to comment.