Skip to content
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

Respect --basepath configuration option in output #457

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
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 @@ -4,6 +4,7 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento2\Sniffs\Commenting;

use Magento2\Helpers\Commenting\PHPDocFormattingValidator;
Expand Down Expand Up @@ -67,12 +68,12 @@ public function process(File $phpcsFile, $stackPtr)

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

if ($fix) {
Expand Down Expand Up @@ -107,6 +108,7 @@ public function process(File $phpcsFile, $stackPtr)
* @param File $phpcsFile
* @param int $commentStartPtr
* @param array $tokens
*
* @return bool
*/
private function validateTags(File $phpcsFile, $commentStartPtr, $tokens)
Expand All @@ -120,9 +122,12 @@ private function validateTags(File $phpcsFile, $commentStartPtr, $tokens)

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

if ($fix) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php

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

declare(strict_types=1);

namespace Magento2\Sniffs\Functions;
Expand Down Expand Up @@ -71,17 +73,23 @@ public function process(File $phpcsFile, $stackPtr): void

if (self::DEPRECATED_FUNCTIONS_AND_FIXES[$functionName] === false) {
$phpcsFile->addWarning(
sprintf(self::WARNING_MESSAGE, $functionName),
self::WARNING_MESSAGE,
$stackPtr,
self::WARNING_CODE
self::WARNING_CODE,
[
$functionName,
]
);
return;
}

$fix = $phpcsFile->addFixableWarning(
sprintf(self::WARNING_MESSAGE, $functionName),
self::WARNING_MESSAGE,
$stackPtr,
self::WARNING_CODE
self::WARNING_CODE,
[
$functionName,
]
);

if ($fix === true) {
Expand Down
10 changes: 8 additions & 2 deletions Magento2/Sniffs/Html/HtmlClosingVoidTagsSniff.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php

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

declare(strict_types=1);

namespace Magento2\Sniffs\Html;
Expand Down Expand Up @@ -75,6 +77,7 @@ public function process(File $phpcsFile, $stackPtr): void
if ($stackPtr !== 0) {
return;
}

$html = $phpcsFile->getTokensAsString($stackPtr, count($phpcsFile->getTokens()));

if (empty($html)) {
Expand All @@ -85,9 +88,12 @@ public function process(File $phpcsFile, $stackPtr): void
foreach ($matches as $match) {
if (in_array($match[1], self::HTML_VOID_ELEMENTS)) {
$phpcsFile->addWarning(
sprintf(self::WARNING_MESSAGE, $match[0]),
self::WARNING_MESSAGE,
null,
self::WARNING_CODE
self::WARNING_CODE,
[
$match[0],
]
);
}
}
Expand Down
40 changes: 14 additions & 26 deletions Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php

/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types = 1);

declare(strict_types=1);

namespace Magento2\Sniffs\Legacy;

Expand All @@ -14,6 +16,8 @@

class ClassReferencesInConfigurationFilesSniff implements Sniff
{
use ParseXMLTrait;

private const ERROR_MESSAGE_CONFIG = 'Incorrect format of PHP class reference';
private const ERROR_CODE_CONFIG = 'IncorrectClassReference';
private const ERROR_MESSAGE_MODULE = 'Incorrect format of module reference';
Expand Down Expand Up @@ -43,14 +47,7 @@ public function process(File $phpcsFile, $stackPtr)
// instead, as it is the one we compare with $stackPtr later on.
$xml = simplexml_load_string($this->getFormattedXML($phpcsFile));
if ($xml === false) {
$phpcsFile->addError(
sprintf(
"Couldn't parse contents of '%s', check that they are in valid XML format",
$phpcsFile->getFilename(),
),
$stackPtr,
self::ERROR_CODE_CONFIG
);
return;
}

$classes = $this->collectClassesInConfig($xml);
Expand All @@ -72,6 +69,7 @@ private function assertNonFactoryName(File $phpcsFile, array $elements)
if (stripos($element['value'], 'Magento') === false) {
continue;
}

if (preg_match('/^([A-Z][a-z\d\\\\]+)+$/', $element['value']) !== 1) {
$phpcsFile->addError(
self::ERROR_MESSAGE_CONFIG,
Expand Down Expand Up @@ -101,24 +99,11 @@ private function assertNonFactoryNameModule(File $phpcsFile, array $classes)
}
}

/**
* Format the incoming XML to avoid tags split into several lines.
*
* @param File $phpcsFile
* @return false|string
*/
private function getFormattedXML(File $phpcsFile)
{
$doc = new DomDocument('1.0');
$doc->formatOutput = true;
$doc->loadXML($phpcsFile->getTokensAsString(0, count($phpcsFile->getTokens())));
return $doc->saveXML();
}

/**
* Parse an XML for references to PHP class names in selected tags or attributes
*
* @param SimpleXMLElement $xml
*
* @return array
*/
private function collectClassesInConfig(SimpleXMLElement $xml): array
Expand Down Expand Up @@ -166,6 +151,7 @@ function (array $extendedNode) {
*
* @param SimpleXMLElement $xml
* @param string $xPath
*
* @return array
*/
private function getValuesFromXmlTagContent(SimpleXMLElement $xml, string $xPath): array
Expand All @@ -174,7 +160,7 @@ private function getValuesFromXmlTagContent(SimpleXMLElement $xml, string $xPath
return array_map(function ($item) {
return [
'value' => (string)$item,
'lineNumber' => dom_import_simplexml($item)->getLineNo()-1,
'lineNumber' => dom_import_simplexml($item)->getLineNo() - 1,
];
}, $nodes);
}
Expand All @@ -184,6 +170,7 @@ private function getValuesFromXmlTagContent(SimpleXMLElement $xml, string $xPath
*
* @param SimpleXMLElement $xml
* @param string $xPath
*
* @return array
*/
private function getValuesFromXmlTagName(SimpleXMLElement $xml, string $xPath): array
Expand All @@ -192,7 +179,7 @@ private function getValuesFromXmlTagName(SimpleXMLElement $xml, string $xPath):
return array_map(function ($item) {
return [
'value' => $item->getName(),
'lineNumber' => dom_import_simplexml($item)->getLineNo()-1,
'lineNumber' => dom_import_simplexml($item)->getLineNo() - 1,
];
}, $nodes);
}
Expand All @@ -203,6 +190,7 @@ private function getValuesFromXmlTagName(SimpleXMLElement $xml, string $xPath):
* @param SimpleXMLElement $xml
* @param string $xPath
* @param string $attr
*
* @return array
*/
private function getValuesFromXmlTagAttribute(SimpleXMLElement $xml, string $xPath, string $attr): array
Expand All @@ -213,7 +201,7 @@ private function getValuesFromXmlTagAttribute(SimpleXMLElement $xml, string $xPa
if (isset($nodeArray['@attributes'][$attr])) {
return [
'value' => $nodeArray['@attributes'][$attr],
'lineNumber' => dom_import_simplexml($item)->getLineNo()-1,
'lineNumber' => dom_import_simplexml($item)->getLineNo() - 1,
];
}
}, $nodes);
Expand Down
15 changes: 11 additions & 4 deletions Magento2/Sniffs/Legacy/InstallUpgradeSniff.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
<?php

/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types = 1);

declare(strict_types=1);

namespace Magento2\Sniffs\Legacy;

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Common;
use SplFileInfo;

class InstallUpgradeSniff implements Sniff
Expand Down Expand Up @@ -85,7 +88,7 @@ public function process(File $phpcsFile, $stackPtr)
if ($stackPtr > 0) {
return;
}

$fileInfo = new SplFileInfo($phpcsFile->getFilename());

foreach (self::WRONG_PREFIXES as $code => $data) {
Expand All @@ -99,11 +102,15 @@ public function process(File $phpcsFile, $stackPtr)

if ($folderName === 'data' || $folderName === 'sql') {
$phpcsFile->addError(
$fileInfo->getFilename()." is in an invalid directory ".$fileInfo->getPath().":\n"
"%s is in an invalid directory %s:\n"
. "- Create a data patch within module's Setup/Patch/Data folder for data upgrades.\n"
. "- Use declarative schema approach in module's etc/db_schema.xml file for schema changes.",
0,
self::INVALID_DIRECTORIES_ERROR_CODES[$folderName]
self::INVALID_DIRECTORIES_ERROR_CODES[$folderName],
[
$fileInfo->getFilename(),
Common::stripBasepath($fileInfo->getPath(), $phpcsFile->config->basepath),
]
);
}
}
Expand Down
Loading
Loading