Skip to content

Commit

Permalink
Added Namespace support, cleant a bit code and added a sample folder …
Browse files Browse the repository at this point in the history
…for testing purpose
  • Loading branch information
Bashar committed Feb 24, 2015
1 parent 9c1ca73 commit fd2e1b5
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 18 deletions.
File renamed without changes.
4 changes: 2 additions & 2 deletions lib/appController.php
Expand Up @@ -51,7 +51,7 @@ class appController
*/
public static function parseParameters(array $array = array())
{
$result = array();
$results = array();

// Check what options were called
$cpt = 3;
Expand Down Expand Up @@ -126,7 +126,7 @@ public static function compareMethods($results, $path1, $path2)
{
$changed = array();

echo 'Comparing methods between V1:"' . $path1 . '" and V2:"' . $path2 . '"' . PHP_EOL;
echo PHP_EOL . 'Comparing methods between V1:"' . $path1 . '" and V2:"' . $path2 . '"' . PHP_EOL;
foreach($results[0] as $className => $classInfo)
{
$msg = true;
Expand Down
59 changes: 44 additions & 15 deletions lib/codeCompare.php
Expand Up @@ -41,8 +41,22 @@
* @since File available since Release 1.0.0
*/

define('TMP_PATH', sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'apiCompare');

define('TMP_PATH', '/tmp/apiCompare');
if (!defined('T_NAMESPACE'))
{
/**
* This is just for backword compatibilty with previous versions
* Token -1 will never exists but we just want to avoid having undefined
* constant
*/
define('T_NAMESPACE', -1);
define('T_NS_SEPARATOR', -1);
}
if (!defined('T_TRAIT'))
{
define('T_TRAIT', -1);
}

/**
* Code comparator class
Expand Down Expand Up @@ -82,6 +96,7 @@ public static function parseFolder($folder)
$results = array_merge($results, $classNames);
}
}

return $results;
}

Expand All @@ -96,13 +111,38 @@ private static function getClassesFromFile($file)
$classes = array();
$tokens = token_get_all(file_get_contents($file));
$nbtokens = count($tokens);

$classCpt = 0;
$currentClass = null;
$namespace = null;

for($i = 0 ; $i < $nbtokens ; $i++)
{
switch($tokens[$i][0])
{
case T_NAMESPACE:
$i+=2;
while ($tokens[$i][0] === T_STRING || $tokens[$i][0] === T_NS_SEPARATOR)
{
$namespace .= $tokens[$i++][1];
}
break;

case T_INTERFACE:
case T_CLASS:
case T_TRAIT:
$i+=2;
if ($namespace)
{
$currentClass = strtolower($namespace . '\\' . $tokens[$i][1]);
}
else
{
$currentClass = strtolower($tokens[$i][1]);
}

$classes[$currentClass]['fileName'] = $file;
$classes[$currentClass]['methods'] = array();
break;

// Retrieve methods
case T_FUNCTION:
$i+=2;
Expand All @@ -118,18 +158,7 @@ private static function getClassesFromFile($file)

++$i;
}
break;

// Retrieve classes and interfaces
case T_INTERFACE:
case T_CLASS:
$i+=2;
++$classCpt;
$classes[$tokens[$i][1]]['fileName'] = $file;
$classes[$tokens[$i][1]]['methods'] = array();

$currentClass = $tokens[$i][1];
break;
break;
}
}

Expand Down
1 change: 0 additions & 1 deletion lib/textUI.php
Expand Up @@ -78,7 +78,6 @@ public static function displayHelp()

public static function displayHeader()
{
echo `clear`;
echo PHP_EOL . self::COLOR_BOLD . 'Code Comparator ver1.0 by Bashar Al-Fallouji' . PHP_EOL . self::COLOR_NORMAL;
}
}
9 changes: 9 additions & 0 deletions sample/versionA/class1.php
@@ -0,0 +1,9 @@
<?php
namespace myNamespace\Subcategory;

class class1
{
public function function1($param1, StdClass $param2) {}
public function function2($param1, $param2) {}
public function function3() {}
}
6 changes: 6 additions & 0 deletions sample/versionA/class2.php
@@ -0,0 +1,6 @@
<?php

class class2
{
public function methodA(StdClass $clas) {}
}
9 changes: 9 additions & 0 deletions sample/versionB/class1.php
@@ -0,0 +1,9 @@
<?php

class class1
{
public function function1($param1, $param2) {}
public function function2($param1) {}
public function function3($param1, $param2) {}
public function function4() {}
}
3 changes: 3 additions & 0 deletions sample/versionB/class3.php
@@ -0,0 +1,3 @@
<?php

class class3 { }

0 comments on commit fd2e1b5

Please sign in to comment.