Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/develop' into deprecated-versi…
Browse files Browse the repository at this point in the history
…on-range
  • Loading branch information
fredden committed Sep 26, 2023
2 parents a957d87 + d1711ba commit 6b3b7a5
Show file tree
Hide file tree
Showing 15 changed files with 784 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,29 @@ public function process(File $phpcsFile, $stackPtr)
return;
}

$commentCloserPtr = $tokens[$commentStartPtr]['comment_closer'];

if ($this->PHPDocFormattingValidator->providesMeaning($namePtr, $commentStartPtr, $tokens) !== true) {
$phpcsFile->addWarning(
$fix = $phpcsFile->addFixableWarning(
sprintf(
'%s description must contain meaningful information beyond what its name provides or be removed.',
ucfirst($tokens[$stackPtr]['content'])
),
$stackPtr,
'InvalidDescription'
);

if ($fix) {
for ($i = $commentStartPtr; $i <= $commentCloserPtr; $i++) {
$phpcsFile->fixer->replaceToken($i, '');
}

if ($tokens[$commentStartPtr - 1]['code'] === T_WHITESPACE
&& $tokens[$commentCloserPtr + 1]['code'] === T_WHITESPACE
) {
$phpcsFile->fixer->replaceToken($commentCloserPtr + 1, '');
}
}
}

if ($this->PHPDocFormattingValidator->hasDeprecatedWellFormatted($commentStartPtr, $tokens) !== true) {
Expand Down Expand Up @@ -105,11 +119,35 @@ private function validateTags(File $phpcsFile, $commentStartPtr, $tokens)
}

if (in_array($tokens[$i]['content'], $this->forbiddenTags) === true) {
$phpcsFile->addWarning(
$fix = $phpcsFile->addFixableWarning(
sprintf('Tag %s MUST NOT be used.', $tokens[$i]['content']),
$i,
'ForbiddenTags'
);

if ($fix) {
for ($j = $i - 1; $j > $commentStartPtr; $j--) {
if (!in_array($tokens[$j]['code'], [T_DOC_COMMENT_STAR, T_DOC_COMMENT_WHITESPACE], true)) {
break;
}

if ($tokens[$j]['code'] === T_DOC_COMMENT_WHITESPACE && $tokens[$j]['content'] === "\n") {
break;
}

$phpcsFile->fixer->replaceToken($j, '');
}

$phpcsFile->fixer->replaceToken($i, '');

for ($j = $i + 1; $j < $commentCloserPtr; $j++) {
$phpcsFile->fixer->replaceToken($j, '');

if ($tokens[$j]['code'] === T_DOC_COMMENT_WHITESPACE && $tokens[$j]['content'] === "\n") {
break;
}
}
}
}
}

Expand Down
106 changes: 106 additions & 0 deletions Magento2/Sniffs/Legacy/EscapeMethodsOnBlockClassSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento2\Sniffs\Legacy;

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;

class EscapeMethodsOnBlockClassSniff implements Sniff
{
private const ESCAPER_METHODS = [
'escapeCss' => true,
'escapeHtml' => true,
'escapeHtmlAttr' => true,
'escapeJs' => true,
'escapeJsQuote' => true,
'escapeQuote' => true,
'escapeUrl' => true,
'escapeXssInUrl' => true,
];

/**
* @inheritDoc
*/
public function register()
{
return [
T_OBJECT_OPERATOR,
];
}

/**
* @inheritDoc
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();

if ($stackPtr <= 1 || !isset($tokens[$stackPtr + 2])) {
return;
}

$objectPtr = $stackPtr - 1;
if ($tokens[$objectPtr]['code'] !== T_VARIABLE) {
$objectPtr = $phpcsFile->findPrevious(Tokens::$emptyTokens, $objectPtr, null, true);

if (!$objectPtr) {
return;
}
}

if ($tokens[$objectPtr]['code'] !== T_VARIABLE
|| $tokens[$objectPtr]['content'] !== '$block'
) {
return;
}

$methodPtr = $stackPtr + 1;
if ($tokens[$methodPtr]['code'] !== T_STRING) {
$methodPtr = $phpcsFile->findNext(Tokens::$emptyTokens, $methodPtr, null, true);

if (!$methodPtr) {
return;
}
}

if ($tokens[$methodPtr]['code'] !== T_STRING
|| !isset(self::ESCAPER_METHODS[$tokens[$methodPtr]['content']])
) {
return;
}

$openParenPtr = $methodPtr + 1;
if ($tokens[$openParenPtr]['code'] !== T_OPEN_PARENTHESIS) {
$openParenPtr = $phpcsFile->findNext(Tokens::$emptyTokens, $openParenPtr, null, true);

if (!$openParenPtr) {
return;
}
}

if ($tokens[$openParenPtr]['code'] !== T_OPEN_PARENTHESIS) {
return;
}

$fix = $phpcsFile->addFixableWarning(
'Using %s on $block is deprecated. Please use equivalent method on $escaper',
$methodPtr,
'Found',
[
$tokens[$methodPtr]['content'], // method name
]
);

if ($fix) {
$phpcsFile->fixer->replaceToken($objectPtr, '$escaper');
}
}
}
4 changes: 2 additions & 2 deletions Magento2/Sniffs/Security/XssTemplateSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,11 @@ public function process(File $phpcsFile, $stackPtr)
private function findSpecialAnnotation($stackPtr)
{
if ($this->tokens[$stackPtr]['code'] === T_ECHO) {
$startOfStatement = $this->file->findPrevious(T_OPEN_TAG, $stackPtr);
$startOfStatement = $this->file->findPrevious([T_OPEN_TAG, T_SEMICOLON], $stackPtr);
return $this->file->findPrevious(T_COMMENT, $stackPtr, $startOfStatement);
}
if ($this->tokens[$stackPtr]['code'] === T_OPEN_TAG_WITH_ECHO) {
$endOfStatement = $this->file->findNext(T_CLOSE_TAG, $stackPtr);
$endOfStatement = $this->file->findNext([T_CLOSE_TAG, T_SEMICOLON], $stackPtr);
return $this->file->findNext(T_COMMENT, $stackPtr, $endOfStatement);
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,11 @@ class AlsoDeprecatedButHandlerLongVersion
{

}

/**
* @package this tag should not be used
*/
class OnlyUselessCommentContent
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
<?php

/**
* Handler for PHP errors/warnings/notices that converts them to exceptions.
*/
class ErrorHandler
{

}

class NotAnErrorHandler
{

}

class FaultyHandler
{

}

class SomeHandler
{

}

class YetAnotherHandler
{

}

class GreenHandler
{

}

class EmptyHandler
{

}

/**
* Handler for PHP errors/warnings/notices that converts them to exceptions.
*
* @api is ok here
* @deprecated can be used in this context
* @see is ok here
*/
class ExampleHandler
{

}

/**
* @api
* @since 100.0.2
*/
class ApiHandler
{

}

/**
* @api
*/
class AsyncApiHandler
{

}

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class GroupRepositoryHandler
{

}

/**
* @deprecated
*/
class DeprecatedHandler
{

}

/**
* @deprecated Should not be used
*/
class AncientHandler
{

}

/**
* @deprecated
* @see
*/
class AgedHandler
{

}

/**
* @deprecated Should not be used
* @see
*/
class ArhaicHandler
{

}

/**
* @deprecated Should not be used
* @see Magento\Framework\NewHandler
*/
class OldHandler
{

}

/**
* @see Magento\Framework\NewHandler
*/
class SomethingHandler
{

}

/**
* @see
*/
class DoNotCareHandler
{

}

/**
* @deprecated
* @see Magento\Framework\NewHandler
*/
class OldHandler
{

}

/**
* @deprecated This class will be removed in version 1.0.0 without replacement
*/
class DeprecatedButHandler
{

}

/**
* @deprecated This class will be removed in version 123.45.6789 without replacement
*/
class DeprecatedButHandlerLongVersion
{

}

/**
* @deprecated It's also deprecated - This class will be removed in version 1.0.0 without replacement
*/
class AlsoDeprecatedButHandler
{

}

/**
* @deprecated It's also deprecated - This class will be removed in version 123.45.6789 without replacement
*/
class AlsoDeprecatedButHandlerLongVersion
{

}

class OnlyUselessCommentContent
{

}
Loading

0 comments on commit 6b3b7a5

Please sign in to comment.