SonarQube code smells resolve #35
continuous-integration/drone/push Build is passing Details

LoginModification
Alexis 2 years ago
parent 9da2e2df9a
commit f5d9267200

@ -38,21 +38,22 @@
*/ */
class SplClassLoader class SplClassLoader
{ {
private $_fileExtension = '.php'; private $fileExtension = '.php';
private $_namespace; private $namespace;
private $_includePath; private $includePath;
private $_namespaceSeparator = '\\'; private $namespaceSeparator = '\\';
/** /**
* Creates a new <tt>SplClassLoader</tt> that loads classes of the * Creates a new <tt>SplClassLoader</tt> that loads classes of the
* specified namespace. * specified namespace.
* *
* @param string $ns The namespace to use. * @param string|null $ns The namespace to use.
* @param string|null $includePath
*/ */
public function __construct(string $ns = null, string $includePath = null) public function __construct(string $ns = null, string $includePath = null)
{ {
$this->_namespace = $ns; $this->namespace = $ns;
$this->_includePath = $includePath; $this->includePath = $includePath;
} }
/** /**
@ -60,19 +61,19 @@ class SplClassLoader
* *
* @param string $sep The separator to use. * @param string $sep The separator to use.
*/ */
public function setNamespaceSeparator(string $sep) public function setNamespaceSeparator(string $sep): void
{ {
$this->_namespaceSeparator = $sep; $this->namespaceSeparator = $sep;
} }
/** /**
* Gets the namespace seperator used by classes in the namespace of this class loader. * Gets the namespace seperator used by classes in the namespace of this class loader.
* *
* @return void * @return string
*/ */
public function getNamespaceSeparator() public function getNamespaceSeparator(): string
{ {
return $this->_namespaceSeparator; return $this->namespaceSeparator;
} }
/** /**
@ -80,9 +81,9 @@ class SplClassLoader
* *
* @param string $includePath * @param string $includePath
*/ */
public function setIncludePath(string $includePath) public function setIncludePath(string $includePath): void
{ {
$this->_includePath = $includePath; $this->includePath = $includePath;
} }
/** /**
@ -90,9 +91,9 @@ class SplClassLoader
* *
* @return string $includePath * @return string $includePath
*/ */
public function getIncludePath() public function getIncludePath(): string
{ {
return $this->_includePath; return $this->includePath;
} }
/** /**
@ -100,9 +101,9 @@ class SplClassLoader
* *
* @param string $fileExtension * @param string $fileExtension
*/ */
public function setFileExtension($fileExtension) public function setFileExtension(string $fileExtension): void
{ {
$this->_fileExtension = $fileExtension; $this->fileExtension = $fileExtension;
} }
/** /**
@ -110,15 +111,15 @@ class SplClassLoader
* *
* @return string $fileExtension * @return string $fileExtension
*/ */
public function getFileExtension() public function getFileExtension(): string
{ {
return $this->_fileExtension; return $this->fileExtension;
} }
/** /**
* Installs this class loader on the SPL autoload stack. * Installs this class loader on the SPL autoload stack.
*/ */
public function register() public function register(): void
{ {
spl_autoload_register(array($this, 'loadClass')); spl_autoload_register(array($this, 'loadClass'));
} }
@ -126,7 +127,7 @@ class SplClassLoader
/** /**
* Uninstalls this class loader from the SPL autoloader stack. * Uninstalls this class loader from the SPL autoloader stack.
*/ */
public function unregister() public function unregister(): void
{ {
spl_autoload_unregister(array($this, 'loadClass')); spl_autoload_unregister(array($this, 'loadClass'));
} }
@ -137,19 +138,19 @@ class SplClassLoader
* @param string $className The name of the class to load. * @param string $className The name of the class to load.
* @return void * @return void
*/ */
public function loadClass(string $className) public function loadClass(string $className): void
{ {
if (null === $this->_namespace || $this->_namespace.$this->_namespaceSeparator === substr($className, 0, strlen($this->_namespace.$this->_namespaceSeparator))) { if (null === $this->namespace || str_starts_with($className, $this->namespace . $this->namespaceSeparator)) {
$fileName = ''; $fileName = '';
$namespace = ''; $namespace = '';
if (false !== ($lastNsPos = strripos($className, $this->_namespaceSeparator))) { if (false !== ($lastNsPos = strripos($className, $this->namespaceSeparator))) {
$namespace = substr($className, 0, $lastNsPos); $namespace = substr($className, 0, $lastNsPos);
$className = substr($className, $lastNsPos + 1); $className = substr($className, $lastNsPos + 1);
$fileName = str_replace($this->_namespaceSeparator, DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR; $fileName = str_replace($this->namespaceSeparator, DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
} }
$fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . $this->_fileExtension; $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . $this->fileExtension;
require_once ($this->_includePath !== null ? $this->_includePath . DIRECTORY_SEPARATOR : '') . $fileName; require_once ($this->includePath !== null ? $this->includePath . DIRECTORY_SEPARATOR : '') . $fileName;
} }
} }
} }
Loading…
Cancel
Save