Skip to content

Commit

Permalink
Moved class name to file name logic into the PSR-0 Scanner to be able…
Browse files Browse the repository at this point in the history
… to use it with a phpCS rule set
  • Loading branch information
lapistano committed Oct 29, 2011
1 parent 1f1d1ae commit 89ef3a5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 30 deletions.
32 changes: 3 additions & 29 deletions accepted/tests/Psr0_CompatibilityTest.php
Expand Up @@ -12,32 +12,6 @@

class psr0_CompatibilityTest extends \PHPUnit_Framework_TestCase
{

/**
* Translates a classname to a filename according to the rules of PSR-0.
*
* This method is a clone of the autoload() function accepted to be the reference
* implementation to autoload classes following the PSR-0 FIG standard.
*
* @param string $className
* @return string
* @link https://github.com/php-fig/fig-standards
*/
protected function translateClassToFilename($className)
{
$className = ltrim($className, '\\');
$fileName = '';
$namespace = '';
if ($lastNsPos = strripos($className, '\\')) {
$namespace = substr($className, 0, $lastNsPos);
$className = substr($className, $lastNsPos + 1);
$fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
}
$fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
return $fileName;
}


/**
* @dataProvider psr0CompatibilityDataprovider
*/
Expand All @@ -48,7 +22,7 @@ public function testPsr0Compatability($filename, $classname)

$this->assertEquals(
$filename,
$this->translateClassToFilename($classname),
Psr0_Scanner::translateClassToFilename($classname),
'The classname does not translate correctly into a PSR-0 compatible filename.'
);
}
Expand All @@ -67,8 +41,8 @@ public static function psr0CompatibilityDataprovider()
{
// preparations
$psr0 = new Psr0_Scanner(
Psr0_ScannerInclude,
Psr0_ScannerExclude
defined(Psr0_ScannerInclude) ? Psr0_ScannerInclude : '',
defined(Psr0_ScannerExclude) ? Psr0_ScannerExclude : ''
);

return $psr0->scan(Psr0_ScannerStartDir);
Expand Down
25 changes: 24 additions & 1 deletion accepted/tests/Psr0_Scanner.php
Expand Up @@ -16,7 +16,7 @@ class Psr0_Scanner
protected $registry = array();
protected $errors = array();

public function __construct($includes, $excludes)
public function __construct($includes = '' , $excludes = '')
{
$this->includes = $includes;
$this->excludes = $excludes;
Expand Down Expand Up @@ -62,6 +62,29 @@ public function getErrors()
return $this->errors;
}

/**
* Translates a classname to a filename according to the rules of PSR-0.
*
* This method is a clone of the autoload() function accepted to be the reference
* implementation to autoload classes following the PSR-0 FIG standard.
*
* @param string $className
* @return string
* @link https://github.com/php-fig/fig-standards
*/
public static function translateClassToFilename($className)
{
$className = ltrim($className, '\\');
$fileName = '';
$namespace = '';
if ($lastNsPos = strripos($className, '\\')) {
$namespace = substr($className, 0, $lastNsPos);
$className = substr($className, $lastNsPos + 1);
$fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
}
$fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
return $fileName;


/**
* Greps namespaces and classnames from the given file
Expand Down

0 comments on commit 89ef3a5

Please sign in to comment.