Skip to content

💄 Multiple improvements #78

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,7 @@ public function register()
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
if (in_array(
$tokens[$stackPtr]['content'],
$this->tags
)
) {
if (in_array($tokens[$stackPtr]['content'], $this->tags)) {
$phpcsFile->addError(
'The %s annotation is forbidden to use',
$stackPtr,
Expand Down
60 changes: 44 additions & 16 deletions SymfonyCustom/Sniffs/Commenting/FunctionCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,8 @@ public function process(File $phpcsFile, $stackPtr)
*
* @return void
*/
protected function processReturn(
File $phpcsFile,
$stackPtr,
$commentStart,
$hasComment = true
) {
protected function processReturn(File $phpcsFile, $stackPtr, $commentStart, $hasComment = true)
{
// Check for inheritDoc if there is comment
if ($hasComment && $this->isInheritDoc($phpcsFile, $stackPtr)) {
return;
Expand Down Expand Up @@ -166,16 +162,51 @@ protected function processReturn(
}
}

/**
* @param File $phpcsFile
* @param int $stackPtr
* @param int $commentStart
*/
protected function processThrows(File $phpcsFile, $stackPtr, $commentStart)
{
$tokens = $phpcsFile->getTokens();

$throw = null;
foreach ($tokens[$commentStart]['comment_tags'] as $tag) {
if ('@throws' === $tokens[$tag]['content']) {
if (null !== $throw) {
$error = 'Only 1 @throws tag is allowed in a function comment';
$phpcsFile->addError($error, $tag, 'DuplicateThrow');

return;
}

$throw = $tag;
}
}

if (null !== $throw) {
$exception = null;
if (T_DOC_COMMENT_STRING === $tokens[($throw + 2)]['code']) {
$matches = [];
preg_match('/([^\s]+)(?:\s+(.*))?/', $tokens[($throw + 2)]['content'], $matches);
$exception = $matches[1];
}

if (null === $exception) {
$error = 'Exception type missing for @throws tag in function comment';
$phpcsFile->addError($error, $throw, 'InvalidThrows');
}
}
}

/**
* @param File $phpcsFile
* @param int $commentStart
* @param bool $hasComment
*/
protected function processWhitespace(
File $phpcsFile,
$commentStart,
$hasComment = true
) {
protected function processWhitespace(File $phpcsFile, $commentStart, $hasComment = true)
{
$tokens = $phpcsFile->getTokens();
$before = $phpcsFile->findPrevious(T_WHITESPACE, ($commentStart - 1), null, true);

Expand Down Expand Up @@ -249,11 +280,8 @@ protected function isInheritDoc(File $phpcsFile, $stackPtr)
*
* @return void
*/
protected function processParams(
File $phpcsFile,
$stackPtr,
$commentStart
) {
protected function processParams(File $phpcsFile, $stackPtr, $commentStart)
{
if ($this->isInheritDoc($phpcsFile, $stackPtr)) {
return;
}
Expand Down
4 changes: 2 additions & 2 deletions SymfonyCustom/Sniffs/NamingConventions/ValidFileNameSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public function process(File $phpcsFile, $stackPtr)
return;
}

$filenamePhp = str_replace('_', '', basename($filename, '.php'));
$filenameInc = str_replace('_', '', basename($filename, '.inc'));
$filenamePhp = basename($filename, '.php');
$filenameInc = basename($filename, '.inc');

if (strlen($filenameInc) < strlen($filenamePhp)) {
$filename = $filenameInc;
Expand Down
25 changes: 25 additions & 0 deletions SymfonyCustom/Tests/Commenting/FunctionCommentUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,28 @@ function toIgnore2()
{
$test = 42;
}

/**
* @throws Exception
*/
function throwTest()
{
throw new Exception();
}

/**
* @throws Exception|RuntimeException
*/
function throwTest2()
{
throw new Exception();
}

/**
* @throws Exception
* @throws RuntimeException
*/
function throwTest3()
{
throw new Exception();
}
25 changes: 25 additions & 0 deletions SymfonyCustom/Tests/Commenting/FunctionCommentUnitTest.inc.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,28 @@ function toIgnore2()
{
$test = 42;
}

/**
* @throws Exception
*/
function throwTest()
{
throw new Exception();
}

/**
* @throws Exception|RuntimeException
*/
function throwTest2()
{
throw new Exception();
}

/**
* @throws Exception
* @throws RuntimeException
*/
function throwTest3()
{
throw new Exception();
}
1 change: 1 addition & 0 deletions SymfonyCustom/Tests/Commenting/FunctionCommentUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function getErrorList()
83 => 1,
93 => 1,
102 => 1,
125 => 1,
];
}

Expand Down
52 changes: 27 additions & 25 deletions SymfonyCustom/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,34 +39,19 @@
</properties>
</rule>

<!-- **************** -->
<!-- *** OTHERS *** -->
<!-- **************** -->
<!-- ************** -->
<!-- *** MORE *** -->
<!-- ************** -->

<!-- From djoos repo -->
<rule ref="Generic.PHP.DisallowShortOpenTag"/>
<rule ref="Squiz.NamingConventions.ValidVariableName">
<exclude name="Squiz.NamingConventions.ValidVariableName.PrivateNoUnderscore"/>
<exclude name="Squiz.NamingConventions.ValidVariableName.ContainsNumbers"/>
</rule>
<!-- Whitespace -->
<rule ref="Generic.Formatting.SpaceAfterCast"/>
<rule ref="Generic.WhiteSpace.SpreadOperatorSpacingAfter"/>
<rule ref="Squiz.Arrays.ArrayBracketSpacing"/>
<rule ref="Squiz.PHP.LowercasePHPFunctions"/>
<rule ref="Squiz.Scope.MemberVarScope"/>
<rule ref="PEAR.Commenting.InlineComment"/>

<!-- From endouble repo -->
<rule ref="Squiz.Strings.ConcatenationSpacing">
<properties>
<property name="ignoreNewlines" value="true" />
</properties>
</rule>
<rule ref="SymfonyCustom.Commenting.FunctionComment">
<exclude name="SymfonyCustom.Commenting.FunctionComment.MissingParamComment"/>
</rule>

<!-- Added by VincentLanglet repo -->
<!-- Whitespace -->
<rule ref="Squiz.WhiteSpace.CastSpacing"/>
<rule ref="Squiz.WhiteSpace.FunctionOpeningBraceSpace"/>
<rule ref="Squiz.WhiteSpace.LogicalOperatorSpacing"/>
Expand All @@ -82,15 +67,32 @@
</rule>
<rule ref="Squiz.WhiteSpace.SemicolonSpacing"/>

<!-- Disallow -->
<rule ref="Generic.Arrays.DisallowLongArraySyntax"/>
<rule ref="Generic.CodeAnalysis.EmptyPHPStatement"/>
<rule ref="Generic.PHP.DisallowShortOpenTag"/>
<rule ref="Generic.PHP.ForbiddenFunctions"/>
<rule ref="PEAR.Commenting.InlineComment"/>
<rule ref="Squiz.PHP.DiscouragedFunctions"/>
<rule ref="Squiz.Operators.ValidLogicalOperators"/>

<!-- Others -->
<rule ref="Squiz.Classes.ClassFileName"/>
<rule ref="Squiz.Commenting.DocCommentAlignment">
<!-- Allow to indent special annotations like @ORM\AttributeOverride -->
<exclude name="Squiz.Commenting.DocCommentAlignment.SpaceAfterStar"/>
</rule>
<rule ref="Generic.PHP.ForbiddenFunctions"/>
<rule ref="Squiz.PHP.DiscouragedFunctions"/>
<rule ref="Generic.Arrays.DisallowLongArraySyntax"/>
<rule ref="Squiz.NamingConventions.ValidVariableName">
<exclude name="Squiz.NamingConventions.ValidVariableName.PrivateNoUnderscore"/>
<exclude name="Squiz.NamingConventions.ValidVariableName.ContainsNumbers"/>
</rule>
<rule ref="Squiz.PHP.LowercasePHPFunctions"/>
<rule ref="Squiz.Scope.MemberVarScope"/>
<rule ref="Squiz.Strings.DoubleQuoteUsage">
<exclude name="Squiz.Strings.DoubleQuoteUsage.ContainsVar"/>
</rule>
<rule ref="Generic.CodeAnalysis.EmptyPHPStatement"/>

<!-- Custom -->
<rule ref="SymfonyCustom.Commenting.FunctionComment">
<exclude name="SymfonyCustom.Commenting.FunctionComment.MissingParamComment"/>
</rule>
</ruleset>
Loading