Skip to content
This repository has been archived by the owner on Jul 20, 2018. It is now read-only.

Commit

Permalink
Base VIPRestrictedClassesCheck on ClassCheckVisitor, and modify test …
Browse files Browse the repository at this point in the history
…accordingly
  • Loading branch information
ockham committed Dec 6, 2014
1 parent 48c78fb commit ed41afc
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 46 deletions.
16 changes: 6 additions & 10 deletions tests/checks/test-VIPRestrictedClassesCheck.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
<?php

require_once( 'CheckTestBase.php' );
require_once( 'PhpFileCheckTestBase.php' );

class VIPRestrictedClassesTest extends CheckTestBase {
class VIPRestrictedClassesTest extends PhpFileCheckTestBase {

public function testWPClasses() {
$class_name = 'WP_User_Query';

$file_contents = '<?php $dummy = new ' . $class_name . '( $args )';

$error_slugs = $this->runCheck( $file_contents );

$this->assertContains( $class_name, $error_slugs );
public function expectedErrors() {
return array(
array( 3, 'WP_User_Query', 'Note', 'Use of WP_User_Query' ),
);
}
}
3 changes: 3 additions & 0 deletions tests/data/VIPRestrictedClassesTest.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

$dummy = new WP_User_Query( $args );
43 changes: 7 additions & 36 deletions vip-scanner/checks/VIPRestrictedClassesCheck.php
Original file line number Diff line number Diff line change
@@ -1,42 +1,13 @@
<?php

class VIPRestrictedClassesCheck extends BaseCheck
class VIPRestrictedClassesCheck extends ClassCheckVisitor
{
function check( $files ) {
$result = true;

$class_names = array(
// WordPress Classes
"WP_User_Query" => array( 'level' => "Note", "note" => "Use of WP_User_Query" ),
function __construct() {
parent::__construct(
array(
// WordPress Classes
"WP_User_Query" => array( 'level' => "Note", "note" => "Use of WP_User_Query" ),
)
);


foreach ( $this->filter_files( $files, 'php' ) as $file_path => $file_content ) {
foreach ( $class_names as $class_name => $check_info ) {
$this->increment_check_count();

if ( strpos( $file_content, $class_name ) !== false ) {
$pattern = "/\s+($class_name)+\s?\(+/msiU";

if ( preg_match( $pattern, $file_content, $matches ) ) {
$filename = $this->get_filename( $file_path );

$lines = $this->grep_content( rtrim( $matches[0], '(' ), $file_content );

$this->add_error(
$class_name,
$check_info['note'],
$check_info['level'],
$filename,
$lines
);

$result = false;
}
}
}
}

return $result;
}
}

0 comments on commit ed41afc

Please sign in to comment.