Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed autoload, class/method visibilities, some phpdoc

  • Loading branch information...
commit 7840c429d4794f8a884d3e2ed1ada9496c9e4b09 1 parent 72a9ac0
@gordjw authored
View
8 example.php
@@ -0,0 +1,8 @@
+<?php
+
+//Simulate autoload
+require_once( dirname( __FILE__ ) . '/lib/phpcs.php' );
+
+$phpcs = new PHPCS();
+$report = $phpcs->generate_report();
+$report->show_all_files();
View
26 lib/afile.php
@@ -0,0 +1,26 @@
+<?php
+/**
+ * File object
+ *
+ * Holds information about each file, such as:
+ ** Filename
+ ** Issues
+ *
+ * @package Phpcs-php
+ * @author Gordon Williamson <gordjw@gmail.com>
+ * @since 1.0
+ * @category
+ * @license
+ * @link
+ */
+
+class AFile
+{
+ var $filename;
+ public $issues;
+
+ function __construct( $filename )
+ {
+ $this->filename = $filename;
+ }
+}
View
6 lib/error.php
@@ -0,0 +1,6 @@
+<?php
+
+class Error extends Issue
+{
+
+}
View
18 lib/issue.php
@@ -0,0 +1,18 @@
+<?php
+
+class Issue
+{
+ public $line;
+ public $message;
+
+ function __construct( $line, $message )
+ {
+ $this->line = $line;
+ $this->message = $message;
+ }
+
+ function _print()
+ {
+ echo get_class( $this ) . " | " . $this->line . " : " . $this->message . "\n";
+ }
+}
View
109 lib/phpcs.php
@@ -0,0 +1,109 @@
+<?php
+/**
+ * Primary PHPCS-PHP interface.
+ *
+ * Create a new PHPCS object, which allows you to generate reports, change files and coding standards
+ *
+ * @author Gordon Williamson <gordjw@gmail.com>
+ * @since 1.0
+ * @packags phpcs-php
+ */
+class PHPCS
+{
+ /**
+ * @access private
+ * @var string
+ */
+ var $filename;
+ /**
+ * @access private
+ * @var string
+ */
+ var $standard;
+
+ /**
+ * @access public
+ * @param string $filename Path (file or directory) to run phpcs over (defaults to '.')
+ * @param string $standard Coding standard to apply to your code
+ * @return object Instance of the PHPCS class, on which you call generate_report
+ */
+ public function __construct( $filename = '.', $standard = 'ZEND' ) {
+ spl_autoload_register( array( $this, 'autoload' ) );
+
+ $this->filename = $filename;
+ $this->standard = $standard;
+ }
+
+ /**
+ * @access public
+ * @param string $filename Change the filename to generate a report on
+ **/
+ public function set_filename( $filename ) { $this->filename = $filename; }
+
+ /**
+ * @access public
+ * @return string Return the filename currently in use
+ **/
+ public function get_filename() { return $this->filename; }
+
+ /**
+ * @access public
+ * @param string $standard Change the coding stanrad to be used
+ **/
+ public function set_standard( $standard ) { $this->standard = $standard; }
+
+ /**
+ * @access public
+ * @return string Return the coding standard currently in use
+ **/
+ public function get_standard() { return $this->standard; }
+
+ /**
+ * @access private
+ * @param string $classname Attempt to autoload $classname
+ */
+ private function autoload( $classname ) {
+ $classname = strtolower( $classname );
+ if( file_exists( dirname(__FILE__) . '/' . $classname . '.php' ) )
+ require_once( dirname(__FILE__) . '/' . $classname . '.php' );
+
+ }
+
+ /**
+ * @access private
+ * @return string Return the raw output of the phpcs command
+ */
+ private function _get_raw()
+ {
+ exec( 'phpcs ' . $this->filename, $raw );
+
+ return $raw;
+ }
+
+ /**
+ * @access public
+ * @return array Return the converted raw output as an array for further use
+ */
+ public function generate_report()
+ {
+ $raw = $this->_get_raw();
+
+ $report = new Report();
+
+ foreach( $raw as $line )
+ {
+ if( preg_match( "/^FILE: (.*)$/", $line, $matches ) )
+ {
+ $file = new AFile( $matches[1] );
+ $report->files[$matches[1]] = $file;
+ }
+
+ if( preg_match( "/^\s+(\d)\s+\|\s+(ERROR|WARNING)\s+\|\s+(.*)$/", $line, $matches ) )
+ {
+ $file->issues[] = new $matches[2]($matches[1], $matches[3]);
+ }
+ }
+
+ return $report;
+ }
+}
View
18 lib/report.php
@@ -0,0 +1,18 @@
+<?php
+
+class Report
+{
+ public $files;
+
+ public function show_all_files()
+ {
+ foreach( $this->files as $file )
+ {
+ echo $file->filename . "\n";
+ foreach( $file->issues as $issue )
+ {
+ $issue->_print();
+ }
+ }
+ }
+}
View
6 lib/warning.php
@@ -0,0 +1,6 @@
+<?php
+
+class Warning extends Issue
+{
+
+}
View
26 main.php
@@ -2,15 +2,28 @@
class PHPCS
{
- static function get_raw( $filename = "." )
+ public function __construct() {
+ spl_autoload_register( array( $this, 'autoload' ) );
+ }
+
+ private function autoload( $classname ) {
+ $classname = strtolower( $classname );
+ if( file_exists( dirname(__FILE__) . '/lib/' . $classname . '.php' ) )
+ require_once( dirname(__FILE__) . '/lib/' . $classname . '.php' );
+
+ }
+
+ private static function _get_raw( $filename )
{
exec( 'phpcs ' . $filename, $raw );
return $raw;
}
- static function generate_report( $raw )
+ public function generate_report( $filename = "." )
{
+ $raw = self::_get_raw( $filename );
+
$report = new Report();
foreach( $raw as $line )
@@ -29,10 +42,11 @@ static function generate_report( $raw )
return $report;
}
-}
+}
-$raw = PHPCS::get_raw();
-$report = PHPCS::generate_report( $raw );
-$report->show_all_files();
+$phpcs = new PHPCS();
+$report = $phpcs->generate_report();
+//$report->show_all_files();
+print_r( $report );
Please sign in to comment.
Something went wrong with that request. Please try again.