More code style fixes

This commit is contained in:
Timothy Warren 2018-01-17 15:32:41 -05:00
parent 22dc29f945
commit 578aad3e2e
37 changed files with 213 additions and 1700 deletions

View File

@ -6,7 +6,7 @@ test:7:
- php composer.phar install --ignore-platform-reqs - php composer.phar install --ignore-platform-reqs
image: php:7-alpine image: php:7-alpine
script: script:
- phpdbg -qrr -- vendor/bin/phpunit --coverage-text --colors=never - phpdbg -qrr -- vendor/bin/phpunit -c build --coverage-text --colors=never
test:7.1: test:7.1:
stage: test stage: test
@ -17,7 +17,7 @@ test:7.1:
- php composer.phar install --ignore-platform-reqs - php composer.phar install --ignore-platform-reqs
image: php:7.1-alpine image: php:7.1-alpine
script: script:
- phpdbg -qrr -- ./vendor/bin/phpunit --coverage-text --colors=never - phpdbg -qrr -- ./vendor/bin/phpunit -c build --coverage-text --colors=never
test:7.2: test:7.2:
stage: test stage: test
@ -28,4 +28,4 @@ test:7.2:
- php composer.phar install --ignore-platform-reqs - php composer.phar install --ignore-platform-reqs
image: php:7.2-alpine image: php:7.2-alpine
script: script:
- phpdbg -qrr -- ./vendor/bin/phpunit --coverage-text --colors=never - phpdbg -qrr -- ./vendor/bin/phpunit -c build --coverage-text --colors=never

View File

@ -204,7 +204,7 @@ class RoboFile extends \Robo\Tasks {
'tests' 'tests'
]; ];
$cmd_parts = ($report) ? $report_cmd_parts : $normal_cmd_parts; $cmd_parts = $report ? $report_cmd_parts : $normal_cmd_parts;
$this->_run($cmd_parts); $this->_run($cmd_parts);
} }
@ -224,7 +224,7 @@ class RoboFile extends \Robo\Tasks {
{ {
$this->lint(); $this->lint();
$this->taskPHPUnit() $this->taskPHPUnit()
->configFile('phpunit.xml') ->configFile('build/phpunit.xml')
->printed(true) ->printed(true)
->run(); ->run();
} }

View File

@ -90,7 +90,7 @@ class ByteOrderMarkSniff implements Sniff
$fileStartHex = bin2hex(substr($fileStartString, 0, $bomByteLength)); $fileStartHex = bin2hex(substr($fileStartString, 0, $bomByteLength));
if ($fileStartHex === $expectedBomHex) { if ($fileStartHex === $expectedBomHex) {
$error = "File contains a $bomName byte order mark (BOM)."; $error = "File contains a $bomName byte order mark (BOM).";
$phpcsFile->addError($error, $stackPtr); $phpcsFile->addError($error, $stackPtr, 'no byte order mark');
break; break;
} }
} }

View File

@ -70,15 +70,15 @@ class Utf8EncodingSniff implements Sniff
$file_content = file_get_contents($file_path); $file_content = file_get_contents($file_path);
if (false === mb_check_encoding($file_content, 'UTF-8')) { if (false === mb_check_encoding($file_content, 'UTF-8')) {
$error = 'File "' . $file_name . '" should be saved with Unicode (UTF-8) encoding.'; $error = 'File "' . $file_name . '" should be saved with Unicode (UTF-8) encoding.';
$phpcsFile->addError($error, 0); $phpcsFile->addError($error, 0, 'utf8OrDie');
} }
if ( ! self::_checkUtf8W3c($file_content)) { if ( ! self::_checkUtf8W3c($file_content)) {
$error = 'File "' . $file_name . '" should be saved with Unicode (UTF-8) encoding, but it did not successfully pass the W3C test.'; $error = 'File "' . $file_name . '" should be saved with Unicode (UTF-8) encoding, but it did not successfully pass the W3C test.';
$phpcsFile->addError($error, 0); $phpcsFile->addError($error, 0, 'utf8OrDie1');
} }
if ( ! self::_checkUtf8Rfc3629($file_content)) { if ( ! self::_checkUtf8Rfc3629($file_content)) {
$error = 'File "' . $file_name . '" should be saved with Unicode (UTF-8) encoding, but it did not meet RFC3629 requirements.'; $error = 'File "' . $file_name . '" should be saved with Unicode (UTF-8) encoding, but it did not meet RFC3629 requirements.';
$phpcsFile->addError($error, 0); $phpcsFile->addError($error, 0, 'utf8OrDie2');
} }
}//end process() }//end process()

View File

@ -67,11 +67,11 @@ class LogicalOperatorAndSniff implements Sniff
$error_message = 'Logical operator should be in upper case;' $error_message = 'Logical operator should be in upper case;'
. ' use "' . strtoupper($operator_string) . ' use "' . strtoupper($operator_string)
. '" instead of "' . $operator_string . '"'; . '" instead of "' . $operator_string . '"';
$phpcsFile->addError($error_message, $stackPtr); $phpcsFile->addError($error_message, $stackPtr, 'uppercase operator');
} }
$warning_message = 'The symbolic form "&&" is preferred over the literal form "AND"'; $warning_message = 'The symbolic form "&&" is preferred over the literal form "AND"';
$phpcsFile->addWarning($warning_message, $stackPtr); $phpcsFile->addWarning($warning_message, $stackPtr, '&& over AND');
}//end process() }//end process()

View File

@ -72,7 +72,7 @@ class StrictComparisonOperatorSniff implements Sniff
$error_message = '"==" and "!=" are prohibited; use "' $error_message = '"==" and "!=" are prohibited; use "'
. self::$_replacements[$operator_code] . '" instead of "' . self::$_replacements[$operator_code] . '" instead of "'
. $operator_string . '".'; . $operator_string . '".';
$phpcsFile->addError($error_message, $stackPtr); $phpcsFile->addError($error_message, $stackPtr, 'strict type comparison');
}//end process() }//end process()

View File

@ -1,84 +0,0 @@
<?php
/**
* CodeIgniter_Sniffs_Operators_UppercaseLogicalOperatorOrSniff.
*
* PHP version 5
*
* @category PHP
* @package PHP_CodeSniffer
* @author Thomas Ernest <thomas.ernest@baobaz.com>
* @copyright 2006 Thomas Ernest
* @license http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
/**
* CodeIgniter_Sniffs_Operators_UppercaseLogicalOperatorOrSniff.
*
* Ensures that the logical operator 'OR' is in upper cases and its symbolic equivalent.
*
* @category PHP
* @package PHP_CodeSniffer
* @author Thomas Ernest <thomas.ernest@baobaz.com>
* @copyright 2006 Thomas Ernest
* @license http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
namespace CodeIgniter\Sniffs\Operators;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File;
class UppercaseLogicalOperatorOrSniff implements Sniff
{
/**
* Returns an array of tokens this test wants to listen for: literal and symbolic operators or.
*
* @return array
*/
public function register()
{
return array(
T_BOOLEAN_OR,
T_LOGICAL_OR,
);
}//end register()
/**
* Processes this test, when one of its tokens is encountered.
*
* @param File $phpcsFile The current file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$operator_token = $tokens[$stackPtr];
$operator_string = $operator_token['content'];
$operator_code = $operator_token['code'];
if ($operator_code == T_BOOLEAN_OR) {
$error_message = 'Logical operator "' . $operator_string
. '" is prohibited; use "OR" instead';
$phpcsFile->addError($error_message, $stackPtr, 'UseOf||InsteadOfOR');
}
// it is literal, if it is not symbolic
else if ($operator_string !== strtoupper($operator_string)) {
$error_message = 'Logical operator should be in upper case;'
. ' use "' . strtoupper($operator_string)
. '" instead of "' . $operator_string . '"';
$phpcsFile->addError($error_message, $stackPtr, 'UseOfLowercaseOr');
}
}//end process()
}//end class
?>

View File

@ -18,6 +18,8 @@ namespace CodeIgniter\Sniffs\Strings;
use PHP_CodeSniffer\Sniffs\Sniff; use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File; use PHP_CodeSniffer\Files\File;
use Exception;
/** /**
* CodeIgniter_Sniffs_Strings_DoubleQuoteUsageSniff. * CodeIgniter_Sniffs_Strings_DoubleQuoteUsageSniff.
* *
@ -104,19 +106,19 @@ class VariableUsageSniff implements Sniff
$this->_parseVariable($strTokens, $strPtr); $this->_parseVariable($strTokens, $strPtr);
} catch (Exception $err) { } catch (Exception $err) {
$error = 'There is no variable, object nor array between curly braces. Please use the escape char for $ or {.'; $error = 'There is no variable, object nor array between curly braces. Please use the escape char for $ or {.';
$phpcsFile->addError($error, $stackPtr); $phpcsFile->addError($error, $stackPtr, 1987234);
} }
$variableFound = TRUE; $variableFound = TRUE;
if ('}' !== $strTokens[$strPtr]) { if ('}' !== $strTokens[$strPtr]) {
$error = 'There is no matching closing curly brace.'; $error = 'There is no matching closing curly brace.';
$phpcsFile->addError($error, $stackPtr); $phpcsFile->addError($error, $stackPtr, 987234);
} }
// don't move forward, since it will be done in the main loop // don't move forward, since it will be done in the main loop
// $strPtr++; // $strPtr++;
} else if (T_VARIABLE === $strToken[0]) { } else if (T_VARIABLE === $strToken[0]) {
$variableFound = TRUE; $variableFound = TRUE;
$error = "Variable {$strToken[1]} in double-quoted strings should be enclosed with curly braces. Please consider {{$strToken[1]}}"; $error = "Variable {$strToken[1]} in double-quoted strings should be enclosed with curly braces. Please consider {{$strToken[1]}}";
$phpcsFile->addError($error, $stackPtr); $phpcsFile->addError($error, $stackPtr, 29087234);
} }
} }
$strPtr++; $strPtr++;
@ -146,7 +148,7 @@ class VariableUsageSniff implements Sniff
if (is_array($strToken)) { if (is_array($strToken)) {
if (T_VARIABLE === $strToken[0]) { if (T_VARIABLE === $strToken[0]) {
$error = "Variables like {$strToken[1]} should be in double-quoted strings only."; $error = "Variables like {$strToken[1]} should be in double-quoted strings only.";
$phpcsFile->addError($error, $stackPtr); $phpcsFile->addError($error, $stackPtr, 12343);
} }
} }
$strPtr++; $strPtr++;
@ -460,5 +462,3 @@ class DoubleQuoteUsageSniff extends VariableUsageSniff
}//end _hasSpecificSequence() }//end _hasSpecificSequence()
}//end class }//end class
?>

View File

@ -77,7 +77,7 @@ class DisallowSpaceIndentSniff implements Sniff
if (strpos($tokens[$stackPtr]['content'], " ") !== false) { if (strpos($tokens[$stackPtr]['content'], " ") !== false) {
$error = 'Tabs must be used to indent lines; spaces are not allowed for code indentation'; $error = 'Tabs must be used to indent lines; spaces are not allowed for code indentation';
$phpcsFile->addError($error, $stackPtr); $phpcsFile->addError($error, $stackPtr, 'indent with tabs!');
} }
}//end process() }//end process()

View File

@ -72,7 +72,7 @@ class DisallowWitheSpaceAroundPhpTagsSniff implements Sniff
$isFirst = 0 === $stackPtr; $isFirst = 0 === $stackPtr;
if ( ! $isFirst) { if ( ! $isFirst) {
$error = 'Any char before the opening PHP tag is prohibited. Please remove newline or indentation before the opening PHP tag.'; $error = 'Any char before the opening PHP tag is prohibited. Please remove newline or indentation before the opening PHP tag.';
$phpcsFile->addError($error, $stackPtr); $phpcsFile->addError($error, $stackPtr, 'nothing before opening tag');
} }
} else { } else {
// if (T_CLOSE_TAG === $php_tag_code) // if (T_CLOSE_TAG === $php_tag_code)
@ -84,7 +84,7 @@ class DisallowWitheSpaceAroundPhpTagsSniff implements Sniff
$containsEndTagOnly = strlen($php_tag_string) > 2; $containsEndTagOnly = strlen($php_tag_string) > 2;
if ( ! $isLast || ! $containsEndTagOnly ) { if ( ! $isLast || ! $containsEndTagOnly ) {
$error = 'Any char after the closing PHP tag is prohibited. Please removes newline or spaces after the closing PHP tag.'; $error = 'Any char after the closing PHP tag is prohibited. Please removes newline or spaces after the closing PHP tag.';
$phpcsFile->addError($error, $stackPtr); $phpcsFile->addError($error, $stackPtr, 'nothing after closing tag');
} }
} }
}//end process() }//end process()

View File

@ -71,12 +71,10 @@ class ElseOnNewLineSniff implements Sniff
$previous_non_blank_token = $tokens[$previous_non_blank_token_ptr]; $previous_non_blank_token = $tokens[$previous_non_blank_token_ptr];
if ($previous_non_blank_token['line'] === $else_token['line']) { if ($previous_non_blank_token['line'] === $else_token['line']) {
$error = '"' . $else_token['content'] . '" should be on a new line.'; $error = '"' . $else_token['content'] . '" should be on a new line.';
$phpcsFile->addError($error, $stackPtr); $phpcsFile->addError($error, $stackPtr, 'else on new line');
} }
}//end process() }//end process()
}//end class }//end class
?>

View File

@ -65,7 +65,7 @@ class LogicalNotSpacingSniff implements Sniff
$next_token = $tokens[$stackPtr + 1]; $next_token = $tokens[$stackPtr + 1];
if (T_WHITESPACE !== $previous_token['code'] || T_WHITESPACE !== $next_token['code']) { if (T_WHITESPACE !== $previous_token['code'] || T_WHITESPACE !== $next_token['code']) {
$error = 'Logical operator ! should always be preceded and followed with a whitespace.'; $error = 'Logical operator ! should always be preceded and followed with a whitespace.';
$phpcsFile->addError($error, $stackPtr); $phpcsFile->addError($error, $stackPtr, 'Logical Not operator');
} }
}//end process() }//end process()

View File

@ -1,104 +0,0 @@
<?php
/**
* CodeIgniter_Sniffs_Files_AbstractClosingCommentSniff.
*
* PHP version 5
*
* @category PHP
* @package PHP_CodeSniffer
* @author Thomas Ernest <thomas.ernest@baobaz.com>
* @copyright 2006 Thomas Ernest
* @license http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
/**
* CodeIgniter_Sniffs_Files_AbstractClosingCommentSniff.
*
* Defines some methods used by
* CodeIgniter_Sniffs_Files_ClosingFileCommentSniff
* and CodeIgniter_Sniffs_Files_ClosingLocationCommentSniff.
*
* @category PHP
* @package PHP_CodeSniffer
* @author Thomas Ernest <thomas.ernest@baobaz.com>
* @copyright 2006 Thomas Ernest
* @license http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
namespace CodeIgniter\Sniffs\Files;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File;
class AbstractClosingCommentSniff implements Sniff
{
/**
* As an abstract class, this sniff is not associated to any token.
*/
public function register()
{
return array();
}
/**
* As an abstract class, this sniff is not dedicated to process a token.
*/
public function process(File $phpcsFile, $stackPtr)
{
$error = __CLASS__.'::'.__METHOD__.' is abstract. Please develop this method in a child class.';
throw new PHP_CodeSniffer_Exception($error);
}
/**
* Returns the comment without its delimiter(s) as well as leading
* and traling whitespaces.
*
* It removes the first #, the two first / (i.e. //) or the first /*
* and last \*\/. If a comment starts with /**, then the last * will remain
* as well as whitespaces between this star and the comment content.
*
* @param string $comment Comment containing either comment delimiter(s) and
* trailing or leading whitspaces to clean.
*
* @return string Comment without comment delimiter(s) and whitespaces.
*/
protected static function _getCommentContent ($comment)
{
if (self::_stringStartsWith($comment, '#')) {
$comment = substr($comment, 1);
} else if (self::_stringStartsWith($comment, '//')) {
$comment = substr($comment, 2);
} else if (self::_stringStartsWith($comment, '/*')) {
$comment = substr($comment, 2, strlen($comment) - 2 - 2);
}
$comment = trim($comment);
return $comment;
}//_getCommentContent()
/**
* Binary safe string comparison between $needle and
* the beginning of $haystack. Returns true if $haystack starts with
* $needle, false otherwise.
*
* @param string $haystack The string to search in.
* @param string $needle The string to search for.
*
* @return bool true if $haystack starts with $needle, false otherwise.
*/
protected static function _stringStartsWith ($haystack, $needle)
{
$startsWith = false;
if (strlen($needle) <= strlen($haystack)) {
$haystackBeginning = substr($haystack, 0, strlen($needle));
if (0 === strcmp($haystackBeginning, $needle)) {
$startsWith = true;
}
}
return $startsWith;
}//_stringStartsWith()
}//end class
?>

View File

@ -1,109 +0,0 @@
<?php
/**
* CodeIgniter_Sniffs_Files_ClosingFileCommentSniff.
*
* PHP version 5
*
* @category PHP
* @package PHP_CodeSniffer
* @author Thomas Ernest <thomas.ernest@baobaz.com>
* @copyright 2006 Thomas Ernest
* @license http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
/**
* CodeIgniter_Sniffs_Files_ClosingFileCommentSniff.
*
* Ensures that a comment containing the file name is available at the end of file.
* Only other comments and whitespaces are allowed to follow this specific comment.
*
* It may be all kind of comment like multi-line and inline C-style comments as
* well as PERL-style comments. Any number of white may separate comment delimiters
* from comment content. However, content has to be equal to template
* "End of file <file_name>". Comparison between content and template is
* case-sensitive.
*
* @category PHP
* @package PHP_CodeSniffer
* @author Thomas Ernest <thomas.ernest@baobaz.com>
* @copyright 2006 Thomas Ernest
* @license http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
namespace CodeIgniter\Sniffs\Files;
use PHP_CodeSniffer\Files\File;
class ClosingFileCommentSniff extends AbstractClosingCommentSniff
{
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register()
{
return array(
T_OPEN_TAG,
);
}//end register()
/**
* Processes this test, when one of its tokens is encountered.
*
* @param File $phpcsFile The current file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr)
{
// We are only interested if this is the first open tag.
if ($stackPtr !== 0) {
if ($phpcsFile->findPrevious(T_OPEN_TAG, ($stackPtr - 1)) !== false) {
return;
}
}
$fullFilename = $phpcsFile->getFilename();
$filename = basename($fullFilename);
$commentTemplate = "End of file $filename";
$tokens = $phpcsFile->getTokens();
$currentToken = count($tokens) - 1;
$hasClosingFileComment = false;
$isNotAWhitespaceOrAComment = false;
while ($currentToken >= 0
&& ! $isNotAWhitespaceOrAComment
&& ! $hasClosingFileComment
) {
$token = $tokens[$currentToken];
$tokenCode = $token['code'];
if (T_COMMENT === $tokenCode) {
$commentString = self::_getCommentContent($token['content']);
if (0 === strcmp($commentString, $commentTemplate)) {
$hasClosingFileComment = true;
}
} else if (T_WHITESPACE === $tokenCode) {
// Whitespaces are allowed between the closing file comment,
// other comments and end of file
} else {
$isNotAWhitespaceOrAComment = true;
}
$currentToken--;
}
if ( ! $hasClosingFileComment) {
$error = 'No comment block marks the end of file instead of the closing PHP tag. Please add a comment block containing only "' . $commentTemplate . '".';
$phpcsFile->addError($error, $currentToken);
}
}//end process()
}//end class
?>

View File

@ -1,182 +0,0 @@
<?php
/**
* CodeIgniter_Sniffs_Files_ClosingLocationCommentSniff.
*
* PHP version 5
*
* @category PHP
* @package PHP_CodeSniffer
* @author Thomas Ernest <thomas.ernest@baobaz.com>
* @copyright 2006 Thomas Ernest
* @license http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
/**
* CodeIgniter_Sniffs_Files_ClosingLocationCommentSniff.
*
* Ensures that a comment containing the file location exists at the end of file.
* Only other comments and whitespaces are allowed between this comment and
* the end of file.
*
* It may be all kind of comment like multi-line and inline C-style comments as
* well as PERL-style comments. Any number of white may separate comment delimiters
* from comment content. However, content has to be equal to template
* "Location: <file_path_relative_to_application_root>".
* Comparison between content and template is case-sensitive.
*
* There are several ways to configure the application root. In order of priority :
* - Configuration variable ci_application_root.
* - Rule property applicationRoot.
* - Default value '/application/'
*
* @category PHP
* @package PHP_CodeSniffer
* @author Thomas Ernest <thomas.ernest@baobaz.com>
* @copyright 2006 Thomas Ernest
* @license http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
namespace CodeIgniter\Sniffs\Files;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Util\Common;
class ClosingLocationCommentSniff extends AbstractClosingCommentSniff
{
public $applicationRoot = '/application/';
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register()
{
return array(
T_OPEN_TAG
);
}//end register()
/**
* Processes this test, when one of its tokens is encountered.
*
* @param File $phpcsFile The current file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr)
{
// We are only interested if this is the first open tag.
if ($stackPtr !== 0) {
if ($phpcsFile->findPrevious(T_OPEN_TAG, ($stackPtr - 1)) !== false) {
return;
}
}
$filePath = $phpcsFile->getFilename();
$tokens = $phpcsFile->getTokens();
// removes the application root from the beginning of the file path
$locationPath = self::_getLocationPath($filePath, $this->_getAppRoot());
// add an error, if application root doesn't exist in current file path
if (false === $locationPath) {
$error = 'Unable to find "' . $this->_getAppRoot() . '" in file path "' . $filePath . '". Please set your project\'s application root.';
$phpcsFile->addError($error, count($tokens) - 1);
return;
}
// generates the expected comment
$commentTemplate = "Location: $locationPath";
$currentToken = count($tokens) - 1;
$hasClosingLocationComment = false;
$isNotAWhitespaceOrAComment = false;
while ($currentToken >= 0
&& ! $isNotAWhitespaceOrAComment
&& ! $hasClosingLocationComment
) {
$token = $tokens[$currentToken];
$tokenCode = $token['code'];
if (T_COMMENT === $tokenCode) {
$commentString = self::_getCommentContent($token['content']);
if (0 === strcmp($commentString, $commentTemplate)) {
$hasClosingLocationComment = true;
}
} else if (T_WHITESPACE === $tokenCode) {
// Whitespaces are allowed between the closing file comment,
//other comments and end of file
} else {
$isNotAWhitespaceOrAComment = true;
}
$currentToken--;
}
if ( ! $hasClosingLocationComment) {
$error = 'No comment block marks the end of file instead of the closing PHP tag. Please add a comment block containing only "' . $commentTemplate . '".';
$phpcsFile->addError($error, $currentToken);
}
}//end process()
/**
* Returns the relative path from $appRoot to $filePath, or false if
* $appRoot cannot be found in $filePath, because $appRoot is not a parent
* of $filePath.
*
* @param string $filePath Full path to the file being proceed.
* @param string $appRoot Partial or full path to the CodeIgniter
* application root of the file being proceed. It must not contain the
* full path to the application root, but at least the name of the
* application root. Parent directory of the application root are allowed
* but not mandatory.
*
* @return string|bool The relative path from $appRoot to $filePath, or
* false if $appRoot cannot be found in $filePath.
*/
private static function _getLocationPath ($filePath, $appRoot)
{
// removes the path to application root
// from the beginning of the file path
$appRootAt = strpos($filePath, $appRoot);
if (false === $appRootAt) {
return false;
}
$localPath = substr($filePath, $appRootAt + strlen($appRoot));
// ensures the location path to be a relative path starting with "./".
if ( ! self::_stringStartsWith($localPath, './')) {
$localPath = './' . $localPath;
} else if ( ! self::_stringStartsWith($localPath, '.')
&& self::_stringStartsWith($localPath, '/')
) {
$localPath = '.' . $localPath;
}
return $localPath;
}//end _getLocationPath()
/**
* Returns the application root that should be used first.
*
* There are several ways to configure the application root.
* In order of priority :
* - Configuration variable ci_application_root.
* - Rule property applicationRoot.
* - Default value '/application/'
*
* @return string Path to your project application root.
*/
private function _getAppRoot()
{
$appRoot = Common::getConfigData('ci_application_root');
if (null === $appRoot) {
$appRoot = $this->applicationRoot;
}
return $appRoot;
}//end _getAppRoot()
}//end class
?>

View File

@ -1,142 +0,0 @@
<?php
/**
* CodeIgniter_Sniffs_NamingConventions_ConstructorNameSniff.
*
* PHP version 5
*
* @category PHP
* @package PHP_CodeSniffer
* @author Thomas Ernest <thomas.ernest@gmail.com>
* @copyright 2011 Thomas Ernest
* @license http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
namespace CodeIgniter\Sniffs\NamingConventions;
use PHP_CodeSniffer\Sniffs\AbstractScopeSniff;
use PHP_CodeSniffer\Files\File;
/**
* CodeIgniter_Sniffs_NamingConventions_ConstructorNameSniff.
*
* Favor PHP 4 constructor syntax, which uses "function ClassName()".
* Avoid PHP 5 constructor syntax, which uses "function __construct()".
*
* @todo Try to avoid overly long and verbose names.
*
* @category PHP
* @package PHP_CodeSniffer
* @author Thomas Ernest <thomas.ernest@gmail.com>
* @copyright 2010 Thomas Ernest
* @license http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class ConstructorNameSniff extends AbstractScopeSniff
{
public $php5Constructors = '1';
/**
* Constructs the test with the tokens it wishes to listen for.
*
* @return void
*/
public function __construct()
{
parent::__construct(array(T_CLASS, T_INTERFACE), array(T_FUNCTION), true);
}//end __construct()
/**
* Processes this test when one of its tokens is encountered.
*
* @param File $phpcsFile The current file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
* @param int $currScope A pointer to the start of the scope.
*
* @return void
*/
protected function processTokenWithinScope(
File $phpcsFile,
$stackPtr,
$currScope
) {
$methodName = $phpcsFile->getDeclarationName($stackPtr);
$className = $phpcsFile->getDeclarationName($currScope);
$isPhp4Constructor = strcasecmp($methodName, $className) === 0;
$isPhp5Constructor = strcasecmp($methodName, '__construct') === 0;
if ($this->php5Constructors != '0') {
if ($isPhp4Constructor) {
$error = "PHP4 style constructors are not allowed; use \"__construct\" instead";
$phpcsFile->addError($error, $stackPtr);
}
} else {
if ($isPhp5Constructor) {
$error = "PHP5 style constructors are not allowed; use \"$className\" instead";
$phpcsFile->addError($error, $stackPtr);
}
}
if ( ! $isPhp4Constructor && ! $isPhp5Constructor ) {
return;
}
$tokens = $phpcsFile->getTokens();
$parentClassName = $phpcsFile->findExtendedClassName($currScope);
$wrongConstructor = '';
// prepares the error message and wrong constructor
if ($this->php5Constructors != '0') {
$error = 'PHP4 style calls to parent constructors are not allowed.';
$error = "$error Please use \"parent::__construct\" instead.";
if (false !== $parentClassName) {
$wrongConstructor = $parentClassName;
}
// Else $wrongConstructor will be empty
// and the test expression will always be false.
// It doesn't check that no parent method should be called
// when no parent class is defined.
} else {
$error = 'PHP5 style calls to parent constructors are not allowed.';
if (false !== $parentClassName) {
$error = "$error Please use \"parent::$parentClassName\" instead.";
}
$wrongConstructor = '__construct';
}
// looks for the use of a wrong constructor.
$endFunctionIndex = $tokens[$stackPtr]['scope_closer'];
$doubleColonIndex = $phpcsFile->findNext(
array(T_DOUBLE_COLON),
$stackPtr,
$endFunctionIndex
);
while ($doubleColonIndex) {
if ($tokens[($doubleColonIndex + 1)]['code'] === T_STRING
&& $tokens[($doubleColonIndex + 1)]['content'] === $wrongConstructor
) {
$phpcsFile->addError($error, ($doubleColonIndex + 1));
}
$doubleColonIndex = $phpcsFile->findNext(
array(T_DOUBLE_COLON),
$doubleColonIndex + 1,
$endFunctionIndex
);
}
}//end processTokenWithinScope()
protected function processTokenOutsideScope(File $phpcsFile, $stackPtr)
{
// TODO: Implement processTokenOutsideScope() method.
}
}//end class
?>

View File

@ -1,84 +0,0 @@
<?php
/**
* CodeIgniter_Sniffs_NamingConventions_ValidClassNameSniff.
*
* PHP version 5
*
* @category PHP
* @package PHP_CodeSniffer
* @author Thomas Ernest <thomas.ernest@baoabz.com>
* @copyright 2010 Thomas Ernest
* @license http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
/**
* CodeIgniter_Sniffs_NamingConventions_ValidClassNameSniff.
*
* Ensures that class and interface names have their first letter uppercase
* and that words are separated with an underscore, and not CamelCased.
*
* @todo Try to avoid overly long and verbose names in using property rule and
* configuration variable to set limits. Have a look at
* CodeIgniter_Sniffs_NamingConventions_ValidMethodNameSniff.
*
* @category PHP
* @package PHP_CodeSniffer
* @author Thomas Ernest <thomas.ernest@baoabz.com>
* @copyright 2010 Thomas Ernest
* @license http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
namespace CodeIgniter\Sniffs\NamingConventions;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File;
class ValidClassNameSniff implements Sniff
{
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register()
{
return array(
T_CLASS,
T_INTERFACE,
);
}//end register()
/**
* Processes this test, when one of its tokens is encountered.
*
* @param File $phpcsFile The current file being processed.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr)
{
// get the class name
$className = trim($phpcsFile->getDeclarationName($stackPtr));
// compute the expected class name
// [^_] means "something different from _", but not "nothing or something different from _"
$lcClassNameChunk = preg_replace('/([^_])([A-Z])/', '${1}_${2}', $className);
$expectedClassName
= strtoupper($className[0]) . strtolower(substr($lcClassNameChunk,1));
// ensures that the current class name
// and the expected class name are identical
if (0 !== strcmp($className, $expectedClassName)) {
$error = 'Class names should always have their first letter uppercase. Multiple words should be separated with an underscore, and not CamelCased. Please consider ' . $expectedClassName . ' instead of ' . $className . '.';
$phpcsFile->addError($error, $stackPtr);
}
}//end process()
}//end class
?>

View File

@ -1,84 +0,0 @@
<?php
/**
* CodeIgniter_Sniffs_NamingConventions_ValidFileNameSniff.
*
* PHP version 5
*
* @category PHP
* @package PHP_CodeSniffer
* @author Thomas Ernest <thomas.ernest@baobaz.com>
* @copyright 2011 Thomas Ernest
* @license http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
/**
* CodeIgniter_Sniffs_NamingConventions_ValidFileNameSniff.
*
* Tests that the file name matchs the name of the class that it contains in lower case.
*
* @category PHP
* @package PHP_CodeSniffer
* @author Thomas Ernest <thomas.ernest@baobaz.com>
* @copyright 2011 Thomas Ernest
* @license http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
namespace CodeIgniter\Sniffs\NamingConventions;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File;
class ValidFileNameSniff implements Sniff
{
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register()
{
return array(
T_CLASS,
T_INTERFACE,
);
}//end register()
/**
* Processes this test, when one of its tokens is encountered.
*
* @param File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in the
* stack passed in $tokens.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
// computes the expected filename based on the name of the class or interface that it contains.
$decNamePtr = $phpcsFile->findNext(T_STRING, $stackPtr);
$decName = $tokens[$decNamePtr]['content'];
$expectedFileName = strtolower($decName);
// extracts filename without extension from its path.
$fullPath = $phpcsFile->getFilename();
$fileNameAndExt = basename($fullPath);
$fileName = substr($fileNameAndExt, 0, strrpos($fileNameAndExt, '.'));
if ($expectedFileName !== $fileName) {
$errorTemplate = 'Filename "%s" doesn\'t match the name of the %s that it contains "%s" in lower case. "%s" was expected.';
$errorMessage = sprintf(
$errorTemplate,
$fileName,
strtolower($tokens[$stackPtr]['content']), // class or interface
$decName,
$expectedFileName
);
$phpcsFile->addError($errorMessage, 0);
}
}//end process()
}//end class
?>

View File

@ -1,161 +0,0 @@
<?php
/**
* CodeIgniter_Sniffs_NamingConventions_ValidMethodNameSniff.
*
* PHP version 5
*
* @category PHP
* @package PHP_CodeSniffer
* @author Thomas Ernest <thomas.ernest@baoabz.com>
* @copyright 2010 Thomas Ernest
* @license http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
/**
* CodeIgniter_Sniffs_NamingConventions_ValidMethodNameSniff.
*
* Ensures that class methods and functions areentirely lowercased and that
* words are separated with an underscore, and not CamelCased.
* Ensures that private class methods are prefixed with an underscore and that
* all other methods are not prefixed with an underscored.
* Ensures that names longer than 50 chars are prohibited. Likewise names longer
* than 35 chars raise a warning.
*
* @todo Use a rule property or a configuration variable to allow users to set
* their own maximum lengths for function and method names. Have a look at
* CodeIgniter_Sniffs_Files_ClosingLocationCommentSniff and application root.
*
* @category PHP
* @package PHP_CodeSniffer
* @author Thomas Ernest <thomas.ernest@baoabz.com>
* @copyright 2010 Thomas Ernest
* @license http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
namespace CodeIgniter\Sniffs\NamingConventions;
use PHP_CodeSniffer\Sniffs\AbstactScopeSniff;
use PHP_CodeSniffer\Files\File;
class ValidMethodNameSniff extends AbstractScopeSniff
{
/**
* A list of all PHP magic methods.
*
* @var array
*/
protected static $magicMethods = array(
'construct',
'destruct',
'call',
'callStatic',
'get',
'set',
'isset',
'unset',
'sleep',
'wakeup',
'toString',
'set_state',
'clone',
);
/**
* Defines which token(s) in which scope(s) will be proceed.
*/
public function __construct()
{
parent::__construct(array(T_CLASS, T_INTERFACE), array(T_FUNCTION), true);
}//end __construct()
/**
* Processes the tokens within the scope.
*
* @param File $phpcsFile The file being processed.
* @param int $stackPtr The position where this token was
* found.
* @param int $currScope The position of the current scope.
*
* @return void
*/
protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScope)
{
$methodName = $phpcsFile->getDeclarationName($stackPtr);
if ($methodName === null) {
// Ignore closures.
return;
}
$className = $phpcsFile->getDeclarationName($currScope);
// Is this a magic method i.e. is prefixed with "__".
if (0 === strcmp(substr($methodName, 0, 2), '__')) {
$magicPart = substr($methodName, 2);
if (in_array($magicPart, self::$magicMethods) === false) {
$error = "Method name \"$className::$methodName\" is invalid; only PHP magic methods should be prefixed with a double underscore";
$phpcsFile->addError($error, $stackPtr);
}
return;
}
// PHP4 constructors are allowed to break our rules.
if ($methodName === $className) {
return;
}
// PHP4 destructors are allowed to break our rules.
if ($methodName === '_'.$className) {
return;
}
if (0 !== strcmp($methodName, strtolower($methodName))) {
$uscrdMethodName = preg_replace('/([A-Z])/', '_${1}', $methodName);
$expectedMethodName = strtolower($uscrdMethodName);
$error = "Class methods should be entirely lowercased. Please consider \"$expectedMethodName\" instead of \"$methodName\".";
$phpcsFile->addError($error, $stackPtr);
return;
}
$methodProps = $phpcsFile->getMethodProperties($stackPtr);
$scope = $methodProps['scope'];
$scopeSpecified = $methodProps['scope_specified'];
// If it's a private method, it must have an underscore on the front.
if ($scope === 'private' && $methodName{0} !== '_') {
$error = "Private method name \"$className::$methodName\" must be prefixed with an underscore";
$phpcsFile->addError($error, $stackPtr);
return;
}
// If it's not a private method, it must not have an underscore on the front.
if ($scope !== 'private' && $methodName{0} === '_') {
if (true === $scopeSpecified) {
$error = "Public method name \"$className::$methodName\" must not be prefixed with an underscore";
} else {
$error = ucfirst($scope)." method name \"$className::$methodName\" must not be prefixed with an underscore";
}
$phpcsFile->addError($error, $stackPtr);
return;
}
// If name is too verbose,
// then either an error or a warning is displayed.
$error_limit = 50;
$warning_limit = 35;
if (strlen($methodName) > $error_limit) {
$error = "Overly long and verbose names are prohibited. Please find a name shorter than $error_limit chars.";
$phpcsFile->addError($error, $stackPtr);
return;
} else if (strlen($methodName) > $warning_limit) {
$warning = "Try to avoid overly long and verbose names in finding a name shorter than $warning_limit chars.";
$phpcsFile->addWarning($warning, $stackPtr);
}
}//end processTokenWithinScope()
}//end class
?>

View File

@ -1,562 +0,0 @@
<?php
/**
* CodeIgniter_Sniffs_NamingConventions_ValidVariableNameSniff.
*
* PHP version 5
*
* @category PHP
* @package PHP_CodeSniffer
* @author Thomas Ernest <thomas.ernest@baoabz.com>
* @copyright 2010 Thomas Ernest
* @license http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
/**
* CodeIgniter_Sniffs_NamingConventions_ValidVariableNameSniff.
*
* Ensures that variable names contain only lowercase letters,
* use underscore separators.
* Ensures that class attribute names are prefixed with an underscore,
* only when they are private.
* Ensure that variable names are longer than 3 chars except those declared
* in for loops.
*
* @todo Try to avoid overly long and verbose names in using property rule and
* configuration variable to set limits. Have a look at
* CodeIgniter_Sniffs_NamingConventions_ValidMethodNameSniff.
* @todo Use a property rule or a configuration variable to allow users to set
* minimum variable name length. Have a look at
* CodeIgniter_Sniffs_Files_ClosingLocationCommentSniff and application root.
*
* @category PHP
* @package PHP_CodeSniffer
* @author Thomas Ernest <thomas.ernest@baoabz.com>
* @copyright 2010 Thomas Ernest
* @license http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
namespace CodeIgniter\Sniffs\NamingConventions;
use PHP_CodeSniffer\Sniffs\AbstractVariableSniff;
use PHP_CodeSniffer\Files\File;
class ValidVariableNameSniff extends AbstractVariableSniff
{
/**
* Processes class member variables.
*
* @param File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
*
* @return void
*/
protected function processMemberVar(File $phpcsFile, $stackPtr)
{
// get variable name and properties
$tokens = $phpcsFile->getTokens();
$varTk = $tokens[$stackPtr];
$varName = substr($varTk['content'], 1);
$varProps = $phpcsFile->getMemberProperties($stackPtr);
// check(s)
if ( ! $this->checkLowerCase($phpcsFile, $stackPtr, $varName) ) {
return;
}
if ( ! $this->checkVisibilityPrefix($phpcsFile, $stackPtr, $varName, $varProps)) {
return;
}
if ( ! $this->checkLength($phpcsFile, $stackPtr, $varName)) {
return;
}
}//end processMemberVar()
/**
* Processes normal variables.
*
* @param File $phpcsFile The file where this token was found.
* @param int $stackPtr The position where the token was found.
*
* @return void
*/
protected function processVariable(File $phpcsFile, $stackPtr)
{
// get variable name
$tokens = $phpcsFile->getTokens();
$varTk = $tokens[$stackPtr];
$varName = substr($varTk['content'], 1);
// skip the current object variable, i.e. $this
if (0 === strcmp($varName, 'this')) {
return;
}
// check(s)
if ( ! $this->checkLowerCase($phpcsFile, $stackPtr, $varName)) {
return;
}
if ( ! $this->checkLength($phpcsFile, $stackPtr, $varName)) {
return;
}
}//end processVariable()
/**
* Processes variables in double quoted strings.
*
* @param File $phpcsFile The file where this token was found.
* @param int $stackPtr The position where the token was found.
*
* @return void
*/
protected function processVariableInString(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$stringTk = $tokens[$stackPtr];
$stringString = $stringTk['content'];
$varAt = self::_getVariablePosition($stringString, 0);
while (false !== $varAt) {
// get variable name
$matches = array();
preg_match('/^\$\{?([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\}?/', substr($stringString, $varAt), $matches);
$varName = $matches[1];
// check(s)
if ( ! $this->checkLowerCase($phpcsFile, $stackPtr, $varName)) {
return;
}
if ( ! $this->checkLength($phpcsFile, $stackPtr, $varName)) {
return;
}
// prepare checking next variable
$varAt = self::_getVariablePosition($stringString, $varAt + 1);
}
}//end processVariableInString()
/**
* Checks that the variable name is all in lower case, else it add an error
* to $phpcsFile. Returns true if variable name is all in lower case, false
* otherwise.
*
* @param File $phpcsFile The current file being processed.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
* @param string $varName The name of the variable to
* procced without $, { nor }.
*
* @return bool true if variable name is all in lower case, false otherwise.
*/
protected function checkLowerCase(File $phpcsFile, $stackPtr, $varName)
{
$isInLowerCase = true;
if (0 !== strcmp($varName, strtolower($varName))) {
// get the expected variable name
$varNameWithUnderscores = preg_replace('/([A-Z])/', '_${1}', $varName);
$expectedVarName = strtolower(ltrim($varNameWithUnderscores, '_'));
// adapts the error message to the error case
if (strlen($varNameWithUnderscores) > strlen($varName)) {
$error = 'Variables should not use CamelCasing or start with a Capital.';
} else {
$error = 'Variables should be entirely lowercased.';
}
$error = $error . 'Please consider "' . $expectedVarName
. '" instead of "' . $varName . '".';
// adds the error and changes return value
$phpcsFile->addError($error, $stackPtr);
$isInLowerCase = false;
}
return $isInLowerCase;
}//end checkLowerCase()
/**
* Checks that an underscore is used at the beginning of a variable only if
* it is about a private variable. If it isn't a private variable, then it
* must not be prefixed with an underscore. Returns true if $varName is
* properly prefixed according to the variable visibility provided in
* $varProps, false otherwise.
*
* @param File $phpcsFile The current file being processed.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
* @param string $varName The name of the variable to
* procced without $, { nor }.
* @param array $varProps Member variable properties like
* its visibility.
*
* @return bool true if variable name is prefixed with an underscore only
* when it is about a private variable, false otherwise.
*/
protected function checkVisibilityPrefix(File $phpcsFile, $stackPtr, $varName, $varProps)
{
$isVisibilityPrefixRight = true;
$scope = $varProps['scope'];
// If it's a private variable, it must have an underscore on the front.
if ($scope === 'private' && $varName{0} !== '_') {
$error = "Private variable name \"$varName\" must be prefixed with an underscore";
$phpcsFile->addError($error, $stackPtr);
$isVisibilityPrefixRight = false;
} else if ($scope !== 'private' && $varName{0} === '_') {
// If it's not a private variable,
// then it must not start with an underscore.
if (isset ($scopeSpecified) && true === $scopeSpecified) {
$error = "Public variable name \"$varName\" must not be prefixed with an underscore";
} else {
$error = ucfirst($scope) . " variable name \"$varName\" must not be prefixed with an underscore";
}
$phpcsFile->addError($error, $stackPtr);
$isVisibilityPrefixRight = false;
}
return $isVisibilityPrefixRight;
}//end checkVisibilityPrefix()
/**
* Checks that variable name length is not too short. Returns true, if it
* meets minimum length requirement, false otherwise.
*
* A variable name is too short if it is shorter than the minimal
* length and it isn't in the list of allowed short names nor declared in a
* for loop (in which it would be nested).
* The minimal length is defined in the function. It is 3 chars now.
* The list of allowed short names is defined in the function.
* It is case-sensitive. It contains only 'ci' now.
*
* @param File $phpcsFile The current file being processed.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
* @param string $varName The name of the variable to
* procced without $, { nor }.
*
* @return bool false if variable name $varName is shorter than the minimal
* length and it isn't in the list of allowed short names nor declared in a
* for loop (in which it would be nested), otherwise true.
*/
protected function checkLength(File $phpcsFile, $stackPtr, $varName)
{
$minLength = 3;
$allowedShortName = array('ci');
$isLengthRight = true;
// cleans variable name
$varName = ltrim($varName, '_');
if (strlen($varName) <= $minLength) {
// skips adding an error, if it is a specific variable name
if (in_array($varName, $allowedShortName)) {
return $isLengthRight;
}
// skips adding an error, if the variable is in a for loop
if (false !== self::_isInForLoop($phpcsFile, $stackPtr, $varName)) {
return $isLengthRight;
}
// adds the error message finally
$error = 'Very short'
. (
$minLength > 0 ?
' (i.e. less than ' . ($minLength + 1) . ' chars)'
: ''
)
. ', non-word variables like "' . $varName
. '" should only be used as iterators in for() loops.';
$phpcsFile->addError($error, $stackPtr);
$isLengthRight = false;
}
return $isLengthRight;
}//end checkLength()
/**
* Returns the position of closest previous T_FOR, if token associated with
* $stackPtr in $phpcsFile is in a for loop, otherwise false.
*
* @param File $phpcsFile The current file being processed.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
* @param string $varName The name of the variable to
* procced without $, { nor }.
*
* @return int|bool Position of T_FOR if token associated with $stackPtr in
* $phpcsFile is in the head of a for loop, otherwise false.
*/
private static function _isInForLoop(File $phpcsFile, $stackPtr, $varName)
{
$keepLookingFromPtr = $stackPtr;
while (false !== $keepLookingFromPtr) {
// looks if it is in (head or body) of a for loop
$forPtr = self::_isInForLoopHead($phpcsFile, $keepLookingFromPtr);
if (false === $forPtr) {
$forPtr = self::_isInForLoopBody($phpcsFile, $keepLookingFromPtr);
}
// checks if it is declared in here and prepares next step
if (false !== $forPtr) {
if (false !== self::_isDeclaredInForLoop($phpcsFile, $forPtr, $varName)) {
return $forPtr;
}
$keepLookingFromPtr = $forPtr;
} else {
$keepLookingFromPtr = false;
}
}
return false;
}//end _isInForLoop()
/**
* Returns the position of closest previous T_FOR, if token associated with
* $stackPtr in $phpcsFile is in the head of a for loop, otherwise false.
* The head is the code placed between parenthesis next to the key word
* 'for' : for (<loop_head>) {<loop_body>}.
*
* @param File $phpcsFile The current file being processed.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
*
* @return int|bool Position of T_FOR if token associated with $stackPtr in
* $phpcsFile is in the head of a for loop, otherwise false.
*/
private static function _isInForLoopHead(File $phpcsFile, $stackPtr)
{
$isInForLoop = false;
$tokens = $phpcsFile->getTokens();
$currentTk = $tokens[$stackPtr];
if (array_key_exists('nested_parenthesis', $currentTk)) {
$nestedParenthesis = $currentTk['nested_parenthesis'];
foreach ( $nestedParenthesis as $openParPtr => $closeParPtr) {
$nonWhitspacePtr = $phpcsFile->findPrevious(
array(T_WHITESPACE),
$openParPtr - 1,
null,
true,
null,
true
);
if (false !== $nonWhitspacePtr) {
$isFor = T_FOR === $tokens[$nonWhitspacePtr]['code'];
if ($isFor) {
$isInForLoop = $nonWhitspacePtr;
break;
}
}
}
}
return $isInForLoop;
}//end _isInForLoopHead()
/**
* Returns the position of closest previous T_FOR, if token associated with
* $stackPtr in $phpcsFile is in the body of a for loop, otherwise false.
* The body are the instructions placed after parenthesis of a 'for'
* declaration, enclosed with curly brackets usually.
* 'for' : for (<loop_head>) {<loop_body>}.
*
* @param File $phpcsFile The current file being processed.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
*
* @return int|bool Position of T_FOR if token associated with $stackPtr in
* $phpcsFile is in the body of a for loop, otherwise false.
*/
private static function _isInForLoopBody(File $phpcsFile, $stackPtr)
{
$isInForLoop = false;
$tokens = $phpcsFile->getTokens();
// get englobing hierarchy
$parentPtrAndCode = $tokens[$stackPtr]['conditions'];
krsort($parentPtrAndCode);
// looks for a for loop having a body not enclosed with curly brackets,
// which involves that its body contains only one instruction.
if (is_array($parentPtrAndCode) && ! empty($parentPtrAndCode)) {
$parentCode = reset($parentPtrAndCode);
$parentPtr = key($parentPtrAndCode);
$openBracketPtr = $tokens[$parentPtr]['scope_opener'];
} else {
$parentCode = 0;
$parentPtr = 0;
$openBracketPtr = 0;
}
$openResearchScopePtr = $stackPtr;
// recursive search, since a for statement may englobe other inline
// control statement or may be near to function calls, etc...
while (false !== $openResearchScopePtr) {
$closeParPtr = $phpcsFile->findPrevious(
array(T_CLOSE_PARENTHESIS),
$openResearchScopePtr,
null,
false,
null,
true
);
// is there a closing parenthesis with a control statement before
// the previous instruction ?
if (false !== $closeParPtr) {
// is there no opening curly bracket specific to
// set of instructions, between the closing parenthesis
// and the current token ?
if ($openBracketPtr < $closeParPtr) {
// starts the search from the token before the closing
// parenthesis, if it isn't a for statement
$openResearchScopePtr = $closeParPtr - 1;
// is this parenthesis associated with a for statement ?
$closeParenthesisTk = $tokens[$closeParPtr];
if (array_key_exists('parenthesis_owner', $closeParenthesisTk)) {
$mayBeForPtr = $closeParenthesisTk['parenthesis_owner'];
$mayBeForTk = $tokens[$mayBeForPtr];
if (T_FOR === $mayBeForTk['code']) {
return $mayBeForPtr;
}
}
} else {
// if it is about a for loop, don't go further
// and detect it after one more loop execution, do it now
if (T_FOR === $parentCode) {
return $parentPtr;
}
// starts the search from the token before the one
// englobing the current statement
$openResearchScopePtr = $parentPtr - 1;
// re-initialize variables about the englobing structure
if (is_array($parentPtrAndCode)) {
$parentCode = next($parentPtrAndCode);
$parentPtr = key($parentPtrAndCode);
$openBracketPtr = $tokens[$parentPtr]['scope_opener'];
}
}
} else {
$openResearchScopePtr = false;
}
}
// looks for a for loop having a body enclosed with curly brackets
foreach ($parentPtrAndCode as $parentPtr => $parentCode) {
if (T_FOR === $parentCode) {
return $parentPtr;
}
}
return false;
}//end _isInForLoopBody()
/**
* Returns true if a variable declared in the head of the for loop pointed
* by $forPtr in file $phpcsFile has the name $varName.
*
* @param File $phpcsFile The current file being processed.
* @param int $forPtr The position of the 'for' token
* in the stack passed in $tokens.
* @param string $varName The name of the variable to
* procced without $, { nor }.
*
* @return int|bool true if a variable declared in the head of the for loop
* pointed by $forPtr in file $phpcsFile has the name $varName.
*/
private static function _isDeclaredInForLoop(File $phpcsFile, $forPtr, $varName)
{
$isDeclaredInFor = false;
$tokens = $phpcsFile->getTokens();
$forVarPtrs = self::_getVarDeclaredInFor($phpcsFile, $forPtr);
foreach ($forVarPtrs as $forVarPtr) {
$forVarTk = $tokens[$forVarPtr];
// get variable name
$matches = array();
preg_match('/^\$\{?([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\}?/', $forVarTk['content'], $matches);
$forVarName = $matches[1];
if (0 === strcmp($forVarName, $varName)) {
$isDeclaredInFor = $forVarPtr;
break;
}
}
return $isDeclaredInFor;
}//end _isDeclaredInForLoop()
/**
* Returns list of pointers to variables declared in for loop associated to
* $forPtr in file $phpcsFile.
*
* All pointers in the result list are pointing to token with code
* T_VARIABLE. An exception is raised, if $forPtr doesn't point a token with
* code T_FOR.
*
* @param File $phpcsFile The current file being processed.
* @param int $forPtr The position of the current token
* in the stack passed in $tokens.
*
* @return array List of pointers to variables declared in for loop $forPtr.
*/
private static function _getVarDeclaredInFor(File $phpcsFile, $forPtr)
{
$tokens = $phpcsFile->getTokens();
$forTk = $tokens[$forPtr];
if (T_FOR !== $forTk['code']) {
throw new PHP_CodeSniffer_Exception('$forPtr must be of type T_FOR');
}
$openParPtr = $forTk['parenthesis_opener'];
$openParenthesisTk = $tokens[$openParPtr];
$endOfDeclPtr = $phpcsFile->findNext(array(T_SEMICOLON), $openParPtr);
$forVarPtrs = array();
$varPtr = $phpcsFile->findNext(
array(T_VARIABLE),
$openParPtr + 1,
$endOfDeclPtr
);
while (false !== $varPtr) {
$forVarPtrs [] = $varPtr;
$varPtr = $phpcsFile->findNext(
array(T_VARIABLE),
$varPtr + 1,
$endOfDeclPtr
);
}
return $forVarPtrs;
}//end _getVarDeclaredInFor()
/**
* Returns the position of first occurrence of a PHP variable starting with
* $ in $haystack from $offset.
*
* @param string $haystack The string to search in.
* @param int $offset The optional offset parameter allows you to
* specify which character in haystack to start
* searching. The returned position is still
* relative to the beginning of haystack.
*
* @return mixed The position as an integer
* or the boolean false, if no variable is found.
*/
private static function _getVariablePosition($haystack, $offset = 0)
{
$var_starts_at = strpos($haystack, '$', $offset);
$is_a_var = false;
while (false !== $var_starts_at && ! $is_a_var) {
// makes sure that $ is used for a variable and not as a symbol,
// if $ is protected with the escape char, then it is a symbol.
if (0 !== strcmp($haystack[$var_starts_at - 1], '\\')) {
if (0 === strcmp($haystack[$var_starts_at + 1], '{')) {
// there is an opening brace in the right place
// so it looks for the closing brace in the right place
$hsChunk2 = substr($haystack, $var_starts_at + 2);
if (1 === preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*\}/', $hsChunk2)) {
$is_a_var = true;
}
} else {
$hsChunk1 = substr($haystack, $var_starts_at + 1);
if (1 === preg_match('/^[a-zA-Z_\x7f-\xff]/', $hsChunk1)) {
// $ is used for a variable and not as a symbol,
// since what follows $ matchs the definition of
// a variable label for PHP.
$is_a_var = true;
}
}
}
// update $var_starts_at for the next variable
// only if no variable was found, since it is returned otherwise.
if ( ! $is_a_var) {
$var_starts_at = strpos($haystack, '$', $var_starts_at + 1);
}
}
if ($is_a_var) {
return $var_starts_at;
} else {
return false;
}
}//end _getVariablePosition()
}//end class
?>

View File

@ -1,129 +1,134 @@
<?xml version="1.0" encoding="utf-8" ?> <?xml version="1.0" encoding="utf-8" ?>
<!-- This is a skeleton phpDox config file - Check http://phpDox.de for latest version and more info --> <!-- This is a skeleton phpDox config file - Check http://phpDox.de for latest version and more info -->
<phpdox xmlns="http://xml.phpdox.net/config" silent="false"> <phpdox xmlns="http://xml.phpdox.net/config" silent="false">
<!-- @silent: true | false to enable or disable visual output of progress -->
<!-- Additional bootstrap files to load for additional parsers, enrichers and/or engines --> <!-- A phpDox project to process, you can have multiple projects in one config file -->
<!-- Place as many require nodes as you feel like in this container --> <project name="Ion Framework" source="../src" workdir="${basedir}/phpdox/xml">
<!-- syntax: <require file="/path/to/file.php" /> --> <!-- @name - The name of the project -->
<bootstrap /> <!-- @source - The source directory of the application to process -->
<!-- @workdir - The directory to store the xml data files in -->
<!-- A phpDox project to process, you can have multiple projects in one config file --> <!-- A phpDox config file can define additional variables (properties) per project -->
<project name="Ion Framework" source="../src" workdir="phpdox/xml"> <!-- <property name="some.name" value="the.value" /> -->
<!-- @name - The name of the project -->
<!-- @source - The source directory of the application to process -->
<!-- @workdir - The directory to store the xml data files in -->
<!-- A phpDox config file can define additional variables (properties) per project --> <!-- Values can make use of previously defined properties -->
<!-- <property name="some.name" value="the.value" /> --> <!-- The following are defined by default:
<!-- Values can make use of previously defined properties --> ${basedir} Directory the loaded config file is in
<!-- The following are defined by default:
${basedir} Directory the loaded config file is in ${phpDox.home} Directory of the phpDox installation
${phpDox.file} The current config file
${phpDox.version} phpDox' version number
${phpDox.home} Directory of the phpDox installation ${phpDox.project.name} The value of project/@name if set, otherwise 'unnamed'
${phpDox.file} The current config file ${phpDox.project.source} The value of project/@source if set, otherwise '${basedir}/src'
${phpDox.version} phpDox' version number ${phpDox.project.workdir} The value of project/@workdir if set, otherwise '${basedir}/build/phpdox/xml'
${phpDox.project.name} The value of project/@name if set, otherwise 'unnamed' ${phpDox.php.version} The PHP Version of the interpreter in use
${phpDox.project.source} The value of project/@source if set, otherwise '${basedir}/src'
${phpDox.project.workdir} The value of project/@workdir if set, otherwise '${basedir}/build/phpdox/xml'
${phpDox.php.version} The PHP Version of the interpreter in use -->
--> <!-- Additional configuration for the collecting process (parsing of php code, generation of xml data) -->
<collector publiconly="false" backend="parser" encoding="auto">
<!-- @publiconly - Flag to disable/enable processing of non public methods and members -->
<!-- @backend - The collector backend to use, currently only shipping with 'parser' -->
<!-- @encoding - Charset encoding of source files (overwrite default 'auto' if detection fails) -->
<!-- Additional configuration for the collecting process (parsing of php code, generation of xml data) --> <!-- <include / exclude filter for filelist generator, mask must follow fnmatch() requirements -->
<collector publiconly="false" backend="parser" encoding="auto"> <!-- Multiple elements can be added to define more than one mask per filter -->
<!-- @publiconly - Flag to disable/enable processing of non public methods and members --> <include mask="*.php" />
<!-- @backend - The collector backend to use, currently only shipping with 'parser' --> <exclude mask="" />
<!-- @encoding - Charset encoding of source files (overwrite default 'auto' if detection fails) -->
<!-- <include / exclude filter for filelist generator, mask must follow fnmatch() requirements --> <!-- How to handle inheritance -->
<include mask="*.php" /> <inheritance resolve="true">
<exclude mask="" /> <!-- @resolve - Flag to enable/disable resolving of inheritance -->
<!-- How to handle inheritance --> <!-- You can define multiple (external) dependencies to be included -->
<inheritance resolve="true"> <!-- <dependency path="" -->
<!-- @resolve - Flag to enable/disable resolving of inheritance --> <!-- @path - path to a directory containing an index.xml for a dependency project -->
</inheritance>
</collector>
<!-- Configuration of generation process -->
<generator output="../docs">
<!-- @output - (Base-)Directory to store output data in -->
<!-- A generation process consists of one or more build tasks and of (optional) enrich sources -->
<enrich base="${basedir}/logs">
<!-- @base - (Base-)Directory of datafiles used for enrich process -->
<!--<source type="...">-->
<!-- @type - the handler for the enrichment -->
<!-- known types by default are: build, checkstyle, git, phpcs, phploc, phpunit, pmd -->
<!-- every enrichment source can have additional configuration nodes, most probably need a logfile -->
<!-- <file name="path/to/log.xml" /> -->
<!--</source> -->
<!-- add phploc output -->
<source type="phploc">
<file name="phploc.xml" />
</source>
<!-- git vcs information -->
<source type="git">
<git binary="/usr/bin/git" />
<history enabled="true" limit="15" cache="${phpDox.project.workdir}/gitlog.xml" />
</source>
<!-- PHP Code Sniffer findings -->
<!-- <source type="checkstyle">
<file name="phpcs.xml" />
</source> -->
<!-- PHPMessDetector -->
<!--
<source type="pmd">
<file name="pmd.xml" />
</source>
-->
<!-- PHPUnit Coverage XML -->
<!-- <source type="phpunit"> -->
<!-- <coverage path="" /> -->
<!-- @path - the directory where the xml code coverage report can be found -->
<!--</source>-->
<!--
<source type="phpunit">
<filter directory="${phpDox.project.source}" />
</source>
-->
<!-- PHPUnit Coverage XML -->
<source type="phpunit">
<coverage path="${basedir}/logs/coverage" />
<!-- <coverage path="" /> -->
<!-- @path - the directory where the xml code coverage report can be found -->
<!--<filter directory="${phpDox.project.source}" />-->
<!-- @directory - path of the phpunit config whitelist filter directory -->
<filter directory="${phpDox.project.source}" />
</source>
<!-- You can define multiple (external) dependencies to be included -->
<!-- <dependency path="" -->
<!-- @path - path to a directory containing an index.xml for a dependency project -->
</inheritance>
</collector> </enrich>
<!-- Configuration of generation process --> <!-- <build engine="..." enabled="true" output="..." /> -->
<generator output="../docs"> <!-- @engine - The name of the engine this build task uses, use ./phpDox - -engines to get a list of available engines -->
<!-- @output - (Base-)Directory to store output data in --> <!-- @enabled - Flag to enable/disable this engine, default: enabled=true -->
<!-- @output - (optional) Output directory; if relative (no / as first char) it is interpreted as relative to generator/@output -->
<!-- A generation process consists of one or more build tasks and of (optional) enrich sources --> <!-- An engine and thus build node can have additional configuration child nodes, please check the documentation for the engine to find out more -->
<enrich base="logs"> <!-- default engine "html" -->
<!-- @base - (Base-)Directory of datafiles used for enrich process --> <build engine="html" enabled="true">
<template dir="${phpDox.home}/templates/html" />
<file extension="html" />
</build>
<!--<source type="...">--> </generator>
<!-- @type - the handler for the enrichment --> </project>
<!-- known types by default are: build, checkstyle, git, phpcs, phploc, phpunit, pmd -->
<!-- every enrichment source can have additional configuration nodes, most probably need a logfile -->
<!-- <file name="path/to/log.xml" /> -->
<!--</source> -->
<!-- add phploc output -->
<source type="phploc">
<file name="phploc.xml" />
</source>
<!-- git vcs information -->
<source type="git">
<git binary="/usr/bin/git" />
<history enabled="true" limit="15" cache="${phpDox.project.workdir}/gitlog.xml" />
</source>
<!-- PHP Code Sniffer findings -->
<source type="checkstyle">
<file name="phpcs.xml" />
</source>
<!-- PHPMessDetector -->
<!--
<source type="pmd">
<file name="pmd.xml" />
</source>
-->
<!-- PHPUnit Coverage XML -->
<source type="phpunit">
<coverage path="coverage/clover.xml" />
<!-- <coverage path="clover.xml" />-->
<!-- @path - the directory where the xml code coverage report can be found -->
<!--<filter directory="${phpDox.project.source}" />-->
<!-- @directory - path of the phpunit config whitelist filter directory -->
</source>
<source type="phpunit">
<filter directory="${phpDox.project.source}" />
</source>
</enrich>
<!-- <build engine="..." enabled="true" output="..." /> -->
<!-- @engine - The name of the engine this build task uses, use ./phpDox - -engines to get a list of available engines -->
<!-- @enabled - Flag to enable/disable this engine, default: enabled=true -->
<!-- @output - (optional) Output directory; if relative (no / as first char) it is interpreted as relative to generator/@output -->
<!-- An engine and thus build node can have additional configuration child nodes, please check the documentation for the engine to find out more -->
<!-- default engine "html" -->
<build engine="html" enabled="true">
<template dir="${phpDox.home}/templates/html" />
<file extension="html" />
</build>
</generator>
</project>
</phpdox> </phpdox>

View File

@ -17,8 +17,8 @@
</testsuites> </testsuites>
<logging> <logging>
<log type="coverage-html" target="../coverage"/> <log type="coverage-html" target="../coverage"/>
<log type="coverage-clover" target="logs/clover.xml"/> <!-- <log type="coverage-clover" target="logs/clover.xml"/>
<log type="coverage-crap4j" target="logs/crap4j.xml"/> <log type="coverage-crap4j" target="logs/crap4j.xml"/> -->
<log type="coverage-xml" target="logs/coverage" /> <log type="coverage-xml" target="logs/coverage" />
<log type="junit" target="logs/junit.xml" logIncompleteSkipped="false"/> <log type="junit" target="logs/junit.xml" logIncompleteSkipped="false"/>
</logging> </logging>

View File

@ -11,6 +11,9 @@
} }
}, },
"autoload-dev": { "autoload-dev": {
"files": [
"tests/mocks.php"
],
"psr-4": { "psr-4": {
"Aviat\\Ion\\Tests\\": "tests/", "Aviat\\Ion\\Tests\\": "tests/",
"CodeIgniter\\": "build/CodeIgniter/" "CodeIgniter\\": "build/CodeIgniter/"
@ -26,23 +29,24 @@
"zendframework/zend-diactoros": "^1.4.0" "zendframework/zend-diactoros": "^1.4.0"
}, },
"require-dev": { "require-dev": {
"pdepend/pdepend": "^2.2", "aura/session": "^2.1.0",
"sebastian/phpcpd": "^3.0",
"theseer/phpdox": "^0.11.0",
"phploc/phploc": "^4.0",
"phpmd/phpmd": "^2.4",
"phpunit/phpunit": "^6.0",
"robmorgan/phinx": "^0.9.2",
"consolidation/robo": "^1.1.0", "consolidation/robo": "^1.1.0",
"monolog/monolog": "1.*", "monolog/monolog": "1.*",
"pdepend/pdepend": "^2.2",
"phploc/phploc": "^4.0",
"phpmd/phpmd": "^2.4",
"phpstan/phpstan": "^0.9.1",
"phpunit/phpunit": "^6.0",
"robmorgan/phinx": "^0.9.2",
"sebastian/phpcpd": "^3.0",
"squizlabs/php_codesniffer": "^3.0.0", "squizlabs/php_codesniffer": "^3.0.0",
"phpstan/phpstan": "^0.9.1" "theseer/phpdox": "^0.11.0"
}, },
"scripts": { "scripts": {
"coverage": "phpdbg -qrr -- vendor/bin/phpunit -c build", "coverage": "phpdbg -qrr -- vendor/bin/phpunit -c build",
"build": "robo build", "build": "robo build",
"docs": "cd build && ../vendor/bin/phpdox && cd ..", "docs": "cd build && ../vendor/bin/phpdox && cd ..",
"phpstan": "phpstan analyse -l 7 src ", "phpstan": "phpstan analyse -l 7 -c phpstan.neon src tests",
"test": "phpunit" "test": "phpunit"
}, },
"suggest": { "suggest": {

11
phpstan.neon Normal file
View File

@ -0,0 +1,11 @@
parameters:
autoload_files:
- %rootDir%/../../../tests/Di/ContainerTest.php
- %rootDir%/../../../tests/Di/ContainerAwareTest.php
ignoreErrors:
- '#Access to an undefined property Aviat\\Ion\\Tests\\[a-zA-Z0-9_]+Test::\$[a-zA-Z0-9_]+#'
- '#Call to an undefined method Aviat\\Ion\\Type\\ArrayType::[a-zA-Z0-9_]+\(\)#'
- '#Call to an undefined method Aura\\Html\\HelperLocator::[a-zA-Z0-9_]+\(\)#'
- '#Undefined variable: \$var#'
excludes_analyse:
- %rootDir%/tests/Di/*

View File

@ -44,5 +44,5 @@ interface ConfigInterface {
* @param string|array $key * @param string|array $key
* @return void * @return void
*/ */
public function delete($key); public function delete($key): void;
} }

View File

@ -4,11 +4,11 @@
* *
* Building blocks for web development * Building blocks for web development
* *
* PHP version 7.1 * PHP version 7
* *
* @package Ion * @package Ion
* @author Timothy J. Warren <tim@timshomepage.net> * @author Timothy J. Warren <tim@timshomepage.net>
* @copyright 2015 - 2018 Timothy J. Warren * @copyright 2015 - 2017 Timothy J. Warren
* @license http://www.opensource.org/licenses/mit-license.html MIT License * @license http://www.opensource.org/licenses/mit-license.html MIT License
* @version 2.2.0 * @version 2.2.0
* @link https://git.timshomepage.net/timw4mail/ion * @link https://git.timshomepage.net/timw4mail/ion
@ -68,9 +68,9 @@ class Container implements ContainerInterface {
*/ */
public function get($id) public function get($id)
{ {
if ( ! is_string($id)) if ( ! \is_string($id))
{ {
throw new ContainerException("Id must be a string"); throw new ContainerException('Id must be a string');
} }
if ($this->has($id)) if ($this->has($id))
@ -101,7 +101,7 @@ class Container implements ContainerInterface {
*/ */
public function getNew($id, array $args = NULL) public function getNew($id, array $args = NULL)
{ {
if ( ! is_string($id)) if ( ! \is_string($id))
{ {
throw new ContainerException('Id must be a string'); throw new ContainerException('Id must be a string');
} }
@ -109,8 +109,8 @@ class Container implements ContainerInterface {
if ($this->has($id)) if ($this->has($id))
{ {
// By default, call a factory with the Container // By default, call a factory with the Container
$args = is_array($args) ? $args : [$this]; $args = \is_array($args) ? $args : [$this];
$obj = call_user_func_array($this->container[$id], $args); $obj = \call_user_func_array($this->container[$id], $args);
// Check for container interface, and apply the container to the object // Check for container interface, and apply the container to the object
// if applicable // if applicable
@ -194,7 +194,7 @@ class Container implements ContainerInterface {
* @param string $id The logger to retrieve * @param string $id The logger to retrieve
* @return LoggerInterface|null * @return LoggerInterface|null
*/ */
public function getLogger(string $id = 'default') public function getLogger(string $id = 'default'): ?LoggerInterface
{ {
return $this->hasLogger($id) return $this->hasLogger($id)
? $this->loggers[$id] ? $this->loggers[$id]
@ -214,10 +214,10 @@ class Container implements ContainerInterface {
$trait_name = __NAMESPACE__ . '\\ContainerAware'; $trait_name = __NAMESPACE__ . '\\ContainerAware';
$interface_name = __NAMESPACE__ . '\\ContainerAwareInterface'; $interface_name = __NAMESPACE__ . '\\ContainerAwareInterface';
$uses_trait = in_array($trait_name, class_uses($obj), TRUE); $uses_trait = \in_array($trait_name, class_uses($obj), TRUE);
$implements_interface = in_array($interface_name, class_implements($obj), TRUE); $implements_interface = \in_array($interface_name, class_implements($obj), TRUE);
if ($uses_trait OR $implements_interface) if ($uses_trait || $implements_interface)
{ {
$obj->setContainer($this); $obj->setContainer($this);
} }

View File

@ -20,9 +20,6 @@ use ReflectionClass;
/** /**
* Class emulating an enumeration type * Class emulating an enumeration type
*
* @method bool isValid(mixed $key)
* @method array getConstList()
*/ */
abstract class Enum { abstract class Enum {
@ -35,9 +32,9 @@ abstract class Enum {
{ {
static $self; static $self;
if (is_null($self)) if ($self === NULL)
{ {
$class = \get_called_class(); $class = static::class;
$self = new $class; $self = new $class;
} }
@ -54,7 +51,7 @@ abstract class Enum {
public static function isValid($key): bool public static function isValid($key): bool
{ {
$values = array_values(static::getConstList()); $values = array_values(static::getConstList());
return in_array($key, $values); return \in_array($key, $values, TRUE);
} }
} }
// End of Enum.php // End of Enum.php

View File

@ -4,11 +4,11 @@
* *
* Building blocks for web development * Building blocks for web development
* *
* PHP version 7.1 * PHP version 7
* *
* @package Ion * @package Ion
* @author Timothy J. Warren <tim@timshomepage.net> * @author Timothy J. Warren <tim@timshomepage.net>
* @copyright 2015 - 2018 Timothy J. Warren * @copyright 2015 - 2017 Timothy J. Warren
* @license http://www.opensource.org/licenses/mit-license.html MIT License * @license http://www.opensource.org/licenses/mit-license.html MIT License
* @version 2.2.0 * @version 2.2.0
* @link https://git.timshomepage.net/timw4mail/ion * @link https://git.timshomepage.net/timw4mail/ion
@ -29,6 +29,7 @@ class Json {
* @param mixed $data * @param mixed $data
* @param int $options * @param int $options
* @param int $depth * @param int $depth
* @throws JsonException
* @return string * @return string
*/ */
public static function encode($data, $options = 0, $depth = 512): string public static function encode($data, $options = 0, $depth = 512): string
@ -45,6 +46,7 @@ class Json {
* @param mixed $data * @param mixed $data
* @param int $jsonOptions - Options to pass to json_encode * @param int $jsonOptions - Options to pass to json_encode
* @param int $fileOptions - Options to pass to file_get_contents * @param int $fileOptions - Options to pass to file_get_contents
* @throws JsonException
* @return int * @return int
*/ */
public static function encodeFile(string $filename, $data, int $jsonOptions = 0, int $fileOptions = 0): int public static function encodeFile(string $filename, $data, int $jsonOptions = 0, int $fileOptions = 0): int
@ -60,18 +62,18 @@ class Json {
* @param bool $assoc * @param bool $assoc
* @param int $depth * @param int $depth
* @param int $options * @param int $options
* @throws JsonException
* @return mixed * @return mixed
*/ */
public static function decode($json, bool $assoc = TRUE, int $depth = 512, int $options = 0) public static function decode($json, bool $assoc = TRUE, int $depth = 512, int $options = 0)
{ {
// Don't try to decode null // Don't try to decode null
if (empty($json)) if ($json === NULL)
{ {
return NULL; return NULL;
} }
// cast json to string so that streams from guzzle are correctly decoded $data = json_decode($json, $assoc, $depth, $options);
$data = json_decode((string) $json, $assoc, $depth, $options);
self::check_json_error(); self::check_json_error();
return $data; return $data;
@ -84,6 +86,7 @@ class Json {
* @param bool $assoc * @param bool $assoc
* @param int $depth * @param int $depth
* @param int $options * @param int $options
* @throws JsonException
* @return mixed * @return mixed
*/ */
public static function decodeFile(string $filename, bool $assoc = TRUE, int $depth = 512, int $options = 0) public static function decodeFile(string $filename, bool $assoc = TRUE, int $depth = 512, int $options = 0)
@ -96,6 +99,7 @@ class Json {
* Determines whether a string is valid json * Determines whether a string is valid json
* *
* @param string $string * @param string $string
* @throws \InvalidArgumentException
* @return boolean * @return boolean
*/ */
public static function isJson(string $string): bool public static function isJson(string $string): bool
@ -109,7 +113,7 @@ class Json {
* @throws JsonException * @throws JsonException
* @return void * @return void
*/ */
protected static function check_json_error() protected static function check_json_error(): void
{ {
$constant_map = [ $constant_map = [
JSON_ERROR_NONE => 'JSON_ERROR_NONE', JSON_ERROR_NONE => 'JSON_ERROR_NONE',
@ -126,7 +130,7 @@ class Json {
$error = json_last_error(); $error = json_last_error();
$message = json_last_error_msg(); $message = json_last_error_msg();
if (\JSON_ERROR_NONE !== $error) if (JSON_ERROR_NONE !== $error)
{ {
throw new JsonException("{$constant_map[$error]} - {$message}", $error); throw new JsonException("{$constant_map[$error]} - {$message}", $error);
} }

View File

@ -27,6 +27,7 @@ trait StringWrapper {
* Wrap the String in the Stringy class * Wrap the String in the Stringy class
* *
* @param string $str * @param string $str
* @throws \InvalidArgumentException
* @return StringType * @return StringType
*/ */
public function string($str): StringType public function string($str): StringType

View File

@ -41,15 +41,15 @@ class ArrayType {
*/ */
protected $nativeMethods = [ protected $nativeMethods = [
'chunk' => 'array_chunk', 'chunk' => 'array_chunk',
'pluck' => 'array_column',
'key_diff' => 'array_diff_key',
'diff' => 'array_diff', 'diff' => 'array_diff',
'filter' => 'array_filter', 'filter' => 'array_filter',
'flip' => 'array_flip', 'flip' => 'array_flip',
'intersect' => 'array_intersect', 'intersect' => 'array_intersect',
'key_diff' => 'array_diff_key',
'keys' => 'array_keys', 'keys' => 'array_keys',
'merge' => 'array_merge', 'merge' => 'array_merge',
'pad' => 'array_pad', 'pad' => 'array_pad',
'pluck' => 'array_column',
'product' => 'array_product', 'product' => 'array_product',
'random' => 'array_rand', 'random' => 'array_rand',
'reduce' => 'array_reduce', 'reduce' => 'array_reduce',
@ -97,7 +97,7 @@ class ArrayType {
{ {
$func = $this->nativeMethods[$method]; $func = $this->nativeMethods[$method];
// Set the current array as the first argument of the method // Set the current array as the first argument of the method
return call_user_func($func, $this->arr, ...$args); return $func($this->arr, ...$args);
} }
// Mapping for in-place methods // Mapping for in-place methods
@ -167,7 +167,7 @@ class ArrayType {
*/ */
public function has($value, bool $strict = TRUE): bool public function has($value, bool $strict = TRUE): bool
{ {
return in_array($value, $this->arr, $strict); return \in_array($value, $this->arr, $strict);
} }
/** /**
@ -179,7 +179,7 @@ class ArrayType {
public function &get($key = NULL) public function &get($key = NULL)
{ {
$value = NULL; $value = NULL;
if (is_null($key)) if ($key === NULL)
{ {
$value =& $this->arr; $value =& $this->arr;
} }
@ -222,7 +222,7 @@ class ArrayType {
foreach ($key as $level) foreach ($key as $level)
{ {
if (empty($pos) OR ! is_array($pos)) if (empty($pos) || ! is_array($pos))
{ {
// Directly returning a NULL value here will // Directly returning a NULL value here will
// result in a reference error. This isn't // result in a reference error. This isn't
@ -253,7 +253,7 @@ class ArrayType {
// create the levels if they don't exist // create the levels if they don't exist
foreach ($key as $level) foreach ($key as $level)
{ {
if ( ! is_array($pos) && empty($pos)) if ( ! \is_array($pos) && empty($pos))
{ {
$pos = []; $pos = [];
$pos[$level] = []; $pos[$level] = [];

View File

@ -90,6 +90,7 @@ abstract class View
* *
* @param string $name * @param string $name
* @param string|string[] $value * @param string|string[] $value
* @throws \InvalidArgumentException
* @return ViewInterface * @return ViewInterface
*/ */
public function addHeader(string $name, $value): ViewInterface public function addHeader(string $name, $value): ViewInterface
@ -102,6 +103,8 @@ abstract class View
* Set the output string * Set the output string
* *
* @param mixed $string * @param mixed $string
* @throws \InvalidArgumentException
* @throws \RuntimeException
* @return ViewInterface * @return ViewInterface
*/ */
public function setOutput($string): ViewInterface public function setOutput($string): ViewInterface
@ -115,6 +118,8 @@ abstract class View
* Append additional output. * Append additional output.
* *
* @param string $string * @param string $string
* @throws \InvalidArgumentException
* @throws \RuntimeException
* @return ViewInterface * @return ViewInterface
*/ */
public function appendOutput(string $string): ViewInterface public function appendOutput(string $string): ViewInterface

View File

@ -4,11 +4,11 @@
* *
* Building blocks for web development * Building blocks for web development
* *
* PHP version 7.1 * PHP version 7
* *
* @package Ion * @package Ion
* @author Timothy J. Warren <tim@timshomepage.net> * @author Timothy J. Warren <tim@timshomepage.net>
* @copyright 2015 - 2018 Timothy J. Warren * @copyright 2015 - 2017 Timothy J. Warren
* @license http://www.opensource.org/licenses/mit-license.html MIT License * @license http://www.opensource.org/licenses/mit-license.html MIT License
* @version 2.2.0 * @version 2.2.0
* @link https://git.timshomepage.net/timw4mail/ion * @link https://git.timshomepage.net/timw4mail/ion
@ -88,7 +88,6 @@ class HttpView extends BaseView {
* Send the appropriate response * Send the appropriate response
* *
* @throws DoubleRenderException * @throws DoubleRenderException
* @throws \InvalidArgumentException
* @return void * @return void
*/ */
protected function output(): void protected function output(): void

View File

@ -71,5 +71,5 @@ interface ViewInterface {
* @throws DoubleRenderException * @throws DoubleRenderException
* @return void * @return void
*/ */
public function send(); public function send(): void;
} }

View File

@ -17,7 +17,6 @@
namespace Aviat\Ion\Tests\Di; namespace Aviat\Ion\Tests\Di;
use Aviat\Ion\Di\{Container, ContainerAware, ContainerInterface}; use Aviat\Ion\Di\{Container, ContainerAware, ContainerInterface};
use Aviat\Ion\Di\Exception\ContainerException;
use Aviat\Ion\Tests\Ion_TestCase; use Aviat\Ion\Tests\Ion_TestCase;
class Aware { class Aware {
@ -32,6 +31,8 @@ class Aware {
class ContainerAwareTest extends Ion_TestCase { class ContainerAwareTest extends Ion_TestCase {
protected $aware;
public function setUp() public function setUp()
{ {
$this->container = new Container(); $this->container = new Container();

View File

@ -167,7 +167,7 @@ class ContainerTest extends Ion_TestCase {
return function() {}; return function() {};
}); });
$this->assertInstanceOf('Aviat\Ion\Di\Container', $container); $this->assertInstanceOf(Container::class, $container);
$this->assertInstanceOf('Aviat\Ion\Di\ContainerInterface', $container); $this->assertInstanceOf('Aviat\Ion\Di\ContainerInterface', $container);
// The factory returns a callable // The factory returns a callable

View File

@ -4,11 +4,11 @@
* *
* Building blocks for web development * Building blocks for web development
* *
* PHP version 7.1 * PHP version 7
* *
* @package Ion * @package Ion
* @author Timothy J. Warren <tim@timshomepage.net> * @author Timothy J. Warren <tim@timshomepage.net>
* @copyright 2015 - 2018 Timothy J. Warren * @copyright 2015 - 2017 Timothy J. Warren
* @license http://www.opensource.org/licenses/mit-license.html MIT License * @license http://www.opensource.org/licenses/mit-license.html MIT License
* @version 2.2.0 * @version 2.2.0
* @link https://git.timshomepage.net/timw4mail/ion * @link https://git.timshomepage.net/timw4mail/ion
@ -103,7 +103,7 @@ trait MockViewOutputTrait {
//$friend->output(); //$friend->output();
}*/ }*/
public function send() public function send(): void
{ {
if ($this->hasRendered) if ($this->hasRendered)
{ {
@ -115,7 +115,7 @@ trait MockViewOutputTrait {
} }
class TestView extends View { class TestView extends View {
public function send() public function send(): void
{ {
if ($this->hasRendered) if ($this->hasRendered)
{ {
@ -128,7 +128,7 @@ class TestView extends View {
} }
class TestHtmlView extends HtmlView { class TestHtmlView extends HtmlView {
protected function output() protected function output(): void
{ {
if ($this->hasRendered) if ($this->hasRendered)
{ {
@ -140,7 +140,7 @@ class TestHtmlView extends HtmlView {
} }
class TestHttpView extends HttpView { class TestHttpView extends HttpView {
protected function output() protected function output(): void
{ {
if ($this->hasRendered) if ($this->hasRendered)
{ {
@ -154,7 +154,7 @@ class TestHttpView extends HttpView {
class TestJsonView extends JsonView { class TestJsonView extends JsonView {
public function __destruct() {} public function __destruct() {}
protected function output() protected function output(): void
{ {
if ($this->hasRendered) if ($this->hasRendered)
{ {