First commit
This commit is contained in:
commit
86d649e9e8
20
.editorconfig
Normal file
20
.editorconfig
Normal file
@ -0,0 +1,20 @@
|
||||
# EditorConfig is awesome: http://EditorConfig.org
|
||||
|
||||
# top-most EditorConfig file
|
||||
root = true
|
||||
|
||||
# Unix-style newlines with a newline ending every file
|
||||
[*]
|
||||
end_of_line = lf
|
||||
insert_final_newline = false
|
||||
charset = utf-8
|
||||
indent_style = tab
|
||||
trim_trailing_whitespace = true
|
||||
|
||||
[*.{cpp,c,h,hpp,cxx}]
|
||||
insert_final_newline = true
|
||||
|
||||
# Yaml files
|
||||
[*.{yml,yaml}]
|
||||
indent_style = space
|
||||
indent_size = 4
|
29
.gitignore
vendored
Normal file
29
.gitignore
vendored
Normal file
@ -0,0 +1,29 @@
|
||||
.codelite
|
||||
.phing_targets
|
||||
.sonar/
|
||||
*.phprj
|
||||
*.workspace
|
||||
vendor
|
||||
**/cache/**
|
||||
**/logs/**
|
||||
**/coverage/**
|
||||
**/docs/**
|
||||
**/node_modules/**
|
||||
public/images/*
|
||||
composer.lock
|
||||
*.sqlite
|
||||
*.db
|
||||
*.sqlite3
|
||||
docs/*
|
||||
tests/test_data/sessions/*
|
||||
cache.properties
|
||||
build/**
|
||||
!build/*.txt
|
||||
!build/*.xml
|
||||
!build/*.php
|
||||
app/config/*.toml
|
||||
!app/config/*.toml.example
|
||||
phinx.yml
|
||||
.idea/
|
||||
Caddyfile
|
||||
build/humbuglog.txt
|
3
README.md
Normal file
3
README.md
Normal file
@ -0,0 +1,3 @@
|
||||
# Banker
|
||||
|
||||
A Caching library implementing the PSR-6 interface for several common cache backends
|
330
RoboFile.php
Normal file
330
RoboFile.php
Normal file
@ -0,0 +1,330 @@
|
||||
<?php
|
||||
if ( ! function_exists('glob_recursive'))
|
||||
{
|
||||
// Does not support flag GLOB_BRACE
|
||||
function glob_recursive($pattern, $flags = 0)
|
||||
{
|
||||
$files = glob($pattern, $flags);
|
||||
|
||||
foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir)
|
||||
{
|
||||
$files = array_merge($files, glob_recursive($dir.'/'.basename($pattern), $flags));
|
||||
}
|
||||
|
||||
return $files;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This is project's console commands configuration for Robo task runner.
|
||||
*
|
||||
* @see http://robo.li/
|
||||
*/
|
||||
class RoboFile extends \Robo\Tasks {
|
||||
|
||||
/**
|
||||
* Directories used by analysis tools
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $taskDirs = [
|
||||
'build/logs',
|
||||
'build/pdepend',
|
||||
'build/phpdox',
|
||||
];
|
||||
|
||||
/**
|
||||
* Directories to remove with the clean task
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $cleanDirs = [
|
||||
'coverage',
|
||||
'docs',
|
||||
'phpdoc',
|
||||
'build/logs',
|
||||
'build/phpdox',
|
||||
'build/pdepend'
|
||||
];
|
||||
|
||||
|
||||
/**
|
||||
* Do static analysis tasks
|
||||
*/
|
||||
public function analyze()
|
||||
{
|
||||
$this->prepare();
|
||||
$this->lint();
|
||||
$this->phploc(TRUE);
|
||||
$this->phpcs(TRUE);
|
||||
$this->dependencyReport();
|
||||
$this->phpcpdReport();
|
||||
}
|
||||
|
||||
/**
|
||||
* Run all tests, generate coverage, generate docs, generate code statistics
|
||||
*/
|
||||
public function build()
|
||||
{
|
||||
$this->analyze();
|
||||
$this->coverage();
|
||||
$this->docs();
|
||||
}
|
||||
|
||||
/**
|
||||
* Cleanup temporary files
|
||||
*/
|
||||
public function clean()
|
||||
{
|
||||
$cleanFiles = [
|
||||
'build/humbug.json',
|
||||
'build/humbug-log.txt',
|
||||
];
|
||||
array_map(function ($file) {
|
||||
@unlink($file);
|
||||
}, $cleanFiles);
|
||||
|
||||
// So the task doesn't complain,
|
||||
// make any 'missing' dirs to cleanup
|
||||
array_map(function ($dir) {
|
||||
if ( ! is_dir($dir))
|
||||
{
|
||||
`mkdir -p {$dir}`;
|
||||
}
|
||||
}, $this->cleanDirs);
|
||||
|
||||
$this->_cleanDir($this->cleanDirs);
|
||||
$this->_deleteDir($this->cleanDirs);
|
||||
}
|
||||
|
||||
/**
|
||||
* Run unit tests and generate coverage reports
|
||||
*/
|
||||
public function coverage()
|
||||
{
|
||||
$this->taskPhpUnit()
|
||||
->configFile('build/phpunit.xml')
|
||||
->printed(true)
|
||||
->run();
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate documentation with phpdox
|
||||
*/
|
||||
public function docs()
|
||||
{
|
||||
$cmd_parts = [
|
||||
'cd build',
|
||||
'../vendor/bin/phpdox',
|
||||
'cd ..'
|
||||
];
|
||||
$this->_run($cmd_parts, ' && ');
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that source files are valid
|
||||
*/
|
||||
public function lint()
|
||||
{
|
||||
$files = $this->getAllSourceFiles();
|
||||
|
||||
$chunks = array_chunk($files, 6);
|
||||
|
||||
foreach($chunks as $chunk)
|
||||
{
|
||||
$this->parallelLint($chunk);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Run mutation tests with humbug
|
||||
*
|
||||
* @param bool $stats - if true, generates stats rather than running mutation tests
|
||||
*/
|
||||
public function mutate($stats = FALSE)
|
||||
{
|
||||
$test_parts = [
|
||||
'vendor/bin/humbug'
|
||||
];
|
||||
|
||||
$stat_parts = [
|
||||
'vendor/bin/humbug',
|
||||
'--skip-killed=yes',
|
||||
'-v',
|
||||
'./build/humbug.json'
|
||||
];
|
||||
|
||||
$cmd_parts = ($stats) ? $stat_parts : $test_parts;
|
||||
$this->_run($cmd_parts);
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the phpcs tool
|
||||
*
|
||||
* @param bool $report - if true, generates reports instead of direct output
|
||||
*/
|
||||
public function phpcs($report = FALSE)
|
||||
{
|
||||
$report_cmd_parts = [
|
||||
'vendor/bin/phpcs',
|
||||
'--standard=./build/phpcs.xml',
|
||||
'--report-checkstyle=./build/logs/phpcs.xml',
|
||||
];
|
||||
|
||||
$normal_cmd_parts = [
|
||||
'vendor/bin/phpcs',
|
||||
'--standard=./build/phpcs.xml',
|
||||
];
|
||||
|
||||
$cmd_parts = ($report) ? $report_cmd_parts : $normal_cmd_parts;
|
||||
|
||||
$this->_run($cmd_parts);
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the phploc tool
|
||||
*
|
||||
* @param bool $report - if true, generates reports instead of direct output
|
||||
*/
|
||||
public function phploc($report = FALSE)
|
||||
{
|
||||
// Command for generating reports
|
||||
$report_cmd_parts = [
|
||||
'vendor/bin/phploc',
|
||||
'--count-tests',
|
||||
'--log-csv=build/logs/phploc.csv',
|
||||
'--log-xml=build/logs/phploc.xml',
|
||||
'src',
|
||||
'tests'
|
||||
];
|
||||
|
||||
// Command for generating direct output
|
||||
$normal_cmd_parts = [
|
||||
'vendor/bin/phploc',
|
||||
'--count-tests',
|
||||
'src',
|
||||
'tests'
|
||||
];
|
||||
|
||||
$cmd_parts = ($report) ? $report_cmd_parts : $normal_cmd_parts;
|
||||
|
||||
$this->_run($cmd_parts);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create temporary directories
|
||||
*/
|
||||
public function prepare()
|
||||
{
|
||||
array_map([$this, '_mkdir'], $this->taskDirs);
|
||||
}
|
||||
|
||||
/**
|
||||
* Lint php files and run unit tests
|
||||
*/
|
||||
public function test()
|
||||
{
|
||||
$this->lint();
|
||||
$this->taskPHPUnit()
|
||||
->configFile('phpunit.xml')
|
||||
->printed(true)
|
||||
->run();
|
||||
}
|
||||
|
||||
/**
|
||||
* Watches for file updates, and automatically runs appropriate actions
|
||||
*/
|
||||
public function watch()
|
||||
{
|
||||
$this->taskWatch()
|
||||
->monitor('composer.json', function() {
|
||||
$this->taskComposerUpdate()->run();
|
||||
})
|
||||
->monitor('src', function () {
|
||||
$this->taskExec('test')->run();
|
||||
})
|
||||
->monitor('tests', function () {
|
||||
$this->taskExec('test')->run();
|
||||
})
|
||||
->run();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create pdepend reports
|
||||
*/
|
||||
protected function dependencyReport()
|
||||
{
|
||||
$cmd_parts = [
|
||||
'vendor/bin/pdepend',
|
||||
'--jdepend-xml=build/logs/jdepend.xml',
|
||||
'--jdepend-chart=build/pdepend/dependencies.svg',
|
||||
'--overview-pyramid=build/pdepend/overview-pyramid.svg',
|
||||
'src'
|
||||
];
|
||||
$this->_run($cmd_parts);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the total list of source files, including tests
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getAllSourceFiles()
|
||||
{
|
||||
$files = array_merge(
|
||||
glob_recursive('build/*.php'),
|
||||
glob_recursive('src/*.php'),
|
||||
glob_recursive('tests/*.php'),
|
||||
glob('*.php')
|
||||
);
|
||||
|
||||
sort($files);
|
||||
|
||||
return $files;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run php's linter in one parallel task for the passed chunk
|
||||
*
|
||||
* @param array $chunk
|
||||
*/
|
||||
protected function parallelLint(array $chunk)
|
||||
{
|
||||
$task = $this->taskParallelExec()
|
||||
->timeout(5)
|
||||
->printed(FALSE);
|
||||
|
||||
foreach($chunk as $file)
|
||||
{
|
||||
$task = $task->process("php -l {$file}");
|
||||
}
|
||||
|
||||
$task->run();
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate copy paste detector report
|
||||
*/
|
||||
protected function phpcpdReport()
|
||||
{
|
||||
$cmd_parts = [
|
||||
'vendor/bin/phpcpd',
|
||||
'--log-pmd build/logs/pmd-cpd.xml',
|
||||
'src'
|
||||
];
|
||||
$this->_run($cmd_parts);
|
||||
}
|
||||
|
||||
/**
|
||||
* Shortcut for joining an array of command arguments
|
||||
* and then running it
|
||||
*
|
||||
* @param array $cmd_parts - command arguments
|
||||
* @param string $join_on - what to join the command arguments with
|
||||
*/
|
||||
protected function _run(array $cmd_parts, $join_on = ' ')
|
||||
{
|
||||
$this->taskExec(implode($join_on, $cmd_parts))->run();
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
<documentation title="Closing comments instead of PHP closing tag">
|
||||
<standard>
|
||||
<![CDATA[
|
||||
The PHP closing tag on a PHP document ?> is optional to the PHP parser. However, if used, any whitespace following the closing tag, whether introduced by the developer, user, or an FTP application, can cause unwanted output, PHP errors, or if the latter are suppressed, blank pages. For this reason, all PHP files should OMIT the closing PHP tag, and instead use a comment block to mark the end of file and it's location relative to the application root. This allows you to still identify a file as being complete and not truncated.
|
||||
]]>
|
||||
</standard>
|
||||
<code_comparison>
|
||||
<code title="Examples of valid closing comments">
|
||||
<![CDATA[
|
||||
<?php
|
||||
|
||||
echo "Here's my code!";
|
||||
|
||||
/* End of file myfile.php */
|
||||
/* Location: ./system/modules/mymodule/myfile.php */
|
||||
]]>
|
||||
</code>
|
||||
<code title="Examples of invalid closing comments">
|
||||
<![CDATA[
|
||||
<?php
|
||||
|
||||
echo "Here's my code!";
|
||||
|
||||
?>
|
||||
]]>
|
||||
</code>
|
||||
</code_comparison>
|
||||
</documentation>
|
@ -0,0 +1,7 @@
|
||||
<documentation title="Unicode (UTF-8) encoding without BOM">
|
||||
<standard>
|
||||
<![CDATA[
|
||||
Files should be saved with Unicode (UTF-8) encoding. The BOM should not be used. Unlike UTF-16 and UTF-32, there's no byte order to indicate in a UTF-8 encoded file, and the BOM can have a negative side effect in PHP of sending output, preventing the application from being able to set its own headers. Unix line endings should be used (LF).
|
||||
]]>
|
||||
</standard>
|
||||
</documentation>
|
@ -0,0 +1,31 @@
|
||||
<documentation title="Constructor Names">
|
||||
<standard>
|
||||
<![CDATA[
|
||||
Class names should always start with an uppercase letter. Multiple words should be separated with an underscore, and not CamelCased. All other class methods should be entirely lowercased and named to clearly indicate their function, preferably including a verb. Try to avoid overly long and verbose names.
|
||||
]]>
|
||||
</standard>
|
||||
<code_comparison>
|
||||
<code title="Examples of valid constructor name">
|
||||
<![CDATA[
|
||||
class Super_class
|
||||
{
|
||||
function Super_class()
|
||||
{
|
||||
echo 'Some code here !';
|
||||
}
|
||||
}
|
||||
]]>
|
||||
</code>
|
||||
<code title="Examples of invalid constructor name">
|
||||
<![CDATA[
|
||||
class Super_class
|
||||
{
|
||||
function __constructor()
|
||||
{
|
||||
echo 'Some code here !';
|
||||
}
|
||||
}
|
||||
]]>
|
||||
</code>
|
||||
</code_comparison>
|
||||
</documentation>
|
@ -0,0 +1,21 @@
|
||||
<documentation title="Class names">
|
||||
<standard>
|
||||
<![CDATA[
|
||||
Class names should always start with an uppercase letter. Multiple words should be separated with an underscore, and not CamelCased. All other class methods should be entirely lowercased and named to clearly indicate their function, preferably including a verb. Try to avoid overly long and verbose names.
|
||||
]]>
|
||||
</standard>
|
||||
<code_comparison>
|
||||
<code title="Examples of valid class names">
|
||||
<![CDATA[
|
||||
class Super_class
|
||||
]]>
|
||||
</code>
|
||||
<code title="Examples of invalid class names">
|
||||
<![CDATA[
|
||||
class SuperClass // words not separated with underscores and words next to the first one start with an upper case
|
||||
class superclass // words not separated with underscores
|
||||
class Super_Class // words next to the first one start with an upper case
|
||||
]]>
|
||||
</code>
|
||||
</code_comparison>
|
||||
</documentation>
|
@ -0,0 +1,21 @@
|
||||
<documentation title="File names">
|
||||
<standard>
|
||||
<![CDATA[
|
||||
To be able to find which class is contained in a file, file names should be case-insensitively equal to class names. Some operating systems and tools are case-insensitive, though other are. So, file names should be in lower case to avoid any trouble.
|
||||
]]>
|
||||
</standard>
|
||||
<code_comparison>
|
||||
<code title="Examples of valid file names">
|
||||
<![CDATA[
|
||||
super_class.php
|
||||
]]>
|
||||
</code>
|
||||
<code title="Examples of invalid file names">
|
||||
<![CDATA[
|
||||
superclass.php // words not separated with underscores
|
||||
SuperClass.php // not in lower case and words not separated with underscores
|
||||
Super_class.php // not in lower case
|
||||
]]>
|
||||
</code>
|
||||
</code_comparison>
|
||||
</documentation>
|
@ -0,0 +1,27 @@
|
||||
<documentation title="Function and Method Names">
|
||||
<standard>
|
||||
<![CDATA[
|
||||
Class names should always start with an uppercase letter. Multiple words should be separated with an underscore, and not CamelCased. All other class methods should be entirely lowercased and named to clearly indicate their function, preferably including a verb. Try to avoid overly long and verbose names.
|
||||
Methods and variables that are only accessed internally by your class, such as utility and helper functions that your public methods use for code abstraction, should be prefixed with an underscore.
|
||||
]]>
|
||||
</standard>
|
||||
<code_comparison>
|
||||
<code title="Examples of valid method names">
|
||||
<![CDATA[
|
||||
function get_file_properties() // descriptive, underscore separator, and all lowercase letters
|
||||
private function _get_file_properties()
|
||||
]]>
|
||||
</code>
|
||||
<code title="Examples of invalid method names">
|
||||
<![CDATA[
|
||||
function fileproperties() // not descriptive and needs underscore separator
|
||||
function fileProperties() // not descriptive and uses CamelCase
|
||||
function getfileproperties() // Better! But still missing underscore separator
|
||||
function getFileProperties() // uses CamelCase
|
||||
function get_the_file_properties_from_the_file() // wordy
|
||||
private function get_the_file_properties() // not prefixed with an underscor, though private
|
||||
function _get_the_file_properties() // prefixed with an underscor, though public
|
||||
]]>
|
||||
</code>
|
||||
</code_comparison>
|
||||
</documentation>
|
@ -0,0 +1,31 @@
|
||||
<documentation title="Variable names">
|
||||
<standard>
|
||||
<![CDATA[
|
||||
Namely, variables should contain only lowercase letters, use underscore separators, and be reasonably named to indicate their purpose and contents. Very short, non-word variables should only be used as iterators in for() loops.
|
||||
Methods and variables that are only accessed internally by your class, such as utility and helper functions that your public methods use for code abstraction, should be prefixed with an underscore.
|
||||
]]>
|
||||
</standard>
|
||||
<code_comparison>
|
||||
<code title="Examples of valid variable names">
|
||||
<![CDATA[
|
||||
for ($j = 0; $j < 10; $j++)
|
||||
$str
|
||||
$buffer
|
||||
$group_id
|
||||
$last_city
|
||||
private $_internal_data;
|
||||
]]>
|
||||
</code>
|
||||
<code title="Examples of invalid variable names">
|
||||
<![CDATA[
|
||||
$j = 'foo'; // single letter variables should only be used in for() loops
|
||||
$Str // contains uppercase letters
|
||||
$bufferedText // uses CamelCasing, and could be shortened without losing semantic meaning
|
||||
$groupid // multiple words, needs underscore separator
|
||||
$name_of_last_city_used // too long
|
||||
private $internal_data; // not prefixed with an underscor, though private
|
||||
$_public_attribute; // prefixed with an underscor, though public
|
||||
]]>
|
||||
</code>
|
||||
</code_comparison>
|
||||
</documentation>
|
@ -0,0 +1,40 @@
|
||||
<documentation title="Strict comparison operators">
|
||||
<standard>
|
||||
<![CDATA[
|
||||
Some PHP functions return FALSE on failure, but may also have a valid return value of "" or 0, which would evaluate to FALSE in loose comparisons. Be explicit by comparing the variable type when using these return values in conditionals to ensure the return value is indeed what you expect, and not a value that has an equivalent loose-type evaluation.
|
||||
Use the same stringency in returning and checking your own variables. Use === and !== as necessary.
|
||||
]]>
|
||||
</standard>
|
||||
<code_comparison>
|
||||
<code title="Valid strict comparison">
|
||||
<![CDATA[
|
||||
if (strpos($str, 'foo') === FALSE) {
|
||||
echo 'Do something.';
|
||||
}
|
||||
|
||||
function build_string($str = "")
|
||||
{
|
||||
if ($str === "") {
|
||||
echo 'Buid string.';
|
||||
}
|
||||
}
|
||||
]]>
|
||||
</code>
|
||||
<code title="Invalid loose comparison">
|
||||
<![CDATA[
|
||||
// If 'foo' is at the beginning of the string, strpos will return a 0,
|
||||
// resulting in this conditional evaluating as TRUE
|
||||
if (strpos($str, 'foo') == FALSE) {
|
||||
echo 'Do something.';
|
||||
}
|
||||
|
||||
function build_string($str = "")
|
||||
{
|
||||
if ($str == "") { // uh-oh! What if FALSE or the integer 0 is passed as an argument?
|
||||
echo 'Buid string.';
|
||||
}
|
||||
}
|
||||
]]>
|
||||
</code>
|
||||
</code_comparison>
|
||||
</documentation>
|
28
build/CodeIgniter/Docs/Strings/DoubleQuoteUsageStandard.xml
Executable file
28
build/CodeIgniter/Docs/Strings/DoubleQuoteUsageStandard.xml
Executable file
@ -0,0 +1,28 @@
|
||||
<documentation title="Double-quoted strings">
|
||||
<standard>
|
||||
<![CDATA[
|
||||
Always use single quoted strings unless you need variables parsed, and in cases where you do need variables parsed, use braces to prevent greedy token parsing. You may also use double-quoted strings if the string contains single quotes, so you do not have to use escape characters.
|
||||
]]>
|
||||
</standard>
|
||||
<code_comparison>
|
||||
<code title="Examples of invalid double-quoted strings">
|
||||
<![CDATA[
|
||||
"My String" // no variable parsing, so no use for double quotes
|
||||
"My string $foo" // needs braces
|
||||
'SELECT foo FROM bar WHERE baz = \'bag\'' // ugly
|
||||
'\r\n' // it isn't wrong, but it won't be interpreted as a new line feed
|
||||
]]>
|
||||
</code>
|
||||
<code title="Examples of valid double-quoted strings">
|
||||
<![CDATA[
|
||||
'My String'
|
||||
"My string {$foo}" // variables in strings may be enclosed with braces in 2 ways
|
||||
"My string ${foo}"
|
||||
"My string {$foo['bar']}" // variables in strings may be an array entry
|
||||
"My string {$foo->bar}" // variables in strings may be an object attribute
|
||||
"SELECT foo FROM bar WHERE baz = 'bag'"
|
||||
"\n" // not specified in Code Igniter coding standard, but it should be allowed
|
||||
]]>
|
||||
</code>
|
||||
</code_comparison>
|
||||
</documentation>
|
187
build/CodeIgniter/Sniffs/Commenting/InlineCommentSniff.php
Normal file
187
build/CodeIgniter/Sniffs/Commenting/InlineCommentSniff.php
Normal file
@ -0,0 +1,187 @@
|
||||
<?php
|
||||
/**
|
||||
* CodeIgniter_Sniffs_Commenting_InlineCommentSniff.
|
||||
*
|
||||
* 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_Commenting_InlineCommentSniff.
|
||||
*
|
||||
* Ensure the use of single line comments within code (i.e //)
|
||||
* and blank lines between large comment blocks and code.
|
||||
*
|
||||
* @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\Commenting;
|
||||
|
||||
use PHP_CodeSniffer\Sniffs\Sniff;
|
||||
use PHP_CodeSniffer\Files\File;
|
||||
|
||||
class InlineCommentSniff implements Sniff
|
||||
{
|
||||
/**
|
||||
* @var int Limit defining long comments.
|
||||
* Long comments count $longCommentLimit or more lines.
|
||||
*/
|
||||
public $longCommentLimit = 5;
|
||||
|
||||
/**
|
||||
* Returns an array of tokens this test wants to listen for.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
return array(
|
||||
T_COMMENT
|
||||
);
|
||||
}//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();
|
||||
|
||||
// keep testing only if it's about the first comment of the block
|
||||
$previousCommentPtr = $phpcsFile->findPrevious($tokens[$stackPtr]['code'], $stackPtr - 1);
|
||||
if ($tokens[$previousCommentPtr]['line'] !== $tokens[$stackPtr]['line'] - 1) {
|
||||
if (TRUE !== $this->_checkCommentStyle($phpcsFile, $stackPtr)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$commentLines = $this->_getCommentBlock($phpcsFile, $stackPtr);
|
||||
|
||||
if (count($commentLines) >= $this->longCommentLimit) {
|
||||
$this->_checkBlankLinesAroundLongComment($phpcsFile, $commentLines);
|
||||
}
|
||||
}
|
||||
}//end process()
|
||||
|
||||
|
||||
/**
|
||||
* Add error to $phpcsFile, if comment pointed by $stackPtr doesn't start
|
||||
* with '//'.
|
||||
*
|
||||
* @param File $phpcsFile The current file being scanned.
|
||||
* @param int $stackPtr The position of the current token
|
||||
* that has to be a comment.
|
||||
*
|
||||
* @return bool TRUE if the content of the token pointed by $stackPtr starts
|
||||
* with //, FALSE if an error was added to $phpcsFile.
|
||||
*/
|
||||
private function _checkCommentStyle(File $phpcsFile, $stackPtr)
|
||||
{
|
||||
$tokens = $phpcsFile->getTokens();
|
||||
if ($tokens[$stackPtr]['content']{0} === '#') {
|
||||
$error = 'Perl-style comments are not allowed; use "// Comment" or DocBlock comments instead';
|
||||
$phpcsFile->addError($error, $stackPtr, 'WrongStyle');
|
||||
return FALSE;
|
||||
} else if (substr($tokens[$stackPtr]['content'], 0, 2) === '/*'
|
||||
|| $tokens[$stackPtr]['content']{0} === '*'
|
||||
) {
|
||||
$error = 'Multi lines comments are not allowed; use "// Comment" DocBlock comments instead';
|
||||
$phpcsFile->addError($error, $stackPtr, 'WrongStyle');
|
||||
return FALSE;
|
||||
} else if (substr($tokens[$stackPtr]['content'], 0, 2) !== '//') {
|
||||
$error = 'Use single line or DocBlock comments within code';
|
||||
$phpcsFile->addError($error, $stackPtr, 'WrongStyle');
|
||||
return FALSE;
|
||||
}
|
||||
return TRUE;
|
||||
}//_checkCommentStyle()
|
||||
|
||||
|
||||
/**
|
||||
* Gather into an array all comment lines to which $stackPtr belongs.
|
||||
*
|
||||
* @param File $phpcsFile The current file being scanned.
|
||||
* @param int $stackPtr Pointer to the first comment line.
|
||||
*
|
||||
* @return type array Pointers to tokens making up the comment block.
|
||||
*/
|
||||
private function _getCommentBlock(File $phpcsFile, $stackPtr)
|
||||
{
|
||||
$tokens = $phpcsFile->getTokens();
|
||||
$commentLines = array($stackPtr);
|
||||
$nextComment = $stackPtr;
|
||||
$lastLine = $tokens[$stackPtr]['line'];
|
||||
|
||||
while (($nextComment = $phpcsFile->findNext($tokens[$stackPtr]['code'], ($nextComment + 1), null, false)) !== false) {
|
||||
if (($tokens[$nextComment]['line'] - 1) !== $lastLine) {
|
||||
// Not part of the block.
|
||||
break;
|
||||
}
|
||||
|
||||
$lastLine = $tokens[$nextComment]['line'];
|
||||
$commentLines[] = $nextComment;
|
||||
}
|
||||
|
||||
return $commentLines;
|
||||
}//_getCommentBlock()
|
||||
|
||||
|
||||
/**
|
||||
* Add errors to $phpcsFile, if $commentLines isn't enclosed with blank lines.
|
||||
*
|
||||
* @param File $phpcsFile The current file being scanned.
|
||||
* @param array $commentLines Lines of the comment block being checked.
|
||||
*
|
||||
* @return bool TRUE if $commentLines is enclosed with at least a blank line
|
||||
* before and after, FALSE otherwise.
|
||||
*/
|
||||
private function _checkBlankLinesAroundLongComment(File $phpcsFile, array $commentLines)
|
||||
{
|
||||
$hasBlankLinesAround = TRUE;
|
||||
$tokens = $phpcsFile->getTokens();
|
||||
|
||||
// check blank line before the long comment
|
||||
$firstCommentPtr = reset($commentLines);
|
||||
$firstPreviousSpacePtr = $firstCommentPtr - 1;
|
||||
while (T_WHITESPACE === $tokens[$firstPreviousSpacePtr]['code'] && $firstPreviousSpacePtr > 0) {
|
||||
$firstPreviousSpacePtr--;
|
||||
}
|
||||
if ($tokens[$firstPreviousSpacePtr]['line'] >= $tokens[$firstCommentPtr]['line'] - 1) {
|
||||
$error = "Please add a blank line before comments counting more than {$this->longCommentLimit} lines.";
|
||||
$phpcsFile->addError($error, $firstCommentPtr, 'LongCommentWithoutSpacing');
|
||||
$hasBlankLinesAround = FALSE;
|
||||
}
|
||||
|
||||
// check blank line after the long comment
|
||||
$lastCommentPtr = end($commentLines);
|
||||
$lastNextSpacePtr = $lastCommentPtr + 1;
|
||||
while (T_WHITESPACE === $tokens[$lastNextSpacePtr]['code'] && $lastNextSpacePtr < count($tokens)) {
|
||||
$lastNextSpacePtr++;
|
||||
}
|
||||
if ($tokens[$lastNextSpacePtr]['line'] <= $tokens[$lastCommentPtr]['line'] + 1) {
|
||||
$error = "Please add a blank line after comments counting more than {$this->longCommentLimit} lines.";
|
||||
$phpcsFile->addError($error, $lastCommentPtr, 'LongCommentWithoutSpacing');
|
||||
$hasBlankLinesAround = FALSE;
|
||||
}
|
||||
|
||||
return $hasBlankLinesAround;
|
||||
}//end _checkBlanksAroundLongComment()
|
||||
|
||||
}//end class
|
||||
|
||||
?>
|
98
build/CodeIgniter/Sniffs/Files/ByteOrderMarkSniff.php
Executable file
98
build/CodeIgniter/Sniffs/Files/ByteOrderMarkSniff.php
Executable file
@ -0,0 +1,98 @@
|
||||
<?php
|
||||
/**
|
||||
* CodeIgniter_Sniffs_Files_ByteOrderMarkSniff.
|
||||
*
|
||||
* 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_ByteOrderMarkSniff.
|
||||
*
|
||||
* Ensures that no BOM appears at the beginning of file.
|
||||
*
|
||||
* @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 ByteOrderMarkSniff implements Sniff
|
||||
{
|
||||
/**
|
||||
* Returns an array of tokens this test wants to listen for.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
return array( T_OPEN_TAG );
|
||||
}//end register()
|
||||
|
||||
/**
|
||||
* List of supported BOM definitions.
|
||||
*
|
||||
* Use encoding names as keys and hex BOM representations as values.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getBomDefinitions()
|
||||
{
|
||||
return array(
|
||||
'UTF-8' => 'efbbbf',
|
||||
'UTF-16 (BE)' => 'feff',
|
||||
'UTF-16 (LE)' => 'fffe',
|
||||
'UTF-32 (BE)' => '0000feff',
|
||||
'UTF-32 (LE)' => 'fffe0000'
|
||||
);
|
||||
}//end getBomDefinitions()
|
||||
|
||||
/**
|
||||
* Process tokens.
|
||||
*
|
||||
* Actually, only proceed when we're at index 0, this should be the only case
|
||||
* that will contain BOM. Then check if BOM definition matches what
|
||||
* we've found as file's inline HTML. Inline HTML could be longer than just BOM
|
||||
* so make sure you test as much as needed.
|
||||
*
|
||||
* @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;
|
||||
}
|
||||
}
|
||||
|
||||
$tokens = $phpcsFile->getTokens();
|
||||
$fileStartString = $tokens[0]['content'];
|
||||
foreach ($this->getBomDefinitions() as $bomName => $expectedBomHex) {
|
||||
$bomByteLength = strlen($expectedBomHex) / 2;
|
||||
$fileStartHex = bin2hex(substr($fileStartString, 0, $bomByteLength));
|
||||
if ($fileStartHex === $expectedBomHex) {
|
||||
$error = "File contains a $bomName byte order mark (BOM).";
|
||||
$phpcsFile->addError($error, $stackPtr);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}//end process()
|
||||
}
|
222
build/CodeIgniter/Sniffs/Files/Utf8EncodingSniff.php
Executable file
222
build/CodeIgniter/Sniffs/Files/Utf8EncodingSniff.php
Executable file
@ -0,0 +1,222 @@
|
||||
<?php
|
||||
/**
|
||||
* CodeIgniter_Sniffs_Files_Utf8EncodingSniff.
|
||||
*
|
||||
* 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_Utf8EncodingSniff.
|
||||
*
|
||||
* Ensures that PHP files are encoded with Unicode (UTF-8) encoding.
|
||||
*
|
||||
* @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 Utf8EncodingSniff implements Sniff
|
||||
{
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
|
||||
$file_path = $phpcsFile->getFilename();
|
||||
$file_name = basename($file_path);
|
||||
$file_content = file_get_contents($file_path);
|
||||
if (false === mb_check_encoding($file_content, 'UTF-8')) {
|
||||
$error = 'File "' . $file_name . '" should be saved with Unicode (UTF-8) encoding.';
|
||||
$phpcsFile->addError($error, 0);
|
||||
}
|
||||
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.';
|
||||
$phpcsFile->addError($error, 0);
|
||||
}
|
||||
if ( ! self::_checkUtf8Rfc3629($file_content)) {
|
||||
$error = 'File "' . $file_name . '" should be saved with Unicode (UTF-8) encoding, but it did not meet RFC3629 requirements.';
|
||||
$phpcsFile->addError($error, 0);
|
||||
}
|
||||
}//end process()
|
||||
|
||||
|
||||
/**
|
||||
* Checks that the string $content contains only valid UTF-8 chars
|
||||
* using W3C's method.
|
||||
* Returns true if $content contains only UTF-8 chars, false otherwise.
|
||||
*
|
||||
* @param string $content String to check.
|
||||
*
|
||||
* @return bool true if $content contains only UTF-8 chars, false otherwise.
|
||||
*
|
||||
* @see http://w3.org/International/questions/qa-forms-utf-8.html
|
||||
*/
|
||||
private static function _checkUtf8W3c($content)
|
||||
{
|
||||
$content_chunks=self::mb_chunk_split($content, 4096, '');
|
||||
foreach($content_chunks as $content_chunk)
|
||||
{
|
||||
$preg_result= preg_match(
|
||||
'%^(?:
|
||||
[\x09\x0A\x0D\x20-\x7E] # ASCII
|
||||
| [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
|
||||
| \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
|
||||
| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
|
||||
| \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
|
||||
| \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
|
||||
| [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
|
||||
| \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
|
||||
)*$%xs',
|
||||
$content_chunk
|
||||
);
|
||||
if($preg_result!==1)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
return true;
|
||||
}//end _checkUtf8W3c()
|
||||
|
||||
/**
|
||||
* Checks that the string $content contains only valid UTF-8 chars
|
||||
* using the method described in RFC 3629.
|
||||
* Returns true if $content contains only UTF-8 chars, false otherwise.
|
||||
*
|
||||
* @param string $content String to check.
|
||||
*
|
||||
* @return bool true if $content contains only UTF-8 chars, false otherwise.
|
||||
*
|
||||
* @see http://www.php.net/manual/en/function.mb-detect-encoding.php#85294
|
||||
*/
|
||||
private static function _checkUtf8Rfc3629($content)
|
||||
{
|
||||
$len = strlen($content);
|
||||
for ($i = 0; $i < $len; $i++) {
|
||||
$c = ord($content[$i]);
|
||||
if ($c > 128) {
|
||||
if (($c >= 254)) {
|
||||
return false;
|
||||
} elseif ($c >= 252) {
|
||||
$bits=6;
|
||||
} elseif ($c >= 248) {
|
||||
$bits=5;
|
||||
} elseif ($c >= 240) {
|
||||
$bytes = 4;
|
||||
} elseif ($c >= 224) {
|
||||
$bytes = 3;
|
||||
} elseif ($c >= 192) {
|
||||
$bytes = 2;
|
||||
} else {
|
||||
return false;
|
||||
} if (($i + $bytes) > $len) {
|
||||
return false;
|
||||
} while ($bytes > 1) {
|
||||
$i++;
|
||||
$b = ord($content[$i]);
|
||||
if ($b < 128 || $b > 191) {
|
||||
return false;
|
||||
}
|
||||
$bytes--;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}//_checkUtf8Rfc3629()
|
||||
|
||||
/**
|
||||
* Splits a string to chunks of given size
|
||||
* This helps to avoid segmentation fault errors when large text is given
|
||||
* Returns array of strings after splitting
|
||||
*
|
||||
* @param string $str String to split.
|
||||
* @param int $len number of characters per chunk
|
||||
*
|
||||
* @return array string array after splitting
|
||||
*
|
||||
* @see http://php.net/manual/en/function.chunk-split.php
|
||||
*/
|
||||
private static function mb_chunk_split($str, $len, $glue)
|
||||
{
|
||||
if (empty($str)) return false;
|
||||
$array = self::mbStringToArray ($str);
|
||||
$n = -1;
|
||||
$new = Array();
|
||||
foreach ($array as $char) {
|
||||
$n++;
|
||||
if ($n < $len) $new []= $char;
|
||||
elseif ($n == $len) {
|
||||
$new []= $glue . $char;
|
||||
$n = 0;
|
||||
}
|
||||
}
|
||||
return $new;
|
||||
}//mb_chunk_split
|
||||
/**
|
||||
* Supporting function for mb_chunk_split
|
||||
*
|
||||
* @param string $str
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @see http://php.net/manual/en/function.chunk-split.php
|
||||
*/
|
||||
private static function mbStringToArray ($str)
|
||||
{
|
||||
if (empty($str)) return false;
|
||||
$len = mb_strlen($str);
|
||||
$array = array();
|
||||
for ($i = 0; $i < $len; $i++) {
|
||||
$array[] = mb_substr($str, $i, 1);
|
||||
}
|
||||
return $array;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}//end class
|
||||
|
||||
?>
|
@ -0,0 +1,81 @@
|
||||
<?php
|
||||
/**
|
||||
* CodeIgniter_Sniffs_Operators_LogicalOperatorAndSniff.
|
||||
*
|
||||
* 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_LogicalOperatorAndSniff.
|
||||
*
|
||||
* Ensures that the logical operator 'AND' is in upper case and suggest the use of 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 LogicalOperatorAndSniff implements Sniff
|
||||
{
|
||||
/**
|
||||
* Returns an array of tokens this test wants to listen for: symbolic and literal operators and.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
return array(
|
||||
T_LOGICAL_AND,
|
||||
);
|
||||
|
||||
}//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_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, 'LowercaseLogicalOperator');
|
||||
}
|
||||
|
||||
$warning_message = 'The symbolic form "&&" is preferred over the literal form "AND"';
|
||||
$phpcsFile->addWarning($warning_message, $stackPtr, 'UseOfLiteralAndOperator');
|
||||
|
||||
}//end process()
|
||||
|
||||
|
||||
}//end class
|
||||
|
||||
?>
|
81
build/CodeIgniter/Sniffs/Operators/StrictComparisonOperatorSniff.php
Executable file
81
build/CodeIgniter/Sniffs/Operators/StrictComparisonOperatorSniff.php
Executable file
@ -0,0 +1,81 @@
|
||||
<?php
|
||||
/**
|
||||
* CodeIgniter_Sniffs_Operators_StrictComparisonOperatorSniff.
|
||||
*
|
||||
* 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_StrictComparisonOperatorSniff.
|
||||
*
|
||||
* Ensures that only strict comparison operators are used instead of
|
||||
* equal and not equal operators.
|
||||
*
|
||||
* @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 StrictComparisonOperatorSniff implements Sniff
|
||||
{
|
||||
private static $_replacements = array(
|
||||
T_IS_EQUAL => '===',
|
||||
T_IS_NOT_EQUAL => '!==',
|
||||
);
|
||||
|
||||
/**
|
||||
* Returns an array of tokens this test wants to listen for.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
return array(
|
||||
T_IS_EQUAL,
|
||||
T_IS_NOT_EQUAL,
|
||||
);
|
||||
}//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'];
|
||||
|
||||
$error_message = '"==" and "!=" are prohibited; use "'
|
||||
. self::$_replacements[$operator_code] . '" instead of "'
|
||||
. $operator_string . '".';
|
||||
$phpcsFile->addError($error_message, $stackPtr, 'NonStrictComparisonUsed');
|
||||
}//end process()
|
||||
|
||||
|
||||
}//end class
|
||||
|
||||
?>
|
@ -0,0 +1,84 @@
|
||||
<?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
|
||||
|
||||
?>
|
464
build/CodeIgniter/Sniffs/Strings/DoubleQuoteUsageSniff.php
Executable file
464
build/CodeIgniter/Sniffs/Strings/DoubleQuoteUsageSniff.php
Executable file
@ -0,0 +1,464 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* CodeIgniter_Sniffs_Strings_DoubleQuoteUsageSniff.
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Sniffs\Strings;
|
||||
|
||||
use PHP_CodeSniffer\Sniffs\Sniff;
|
||||
use PHP_CodeSniffer\Files\File;
|
||||
|
||||
/**
|
||||
* CodeIgniter_Sniffs_Strings_DoubleQuoteUsageSniff.
|
||||
*
|
||||
* Ensures that double-quoted strings are used only to parse variables,
|
||||
* to avoid escape characters before single quotes or for chars that need
|
||||
* to be interpreted like \r, \n or \t.
|
||||
* If a double-quoted string contain both single and double quotes
|
||||
* but no variable, then a warning is raised to encourage the use of
|
||||
* single-quoted strings.
|
||||
*
|
||||
* @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
|
||||
*/
|
||||
class VariableUsageSniff implements Sniff
|
||||
{
|
||||
/**
|
||||
* Returns an array of tokens this test wants to listen for.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
/*
|
||||
return array(
|
||||
T_DOUBLE_QUOTED_STRING,
|
||||
T_CONSTANT_ENCAPSED_STRING,
|
||||
);
|
||||
*/
|
||||
return array();
|
||||
}//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();
|
||||
$string = $tokens[$stackPtr]['content'];
|
||||
// makes sure that it is about a double quote string,
|
||||
// since variables are not parsed out of double quoted string
|
||||
$openDblQtStr = substr($string, 0, 1);
|
||||
if (0 === strcmp($openDblQtStr, '"')) {
|
||||
$this->processDoubleQuotedString($phpcsFile, $stackPtr, $string);
|
||||
} else if (0 === strcmp($openDblQtStr, "'")) {
|
||||
$this->processSingleQuotedString($phpcsFile, $stackPtr, $string);
|
||||
}
|
||||
}//end process()
|
||||
|
||||
|
||||
/**
|
||||
* Processes this test, when the token encountered is a double-quoted string.
|
||||
*
|
||||
* @param File $phpcsFile The current file being scanned.
|
||||
* @param int $stackPtr The position of the current token
|
||||
* in the stack passed in $tokens.
|
||||
* @param string $dblQtString The double-quoted string content,
|
||||
* i.e. without quotes.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function processDoubleQuotedString (File $phpcsFile, $stackPtr, $dblQtString)
|
||||
{
|
||||
$variableFound = FALSE;
|
||||
$strTokens = token_get_all('<?php '.$dblQtString);
|
||||
$strPtr = 1; // skip php opening tag added by ourselves
|
||||
$requireDblQuotes = FALSE;
|
||||
while ($strPtr < count($strTokens)) {
|
||||
$strToken = $strTokens[$strPtr];
|
||||
if (is_array($strToken)) {
|
||||
if (in_array($strToken[0], array(T_DOLLAR_OPEN_CURLY_BRACES, T_CURLY_OPEN))) {
|
||||
$strPtr++;
|
||||
try {
|
||||
$this->_parseVariable($strTokens, $strPtr);
|
||||
} catch (Exception $err) {
|
||||
$error = 'There is no variable, object nor array between curly braces. Please use the escape char for $ or {.';
|
||||
$phpcsFile->addError($error, $stackPtr);
|
||||
}
|
||||
$variableFound = TRUE;
|
||||
if ('}' !== $strTokens[$strPtr]) {
|
||||
$error = 'There is no matching closing curly brace.';
|
||||
$phpcsFile->addError($error, $stackPtr);
|
||||
}
|
||||
// don't move forward, since it will be done in the main loop
|
||||
// $strPtr++;
|
||||
} else if (T_VARIABLE === $strToken[0]) {
|
||||
$variableFound = TRUE;
|
||||
$error = "Variable {$strToken[1]} in double-quoted strings should be enclosed with curly braces. Please consider {{$strToken[1]}}";
|
||||
$phpcsFile->addError($error, $stackPtr);
|
||||
}
|
||||
}
|
||||