From fe442f553dfcb4b72181f8103be7555a512a4f2f Mon Sep 17 00:00:00 2001 From: Moosh Aka ctg Date: Wed, 4 Oct 2017 11:16:54 +0200 Subject: [PATCH 01/13] ajout info diverses dans composer.json Signed-off-by: Moosh Aka ctg --- composer.json | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 1ad660d..91295ce 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,7 @@ } }, "name" : "kamelot/code-sniffer", - "description" : "Ruleset for phpcodesniffer. C'est mon premier package composer. C'est mon premier gitflow. C'est mon premier Ruleset. Tout feedback est bien venu", + "description" : "Ruleset for phpcodesniffer. (C'est mon premier package composer. C'est mon premier gitflow. C'est mon premier Ruleset. Tout feedback est bien venu"), "license" : "LGPL-3.0", "require" : { "squizlabs/php_codesniffer" : "3.1.0", @@ -21,7 +21,18 @@ "authors" : [{ "name" : "Moosh", "email" : "christophe@gesche.org", + "role" : "Lead,Developper" + }, + { + "name" : "Gaël", "role" : "Developper" } - ] + ], + "support" : { + "issues" : "https://github.com/Moosh-be/CodeSniffer/issues", + "wiki" : "https://github.com/Moosh-be/CodeSniffer/wiki", + "docs" : "https://github.com/Moosh-be/CodeSniffer/blob/master/README.md", + "source" : "https://github.com/Moosh-be/CodeSniffer" + }, + "homepage" : "https://moosh-be.github.io/CodeSniffer/" } \ No newline at end of file From 7452c17d9ce6624b43d52b528cf762b51b52258c Mon Sep 17 00:00:00 2001 From: Moosh Aka ctg Date: Wed, 4 Oct 2017 11:48:07 +0200 Subject: [PATCH 02/13] fix composer.json Signed-off-by: Moosh Aka ctg --- composer.json | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 91295ce..e8b2eed 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,7 @@ } }, "name" : "kamelot/code-sniffer", - "description" : "Ruleset for phpcodesniffer. (C'est mon premier package composer. C'est mon premier gitflow. C'est mon premier Ruleset. Tout feedback est bien venu"), + "description" : "Ruleset for phpcodesniffer. (C'est mon premier package composer. C'est mon premier gitflow. C'est mon premier Ruleset. Tout feedback est bien venu", "license" : "LGPL-3.0", "require" : { "squizlabs/php_codesniffer" : "3.1.0", @@ -22,8 +22,7 @@ "name" : "Moosh", "email" : "christophe@gesche.org", "role" : "Lead,Developper" - }, - { + }, { "name" : "Gaël", "role" : "Developper" } From b07e10d045615bbb178b736d506b92bfc862b986 Mon Sep 17 00:00:00 2001 From: lgnap Date: Wed, 4 Oct 2017 09:56:17 +0200 Subject: [PATCH 03/13] refactor: move src into src folder as requested into composer.json --- .../Kamelot/Docs/Commenting/TodoStandard.xml | 0 .../Kamelot/Sniffs/Commenting/TodoSniff.php | 19 +++++++++++-------- .../standard}/Kamelot/ruleset.xml | 0 3 files changed, 11 insertions(+), 8 deletions(-) rename {standard => src/standard}/Kamelot/Docs/Commenting/TodoStandard.xml (100%) rename {standard => src/standard}/Kamelot/Sniffs/Commenting/TodoSniff.php (86%) rename {standard => src/standard}/Kamelot/ruleset.xml (100%) diff --git a/standard/Kamelot/Docs/Commenting/TodoStandard.xml b/src/standard/Kamelot/Docs/Commenting/TodoStandard.xml similarity index 100% rename from standard/Kamelot/Docs/Commenting/TodoStandard.xml rename to src/standard/Kamelot/Docs/Commenting/TodoStandard.xml diff --git a/standard/Kamelot/Sniffs/Commenting/TodoSniff.php b/src/standard/Kamelot/Sniffs/Commenting/TodoSniff.php similarity index 86% rename from standard/Kamelot/Sniffs/Commenting/TodoSniff.php rename to src/standard/Kamelot/Sniffs/Commenting/TodoSniff.php index 4a4f9e7..49c0a83 100644 --- a/standard/Kamelot/Sniffs/Commenting/TodoSniff.php +++ b/src/standard/Kamelot/Sniffs/Commenting/TodoSniff.php @@ -12,6 +12,12 @@ * @link http://pear.php.net/package/PHP_CodeSniffer */ +namespace Standards\Sniffs\Comments; + +use PHP_CodeSniffer\Files\File; +use PHP_CodeSniffer\Sniffs\Sniff; +use PHP_CodeSniffer\Util\Tokens; + /** * Based on Generic_Sniffs_Commenting_TodoSniff. * @@ -26,9 +32,7 @@ * @link http://pear.php.net/package/PHP_CodeSniffer */ -use PHP_CodeSniffer\Sniffs\Sniff; - -class Kamelot_Sniffs_Commenting_TodoSniff implements Sniff +class TodoSniff implements Sniff { /** @@ -49,7 +53,7 @@ class Kamelot_Sniffs_Commenting_TodoSniff implements Sniff */ public function register() { - return PHP_CodeSniffer_Tokens::$commentTokens; + return Tokens::$commentTokens; }//end register() @@ -57,13 +61,12 @@ public function register() /** * Processes this sniff, when one of its tokens is encountered. * - * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. - * @param int $stackPtr The position of the current token - * in the stack passed in $tokens. + * @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(PHP_CodeSniffer_File $phpcsFile, $stackPtr) + public function process(File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); diff --git a/standard/Kamelot/ruleset.xml b/src/standard/Kamelot/ruleset.xml similarity index 100% rename from standard/Kamelot/ruleset.xml rename to src/standard/Kamelot/ruleset.xml From bf2d95001678fc459fc224a7afcd19a900709c6c Mon Sep 17 00:00:00 2001 From: lgnap Date: Wed, 4 Oct 2017 10:28:29 +0200 Subject: [PATCH 04/13] Gitignore .idea --- .gitignore | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 55940e5..ea7cdb2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,6 @@ +# IDE relatives +/.idea/ + +# Composer /vendor/ -composer.lock \ No newline at end of file +composer.lock From a7f58fb1e1c88cbb25a2b25cbd3f70c5b69985e4 Mon Sep 17 00:00:00 2001 From: lgnap Date: Wed, 4 Oct 2017 10:29:07 +0200 Subject: [PATCH 05/13] Add default config to run phpunit tests --- composer.json | 5 ++++- phpunit.xml | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 phpunit.xml diff --git a/composer.json b/composer.json index e8b2eed..e7c839a 100644 --- a/composer.json +++ b/composer.json @@ -18,6 +18,9 @@ "squizlabs/php_codesniffer" : "3.1.0", "php" : ">=7.0" }, + "require-dev": { + "phpunit/phpunit": "^6.3" + }, "authors" : [{ "name" : "Moosh", "email" : "christophe@gesche.org", @@ -34,4 +37,4 @@ "source" : "https://github.com/Moosh-be/CodeSniffer" }, "homepage" : "https://moosh-be.github.io/CodeSniffer/" -} \ No newline at end of file +} diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..9faf1d4 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,20 @@ + + + + + tests + + + + + src + + + From 043020cdd71b7ed06dcfd70405d8ca21c444e1bd Mon Sep 17 00:00:00 2001 From: lgnap Date: Wed, 4 Oct 2017 10:39:25 +0200 Subject: [PATCH 06/13] Custom autoloader for tests --- phpunit.xml | 2 +- tests/autoload.php | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 tests/autoload.php diff --git a/phpunit.xml b/phpunit.xml index 9faf1d4..1457c19 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,7 +1,7 @@ Date: Wed, 4 Oct 2017 12:36:14 +0200 Subject: [PATCH 07/13] Use return of preg_match --- src/standard/Kamelot/Sniffs/Commenting/TodoSniff.php | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/standard/Kamelot/Sniffs/Commenting/TodoSniff.php b/src/standard/Kamelot/Sniffs/Commenting/TodoSniff.php index 49c0a83..76f0334 100644 --- a/src/standard/Kamelot/Sniffs/Commenting/TodoSniff.php +++ b/src/standard/Kamelot/Sniffs/Commenting/TodoSniff.php @@ -73,14 +73,8 @@ public function process(File $phpcsFile, $stackPtr) $content = $tokens[$stackPtr]['content']; $matches = array(); // @Todo pouvoir personaliser la regexp - preg_match('/(?:\A|[^\p{L}]+)todo([^\p{L}]+(.*)|\Z)/ui', $content, $matches); - if (empty($matches) === false) { - - preg_match('/WWW([a-z])*-([0-9])*/ui', $matches[1], $jiramatches); - if (empty($jiramatches) === false) { - - } else { - + if (preg_match('/(?:\A|[^\p{L}]+)todo([^\p{L}]+(.*)|\Z)/ui', $content, $matches)) { + if (! preg_match('/WWW([a-z])*-([0-9])*/ui', $matches[1], $jiramatches)) { // Clear whitespace and some common characters not required at // the end of a to-do message to make the warning more informative. From f211988c3efa0b476383e40a9549d46a06924d24 Mon Sep 17 00:00:00 2001 From: lgnap Date: Wed, 4 Oct 2017 16:30:56 +0200 Subject: [PATCH 08/13] Ask for an installation through composer --- README.md | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 9cc1efa..5712d84 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ vendor/bin/phpcs --standard=standard/Kamelot/ruleset.xml --extensions=php \ --encoding=utf-8 -n tests/Kamelot/ok/ --colors ``` -Default ussage +Default usage ```bash vendor/bin/phpcs --standard=standard/Kamelot/ruleset.xml --extensions=php \ --encoding=utf-8 -n src --colors @@ -38,15 +38,6 @@ vendor/bin/phpcs --standard=standard/Kamelot/ruleset.xml \ ## Installation -Installation in a Composer project (method 1) - - - Add the following lines to the require-dev section of your composer.json file. - - "require-dev": { - "squizlabs/php_codesniffer": "^2.2 || ^3.0.2", - "moosh-be/CodeSniffer": "*" - }, - "prefer-stable" : true - +Installation through Composer + composer require kamelot/CodeSniffer From dc90f59dce4581a7e817d9423a3a234a8fa555b7 Mon Sep 17 00:00:00 2001 From: lgnap Date: Wed, 4 Oct 2017 16:40:57 +0200 Subject: [PATCH 09/13] TodoSniff mades compatible to phpcs 3.0 --- .../Kamelot/Sniffs/Commenting/TodoSniff.php | 52 +++++++++++++++---- 1 file changed, 43 insertions(+), 9 deletions(-) diff --git a/src/standard/Kamelot/Sniffs/Commenting/TodoSniff.php b/src/standard/Kamelot/Sniffs/Commenting/TodoSniff.php index 76f0334..8e7e970 100644 --- a/src/standard/Kamelot/Sniffs/Commenting/TodoSniff.php +++ b/src/standard/Kamelot/Sniffs/Commenting/TodoSniff.php @@ -64,19 +64,52 @@ public function register() * @param File $phpcsFile The file being scanned. * @param int $stackPtr The position of the current token in the stack passed in $tokens. * - * @return void + * @return int */ public function process(File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); - $content = $tokens[$stackPtr]['content']; - $matches = array(); - // @Todo pouvoir personaliser la regexp - if (preg_match('/(?:\A|[^\p{L}]+)todo([^\p{L}]+(.*)|\Z)/ui', $content, $matches)) { + $foundComments = []; + $stackEnable = false; + $blockCommentStack = []; + foreach($tokens as $token) { + //search inline comment + if ($token['code'] === T_COMMENT && !$stackEnable) { + $foundComments[] = $token['content']; + } + + + //search block comments + if ($token['code'] === T_DOC_COMMENT_OPEN_TAG) { + $stackEnable = true; + continue; + } + + if ($token['code'] === T_DOC_COMMENT_CLOSE_TAG) { + $foundComments[] = join('', $blockCommentStack); + $blockCommentStack = []; + $stackEnable = false; + } + + if ($stackEnable) { + $blockCommentStack[] = $token['content']; + } + } + + foreach ($foundComments as $foundComment) { + $this->handleComment($phpcsFile, $foundComment); + } + + return count($tokens) + 1; + } + + private function handleComment(File $phpcsFile, $foundComment) + { + if (preg_match('/(?:\A|[^\p{L}]+)todo([^\p{L}]+(.*)|\Z)/ui', $foundComment, $matches)) { if (! preg_match('/WWW([a-z])*-([0-9])*/ui', $matches[1], $jiramatches)) { - // Clear whitespace and some common characters not required at - // the end of a to-do message to make the warning more informative. + // Clear whitespace and some common characters not required at + // the end of a to-do message to make the warning more informative. $type = 'CommentFound'; $todoMessage = trim($matches[1]); @@ -88,9 +121,10 @@ public function process(File $phpcsFile, $stackPtr) $error .= ' "%s"'; } - $phpcsFile->addWarning($error, $stackPtr, $type, $data); + //the stack ptr is invalid so hardcoded to 0 for the moment + $phpcsFile->addWarning($error, 0, $type, $data); } } - }//end process() + } }//end class From 2e2faf738cff962f49242eb0c0064a2f9d4c1b53 Mon Sep 17 00:00:00 2001 From: lgnap Date: Wed, 4 Oct 2017 16:41:33 +0200 Subject: [PATCH 10/13] Write some phpunit tests to check TodoSniff --- tests/Kamelot/TodoSniffTest.php | 70 +++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 tests/Kamelot/TodoSniffTest.php diff --git a/tests/Kamelot/TodoSniffTest.php b/tests/Kamelot/TodoSniffTest.php new file mode 100644 index 0000000..9c68d50 --- /dev/null +++ b/tests/Kamelot/TodoSniffTest.php @@ -0,0 +1,70 @@ + + * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600) + * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence + */ + +namespace PHP_CodeSniffer\Tests\Core\File; + +use PHP_CodeSniffer\Config; +use PHP_CodeSniffer\Ruleset; +use PHP_CodeSniffer\Files\DummyFile; +use PHPUnit\Framework\TestCase; + +class TodoSniffTest extends TestCase +{ + + /** + * @var \PHP_CodeSniffer\Files\File + */ + private $phpcsFile; + /** + * @var Config + */ + private $config; + /** + * @var Ruleset + */ + private $ruleset; + + public function setUp() + { + $this->config = new Config(); + $this->config->standards = ['./src/standard/Kamelot/ruleset.xml']; + + $this->ruleset = new Ruleset($this->config); + } + + public function tearDown() + { + unset($this->phpcsFile); + } + + + public function testErrors() + { + + $pathToTestFile = __DIR__ . DIRECTORY_SEPARATOR . 'errors' . DIRECTORY_SEPARATOR . 'comments.php'; + + $this->phpcsFile = new DummyFile(file_get_contents($pathToTestFile), $this->ruleset, $this->config); + $this->phpcsFile->process(); + + self::assertEquals(7, $this->phpcsFile->getWarningCount()); + self::assertArrayHasKey(1, $this->phpcsFile->getWarnings()); + //the document has in fact 9 errors. The issue it the second to do in a bloc comment + //The numbering of lines is invalid also + } + + public function testValids() + { + $pathToTestFile = __DIR__ . DIRECTORY_SEPARATOR . 'ok' . DIRECTORY_SEPARATOR . 'comments.php'; + + $this->phpcsFile = new DummyFile(file_get_contents($pathToTestFile), $this->ruleset, $this->config); + $this->phpcsFile->process(); + + self::assertEquals(0, $this->phpcsFile->getWarningCount()); + } +}//end class From 625771fe6afe6e7f440f4cf6788cfe09b496008b Mon Sep 17 00:00:00 2001 From: lgnap Date: Wed, 4 Oct 2017 16:41:56 +0200 Subject: [PATCH 11/13] Added: some tests case --- tests/Kamelot/errors/comments.php | 9 +++++++-- tests/Kamelot/ok/comments.php | 14 +++++++++++--- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/tests/Kamelot/errors/comments.php b/tests/Kamelot/errors/comments.php index 2c80402..7513c10 100644 --- a/tests/Kamelot/errors/comments.php +++ b/tests/Kamelot/errors/comments.php @@ -4,8 +4,8 @@ // @TODO je suis un commentaire inline uppercase -/** @Todo je suis un commentaire inline camelcase */ -/** @TODO je suis un commentaire inline uppercase*/ +/** @Todo je suis un commentaire inline in block camelcase */ +/** @TODO je suis un commentaire inline in block uppercase*/ /** * @Todo je suis un commentaire dans un block camelcase @@ -19,3 +19,8 @@ @TODO je suis un commentaire dans un block uppercase */ +/** +je suis un commentaire sans to do + +TODO je suis un commentaire impliquant un travail sans arobase + */ diff --git a/tests/Kamelot/ok/comments.php b/tests/Kamelot/ok/comments.php index edea874..1f13332 100644 --- a/tests/Kamelot/ok/comments.php +++ b/tests/Kamelot/ok/comments.php @@ -7,9 +7,9 @@ // @TODO je suis un WWWAAA-123 commentaire inline uppercase /** je suis un commentaire sans to do */ -/** @TODO je suis un commentaire inline uppercase WWWAAA-123 */ -/** @TODO WWWAAA-123 je suis un commentaire inline uppercase */ -/** @TODO je suis un commentaire WWWAAA-123 inline uppercase */ +/** @TODO je suis un commentaire inline dans un block uppercase WWWAAA-123 */ +/** @TODO WWWAAA-123 je suis un commentaire inline dans un block uppercase */ +/** @TODO je suis un commentaire WWWAAA-123 inline dans un block uppercase */ /** * je suis un commentaire sans to do @@ -24,3 +24,11 @@ @TODO je suis un commentaire dans un block uppercase WWWAAA-123 */ + + +/** + * @TODO je suis un commentaire dans un block uppercase WWWAAA-123 + * + * @TODO je suis un commentaire dans un block uppercase WWWAAA-123 + * + */ \ No newline at end of file From 32f573d9af3abe45912378981982561f61e8694b Mon Sep 17 00:00:00 2001 From: lgnap Date: Wed, 4 Oct 2017 16:54:46 +0200 Subject: [PATCH 12/13] Get back the line number --- .../Kamelot/Sniffs/Commenting/TodoSniff.php | 25 +++++++++++-------- tests/Kamelot/TodoSniffTest.php | 12 ++++++--- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/src/standard/Kamelot/Sniffs/Commenting/TodoSniff.php b/src/standard/Kamelot/Sniffs/Commenting/TodoSniff.php index 8e7e970..d101a50 100644 --- a/src/standard/Kamelot/Sniffs/Commenting/TodoSniff.php +++ b/src/standard/Kamelot/Sniffs/Commenting/TodoSniff.php @@ -74,37 +74,41 @@ public function process(File $phpcsFile, $stackPtr) $stackEnable = false; $blockCommentStack = []; foreach($tokens as $token) { + $code = $token['code']; + $line = $token['line']; + $content = $token['content']; + //search inline comment - if ($token['code'] === T_COMMENT && !$stackEnable) { - $foundComments[] = $token['content']; + if ($code === T_COMMENT && !$stackEnable) { + $foundComments[$line] = $content; } //search block comments - if ($token['code'] === T_DOC_COMMENT_OPEN_TAG) { + if ($code === T_DOC_COMMENT_OPEN_TAG) { $stackEnable = true; continue; } - if ($token['code'] === T_DOC_COMMENT_CLOSE_TAG) { - $foundComments[] = join('', $blockCommentStack); + if ($code === T_DOC_COMMENT_CLOSE_TAG) { + $foundComments[$line] = join('', $blockCommentStack); $blockCommentStack = []; $stackEnable = false; } if ($stackEnable) { - $blockCommentStack[] = $token['content']; + $blockCommentStack[] = $content; } } - foreach ($foundComments as $foundComment) { - $this->handleComment($phpcsFile, $foundComment); + foreach ($foundComments as $lineNumber => $foundComment) { + $this->handleComment($phpcsFile, $lineNumber - 1, $foundComment); } return count($tokens) + 1; } - private function handleComment(File $phpcsFile, $foundComment) + private function handleComment(File $phpcsFile, $lineNumber, $foundComment) { if (preg_match('/(?:\A|[^\p{L}]+)todo([^\p{L}]+(.*)|\Z)/ui', $foundComment, $matches)) { if (! preg_match('/WWW([a-z])*-([0-9])*/ui', $matches[1], $jiramatches)) { @@ -121,8 +125,7 @@ private function handleComment(File $phpcsFile, $foundComment) $error .= ' "%s"'; } - //the stack ptr is invalid so hardcoded to 0 for the moment - $phpcsFile->addWarning($error, 0, $type, $data); + $phpcsFile->addWarning($error, $lineNumber, $type, $data); } } diff --git a/tests/Kamelot/TodoSniffTest.php b/tests/Kamelot/TodoSniffTest.php index 9c68d50..fc3d167 100644 --- a/tests/Kamelot/TodoSniffTest.php +++ b/tests/Kamelot/TodoSniffTest.php @@ -53,9 +53,15 @@ public function testErrors() $this->phpcsFile->process(); self::assertEquals(7, $this->phpcsFile->getWarningCount()); - self::assertArrayHasKey(1, $this->phpcsFile->getWarnings()); - //the document has in fact 9 errors. The issue it the second to do in a bloc comment - //The numbering of lines is invalid also + //the document has in fact 9 errors. The algo must be improved + + self::assertArrayHasKey(3, $this->phpcsFile->getWarnings()); + self::assertArrayHasKey(4, $this->phpcsFile->getWarnings()); + self::assertArrayHasKey(7, $this->phpcsFile->getWarnings()); + self::assertArrayHasKey(8, $this->phpcsFile->getWarnings()); + self::assertArrayHasKey(11, $this->phpcsFile->getWarnings()); + // only 5 keys (lines number) are found + } public function testValids() From a2b0e9c995df65afd5e01b50c6ceb3d5844792c9 Mon Sep 17 00:00:00 2001 From: lgnap Date: Wed, 4 Oct 2017 17:00:25 +0200 Subject: [PATCH 13/13] Adapt README to use phpunit tests --- README.md | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 5712d84..89b21ca 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ vendor/bin/phpcs --standard=standard/Kamelot/ruleset.xml --extensions=php \ ## Testing code -Run commands +### Run commands Checking if everything is wrong :-) @@ -35,6 +35,15 @@ vendor/bin/phpcs --standard=standard/Kamelot/ruleset.xml \ --extensions=php --encoding=utf-8 -n tests/Kamelot/ok ``` +### PhpUnit +You can do that through phpunit now + +```bash +vendor/bin/phpunit +``` + + + ## Installation