Skip to content
This repository was archived by the owner on Nov 5, 2022. It is now read-only.

Commit 939b90f

Browse files
committed
Merge branch 'release/1.0.0-beta0006'
2 parents d1d815a + 281fd73 commit 939b90f

26 files changed

+648
-490
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ composer.phar
44
# Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file
55
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
66
# composer.lock
7+
/Codeigniter4/Tests
8+
/tests

CONTRIBUTING.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
# CodeIgniter4-Standard
22

33
## Contributing
4+

Codeigniter4/Sniffs/Commenting/FileCommentSniff.php renamed to CodeIgniter4/Sniffs/Commenting/FileCommentSniff.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,7 @@ protected function processTags(File $phpcsFile, $stackPtr, $commentStart)
171171
{
172172
$tokens = $phpcsFile->getTokens();
173173

174-
if (get_class($this) === 'CodeIgniter3FileCommentSniff') {
175-
$docBlock = 'file';
176-
} else {
177-
$docBlock = 'class';
178-
}
174+
$docBlock = 'file';
179175

180176
$commentEnd = $tokens[$commentStart]['comment_closer'];
181177

@@ -285,7 +281,7 @@ protected function processPackage(File $phpcsFile, array $tags)
285281
}
286282

287283
if ($isInvalidPackage === true) {
288-
$error = 'Package name "%s" is not valid; consider "%s" instead.';
284+
$error = 'Package name "%s" is not valid. Use "%s" instead';
289285
$validName = trim($newName, '_');
290286
$data = array(
291287
$content,
@@ -366,12 +362,16 @@ protected function processAuthor(File $phpcsFile, array $tags)
366362
}
367363

368364
$content = $tokens[($tag + 2)]['content'];
369-
$local = '\da-zA-Z-_+';
370-
// Dot character cannot be the first or last character in the local-part.
371-
$localMiddle = $local.'.\w';
372-
if (preg_match('/^([^<]*)\s+<(['.$local.'](['.$localMiddle.']*['.$local.'])*@[\da-zA-Z][-.\w]*[\da-zA-Z]\.[a-zA-Z]{2,7})>$/', $content) === 0) {
373-
$error = 'Content of the @author tag must be in the form "Display Name <username@example.com>"';
374-
$phpcsFile->addError($error, $tag, 'InvalidAuthors');
365+
366+
// If it has an @ it's probably contains email address.
367+
if (strrpos($content, '@') !== false) {
368+
$local = '\da-zA-Z-_+';
369+
// Dot character cannot be the first or last character in the local-part.
370+
$localMiddle = $local.'.\w';
371+
if (preg_match('/^([^<]*)\s+<(['.$local.'](['.$localMiddle.']*['.$local.'])*@[\da-zA-Z][-.\w]*[\da-zA-Z]\.[a-zA-Z]{2,7})>$/', $content) === 0) {
372+
$error = '"@author" with email must be "Display Name <username@example.com>"';
373+
$phpcsFile->addError($error, $tag, 'InvalidAuthors');
374+
}
375375
}
376376
}
377377

@@ -411,7 +411,7 @@ protected function processCopyright(File $phpcsFile, array $tags)
411411
}
412412
}
413413
} else {
414-
$error = '@copyright tag must contain a year and the name of the copyright holder';
414+
$error = '"@copyright" must be "YYYY [- YYYY] Name of the copyright holder"';
415415
$phpcsFile->addError($error, $tag, 'IncompleteCopyright');
416416
}
417417
}//end foreach

Codeigniter4/Sniffs/ControlStructures/AllmanControlSignatureSniff.php renamed to CodeIgniter4/Sniffs/ControlStructures/AllmanControlSignatureSniff.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function process(File $phpcsFile, $stackPtr)
7474
|| $tokens[$stackPtr]['code'] === T_WHILE
7575
|| $tokens[($stackPtr)]['code'] === T_ELSE
7676
) {
77-
// If this is alternate syntax ":" instead of ":" then skip it.
77+
// If this is alternate syntax ":" instead of "{" then skip it.
7878
if (isset($tokens[$stackPtr]['scope_opener']) === true) {
7979
$openingBracePtr = $tokens[$stackPtr]['scope_opener'];
8080
if ($tokens[$openingBracePtr]['code'] === T_COLON) {
@@ -144,7 +144,7 @@ public function process(File $phpcsFile, $stackPtr)
144144
$openingBracePtr = $tokens[$stackPtr]['scope_opener'];
145145
$braceLine = $tokens[$openingBracePtr]['line'];
146146

147-
// If this is alternate syntax ":" instead of ":" then skip it.
147+
// If this is alternate syntax ":" instead of "{" then skip it.
148148
if (isset($tokens[$stackPtr]['scope_opener']) === true) {
149149
$openingBracePtr = $tokens[$stackPtr]['scope_opener'];
150150
if ($tokens[$openingBracePtr]['code'] === T_COLON) {

Codeigniter4/Sniffs/ControlStructures/ControlStructureSpacingSniff.php renamed to CodeIgniter4/Sniffs/ControlStructures/ControlStructureSpacingSniff.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,6 @@ public function process(File $phpcsFile, $stackPtr)
9696
}
9797
}
9898

99-
// Spaces after control structure open parenthesis.
100-
$phpcsFile->recordMetric($stackPtr, 'Spaces after control structure open parenthesis', $spaceAfterOpen);
101-
10299
if ($spaceAfterOpen !== $this->requiredSpacesAfterOpen) {
103100
$error = 'Expected %s spaces after opening bracket; %s found';
104101
$data = array(
@@ -125,8 +122,6 @@ public function process(File $phpcsFile, $stackPtr)
125122
$spaceBeforeClose = strlen(ltrim($tokens[($parenCloser - 1)]['content'], $phpcsFile->eolChar));
126123
}
127124

128-
$phpcsFile->recordMetric($stackPtr, 'Spaces before control structure close parenthesis', $spaceBeforeClose);
129-
130125
if ($spaceBeforeClose !== $this->requiredSpacesBeforeClose) {
131126
$error = 'Expected %s spaces before closing bracket; %s found';
132127
$data = array(

Codeigniter4/Sniffs/Files/FilenameMatchesClassSniff.php renamed to CodeIgniter4/Sniffs/Files/FilenameMatchesClassSniff.php

File renamed without changes.

Codeigniter4/Sniffs/Files/OneClassPerFileSniff.php renamed to CodeIgniter4/Sniffs/Files/OneClassPerFileSniff.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class OneClassPerFileSniff implements Sniff
3131
*/
3232
public $filesAllowedMultiClass = array(
3333
'Exception.php',
34+
'Exceptions.php',
3435
'Response.php',
3536
);
3637

Codeigniter4/Sniffs/Functions/FunctionDeclarationSniff.php renamed to CodeIgniter4/Sniffs/Functions/FunctionDeclarationSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public function process(File $phpcsFile, $stackPtr)
122122
if ($spaces !== 0) {
123123
$error = 'Expected 0 spaces before opening parenthesis; %s found';
124124
$data = array($spaces);
125-
$fix = $phpcsFile->addFixableError($error, $openBracket, 'SpaceBeforeOpenParen', $data);
125+
$fix = $phpcsFile->addFixableError($error, $openBracket, 'SpaceBeforeOpenParenthesis', $data);
126126
if ($fix === true) {
127127
$phpcsFile->fixer->replaceToken(($openBracket - 1), '');
128128
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
/**
3+
* Valid Function Name
4+
*
5+
* @package CodeIgniter4-Standard
6+
* @author Louis Linehan <louis.linehan@gmail.com>
7+
* @copyright 2017 Louis Linehan
8+
* @license https://github.com/louisl/CodeIgniter4-Standard/blob/master/LICENSE MIT License
9+
*/
10+
11+
namespace CodeIgniter4\Sniffs\NamingConventions;
12+
13+
use PHP_CodeSniffer\Sniffs\AbstractScopeSniff;
14+
use CodeIgniter4\Util\Common;
15+
use PHP_CodeSniffer\Files\File;
16+
17+
/**
18+
* Valid Function Name Sniff
19+
*
20+
* @author Louis Linehan <louis.linehan@gmail.com>
21+
*/
22+
class ValidFunctionNameSniff extends AbstractScopeSniff
23+
{
24+
25+
26+
/**
27+
* Defines which token(s) in which scope(s) will be proceed.
28+
*/
29+
public function __construct()
30+
{
31+
parent::__construct(array(T_CLASS, T_ANON_CLASS, T_INTERFACE, T_TRAIT), array(T_FUNCTION), true);
32+
33+
}//end __construct()
34+
35+
36+
/**
37+
* Processes the tokens outside the scope.
38+
*
39+
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being processed.
40+
* @param int $stackPtr The position where this token was
41+
* found.
42+
*
43+
* @return void
44+
*/
45+
protected function processTokenOutsideScope(File $phpcsFile, $stackPtr)
46+
{
47+
$functionName = $phpcsFile->getDeclarationName($stackPtr);
48+
if ($functionName === null) {
49+
return;
50+
}
51+
52+
// Is this a magic function. i.e., it is prefixed with "__"?
53+
if (preg_match('|^__[^_]|', $functionName) !== 0) {
54+
$magicPart = strtolower(substr($functionName, 2));
55+
if (isset(Common::$magicMethods[$magicPart]) === false) {
56+
$errorData = array($functionName);
57+
$error = 'Function name "%s" is invalid; only PHP magic methods should be prefixed with a double underscore';
58+
$phpcsFile->addError($error, $stackPtr, 'FunctionDoubleUnderscore', $errorData);
59+
}
60+
61+
return;
62+
}
63+
64+
if (Common::isLowerSnakeCase($functionName) === false
65+
|| $functionName !== strtolower($functionName)
66+
) {
67+
$errorData = array($functionName);
68+
$error = 'Function "%s" must be snake_case';
69+
$phpcsFile->addError($error, $stackPtr, 'FunctionNotSnakeCase', $errorData);
70+
}
71+
72+
$warningLimit = 50;
73+
if (strlen($functionName) > $warningLimit) {
74+
$errorData = array(
75+
$functionName,
76+
$warningLimit,
77+
);
78+
$warning = 'Function "%s" is over "%s" chars';
79+
$phpcsFile->addWarning($warning, $stackPtr, 'FunctionNameIsLong', $errorData);
80+
}
81+
82+
}//end processTokenOutsideScope()
83+
84+
85+
/**
86+
* Processes the tokens within the scope.
87+
*
88+
* @param File $phpcsFile The file being processed.
89+
* @param int $stackPtr The position where this token was
90+
* found.
91+
* @param int $currScope The position of the current scope.
92+
*
93+
* @return void
94+
*/
95+
protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScope)
96+
{
97+
98+
}//end processTokenWithinScope()
99+
100+
101+
}//end class
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<?php
2+
/**
3+
* Valid Method Name
4+
*
5+
* @package CodeIgniter4-Standard
6+
* @author Louis Linehan <louis.linehan@gmail.com>
7+
* @copyright 2017 Louis Linehan
8+
* @license https://github.com/louisl/CodeIgniter4-Standard/blob/master/LICENSE MIT License
9+
*/
10+
11+
namespace CodeIgniter4\Sniffs\NamingConventions;
12+
13+
use PHP_CodeSniffer\Sniffs\AbstractScopeSniff;
14+
use CodeIgniter4\Util\Common;
15+
use PHP_CodeSniffer\Files\File;
16+
17+
/**
18+
* Valid Method Name Sniff
19+
*
20+
* Checks class methods are lowerCameCase.
21+
* Checks public methods are not prefixed with "_" except
22+
* methods defined in allowedPublicMethodNames.
23+
* Checks private and protected methods are prefixed with "_".
24+
* Checks functions are snake_case.
25+
* Warns if names are longer than 50 characters.
26+
*
27+
* @author Louis Linehan <louis.linehan@gmail.com>
28+
*/
29+
class ValidMethodNameSniff extends AbstractScopeSniff
30+
{
31+
32+
33+
/**
34+
* Defines which token(s) in which scope(s) will be proceed.
35+
*/
36+
public function __construct()
37+
{
38+
parent::__construct(array(T_CLASS, T_ANON_CLASS, T_INTERFACE, T_TRAIT), array(T_FUNCTION), true);
39+
40+
}//end __construct()
41+
42+
43+
/**
44+
* Processes a token within the scope that this test is listening to.
45+
*
46+
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file where the token was found.
47+
* @param int $stackPtr The position in the stack where
48+
* this token was found.
49+
*
50+
* @return void
51+
*/
52+
protected function processTokenOutsideScope(File $phpcsFile, $stackPtr)
53+
{
54+
55+
}//end processTokenOutsideScope()
56+
57+
58+
/**
59+
* Processes the tokens within the scope.
60+
*
61+
* @param File $phpcsFile The file being processed.
62+
* @param int $stackPtr The position where this token was
63+
* found.
64+
* @param int $currScope The position of the current scope.
65+
*
66+
* @return void
67+
*/
68+
protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScope)
69+
{
70+
$methodName = $phpcsFile->getDeclarationName($stackPtr);
71+
if ($methodName === null) {
72+
// Ignore closures.
73+
return;
74+
}
75+
76+
$className = $phpcsFile->getDeclarationName($currScope);
77+
78+
// Is this a magic method. i.e., is prefixed with "__"?
79+
if (preg_match('|^__[^_]|', $methodName) !== 0) {
80+
$magicPart = strtolower(substr($methodName, 2));
81+
if (isset(Common::$magicMethods[$magicPart]) === false) {
82+
$errorData = array($className.'::'.$methodName);
83+
$error = 'Method name "%s" is invalid; only PHP magic methods should be prefixed with a double underscore';
84+
$phpcsFile->addError($error, $stackPtr, 'MethodDoubleUnderscore', $errorData);
85+
}
86+
87+
return;
88+
}
89+
90+
// Get the method name without underscore prefix if it exists.
91+
if (strrpos($methodName, '_') === 0) {
92+
$namePart = substr($methodName, 1);
93+
} else {
94+
$namePart = $methodName;
95+
}
96+
97+
// Naming check.
98+
if (Common::isCamelCaps($namePart, false, true, false) === false) {
99+
$errorData = array($methodName);
100+
$error = 'Method "%s" must be lowerCamelCase';
101+
$phpcsFile->addError($error, $stackPtr, 'MethodNotLowerCamelCase', $errorData);
102+
}
103+
104+
// Methods must not be prefixed with an underscore except those in publicMethodNames.
105+
if (strrpos($methodName, '_') === 0) {
106+
if (isset(Common::$publicMethodNames[$methodName]) === false) {
107+
$methodProps = $phpcsFile->getMethodProperties($stackPtr);
108+
$scope = $methodProps['scope'];
109+
$errorData = array($className.'::'.$methodName);
110+
$error = ucfirst($scope).' method "%s" must not be prefixed with an underscore';
111+
$phpcsFile->addError($error, $stackPtr, 'MethodMustNotHaveUnderscore', $errorData);
112+
}
113+
}
114+
115+
// Warn if method name is over 50 chars.
116+
$warningLimit = 50;
117+
if (strlen($methodName) > $warningLimit) {
118+
$errorData = array(
119+
$methodName,
120+
$warningLimit,
121+
);
122+
123+
$warning = 'Method "%s" is over "%s" chars';
124+
$phpcsFile->addWarning($warning, $stackPtr, 'MethodNameIsLong', $errorData);
125+
}
126+
127+
}//end processTokenWithinScope()
128+
129+
130+
}//end class

0 commit comments

Comments
 (0)