Skip to content

Commit

Permalink
Autoloader comment cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
gsherwood committed Apr 16, 2015
1 parent ed51b00 commit 2170ba5
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 35 deletions.
97 changes: 85 additions & 12 deletions autoload.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,49 @@
<?php
/**
* Autoloads files for PHP_CodeSniffer and tracks what has been loaded.
*
* Due to different namespaces being used for custom coding standards,
* the autoloader keeps track of what class is loaded after a file is included,
* even if the file is ultimately included by another autoloader (such as composer).
*
* This allows PHP_CodeSniffer to request the class name after loading a class
* when it only knows the filename, without having to parse the file to find it.
*
* @author Greg Sherwood <gsherwood@squiz.net>
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/

namespace PHP_CodeSniffer;

class Autoload {
class Autoload
{

/**
* A mapping of file names to class names.
*
* @var array<string, string>
*/
private static $loadedClasses = array();

/**
* A mapping of class names to file names.
*
* @var array<string, string>
*/
private static $loadedFiles = array();


/**
* Loads a class.
*
* This method only loads classes that exist in the PHP_CodeSniffer namespace.
* All other classes are ignored and loaded by subsequent autoloaders.
*
* @param string $class The name of the class to load.
*
* @return bool
*/
public static function load($class)
{
$ds = DIRECTORY_SEPARATOR;
Expand All @@ -25,8 +63,17 @@ public static function load($class)
}

return false;
}

}//end load()


/**
* Includes a file and tracks what class or interface was loaded as a result.
*
* @param string $path The path of the file to load.
*
* @return string The fully qualified name of the class in the loaded file.
*/
public static function loadFile($path)
{
if (isset(self::$loadedClasses[$path]) === true) {
Expand All @@ -43,29 +90,55 @@ public static function loadFile($path)
$className = array_pop(array_diff(get_declared_interfaces(), $interfaces));
}

self::$loadedClasses[$path] = $className;
self::$loadedClasses[$path] = $className;
self::$loadedFiles[$className] = $path;
return self::$loadedClasses[$path];
}

public static function getLoadedClassName($file)
}//end loadFile()


/**
* Gets the class name for the given file path.
*
* @param string $path The name of the file.
*
* @throws \Exception If the file path has not been loaded.
* @return string
*/
public static function getLoadedClassName($path)
{
if (isset(self::$loadedClasses[$file]) === false) {
throw new \Exception("Cannot get class name for $file; file has not been included");
if (isset(self::$loadedClasses[$path]) === false) {
throw new \Exception("Cannot get class name for $path; file has not been included");
}

return self::$loadedClasses[$file];
}
return self::$loadedClasses[$path];

}//end getLoadedClassName()


/**
* Gets the file path for the given class name.
*
* @param string $class The name of the class.
*
* @throws \Exception If the class name has not been loaded
* @return string
*/
public static function getLoadedFileName($class)
{
if (isset(self::$loadedFiles[$class]) === false) {
throw new \Exception("Cannot get file name for $class; class has not been included");
}

return self::$loadedFiles[$class];
}

}
}//end getLoadedFileName()


}//end class


spl_autoload_register(__NAMESPACE__.'\Autoload::load', true, true);
// Register the autoloader before any existing autoloaders to ensure
// it gets a chance to hear about every autoload request, and record
// the file and class name for it.
spl_autoload_register(__NAMESPACE__.'\Autoload::load', true, true);
14 changes: 3 additions & 11 deletions bin/phpcbf
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
#!/usr/bin/env php
<?php
/**
* PHP Code Beautifier and Fixer
* PHP Code Beautifier and Fixer fixes violations of a defined coding standard.
*
* PHP version 5
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
* @link http://pear.php.net/package/PHP_CodeSniffer
*/

require_once __DIR__.'/../autoload.php';

use PHP_CodeSniffer\Runner;

$runner = new Runner();
$runner = new PHP_CodeSniffer\Runner();
$runner->runPHPCBF();
15 changes: 3 additions & 12 deletions bin/phpcs
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
#!/usr/bin/env php
<?php
/**
* PHP_CodeSniffer tokenizes PHP code and detects violations of a
* defined set of coding standards.
* PHP_CodeSniffer detects violations of a defined coding standard.
*
* PHP version 5
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
* @link http://pear.php.net/package/PHP_CodeSniffer
*/

require_once __DIR__.'/../autoload.php';

use PHP_CodeSniffer\Runner;

$runner = new Runner();
$runner = new PHP_CodeSniffer\Runner();
$runner->runPHPCS();

0 comments on commit 2170ba5

Please sign in to comment.