Skip to content
Snippets Groups Projects
Unverified Commit 10a7be4b authored by Klaus Purer's avatar Klaus Purer Committed by GitHub
Browse files

fix(ScopeClosingBrace): Exclude sniff from PHP template files and fix PHP error (#3204177)

parent 1a59890f
Branches
Tags
No related merge requests found
......@@ -103,30 +103,30 @@ class ScopeClosingBraceSniff implements Sniff
}
// Check that the closing brace is on it's own line.
$lastContent = $phpcsFile->findPrevious(
[
T_WHITESPACE,
T_INLINE_HTML,
T_OPEN_TAG,
],
($scopeEnd - 1),
$scopeStart,
true
);
for ($lastContent = ($scopeEnd - 1); $lastContent > $scopeStart; $lastContent--) {
if ($tokens[$lastContent]['code'] === T_WHITESPACE || $tokens[$lastContent]['code'] === T_OPEN_TAG) {
continue;
}
if ($tokens[$lastContent]['code'] === T_INLINE_HTML
&& ltrim($tokens[$lastContent]['content']) === ''
) {
continue;
}
break;
}
if ($tokens[$lastContent]['line'] === $tokens[$scopeEnd]['line']) {
// Only allow empty classes and methods.
if (($tokens[$tokens[$scopeEnd]['scope_condition']]['code'] !== T_CLASS
&& $tokens[$tokens[$scopeEnd]['scope_condition']]['code'] !== T_INTERFACE
&& in_array(T_CLASS, $tokens[$scopeEnd]['conditions']) === false
&& in_array(T_INTERFACE, $tokens[$scopeEnd]['conditions']) === false)
|| $tokens[$lastContent]['code'] !== T_OPEN_CURLY_BRACKET
) {
$error = 'Closing brace must be on a line by itself';
$fix = $phpcsFile->addFixableError($error, $scopeEnd, 'Line');
if ($fix === true) {
$phpcsFile->fixer->addNewlineBefore($scopeEnd);
}
if ($tokens[$lastContent]['code'] === T_OPEN_CURLY_BRACKET) {
return;
}
$error = 'Closing brace must be on a line by itself';
$fix = $phpcsFile->addFixableError($error, $scopeEnd, 'Line');
if ($fix === true) {
$phpcsFile->fixer->addNewlineBefore($scopeEnd);
}
return;
......@@ -170,7 +170,7 @@ class ScopeClosingBraceSniff implements Sniff
$fix = $phpcsFile->addFixableError($error, $scopeEnd, 'BreakIndent', $data);
}
} else {
$expectedIndent = ($startColumn - 1);
$expectedIndent = max(0, ($startColumn - 1));
if ($braceIndent !== $expectedIndent) {
$error = 'Closing brace indented incorrectly; expected %s spaces, found %s';
$data = [
......
......@@ -43,6 +43,11 @@
the HTML -->
<exclude-pattern>*.tpl.php</exclude-pattern>
</rule>
<rule ref="Drupal.WhiteSpace.ScopeClosingBrace">
<!-- Do not run this sniff on template files, as the indentation might follow
the HTML -->
<exclude-pattern>*.tpl.php</exclude-pattern>
</rule>
<rule ref="Generic.CodeAnalysis.UselessOverridingMethod" />
<rule ref="Generic.Files.ByteOrderMark" />
......
......@@ -27,3 +27,41 @@ function test2() {
*/
function test3() {
return 7; }
/**
* Defines an archiver attribute object.
*
* Plugin Namespace: Plugin\Archiver.
*
* For a working example, see \Drupal\system\Plugin\Archiver\Zip
*
* @see \Drupal\Core\Archiver\ArchiverManager
* @see \Drupal\Core\Archiver\ArchiverInterface
* @see plugin_api
* @see hook_archiver_info_alter()
*/
#[\Attribute(\Attribute::TARGET_CLASS)]
class Archiver extends Plugin {
/**
* Constructs an archiver plugin attribute object.
*
* @param string $id
* The archiver plugin ID.
* @param \Drupal\Core\StringTranslation\TranslatableMarkup|null $title
* The human-readable name of the archiver plugin.
* @param \Drupal\Core\StringTranslation\TranslatableMarkup|null $description
* The description of the archiver plugin.
* @param array $extensions
* An array of valid extensions for this archiver.
* @param class-string|null $deriver
* (optional) The deriver class.
*/
public function __construct(
public readonly string $id,
public readonly ?TranslatableMarkup $title = NULL,
public readonly ?TranslatableMarkup $description = NULL,
public readonly array $extensions = [],
public readonly ?string $deriver = NULL) {}
}
......@@ -28,3 +28,42 @@ function test2() {
function test3() {
return 7;
}
/**
* Defines an archiver attribute object.
*
* Plugin Namespace: Plugin\Archiver.
*
* For a working example, see \Drupal\system\Plugin\Archiver\Zip
*
* @see \Drupal\Core\Archiver\ArchiverManager
* @see \Drupal\Core\Archiver\ArchiverInterface
* @see plugin_api
* @see hook_archiver_info_alter()
*/
#[\Attribute(\Attribute::TARGET_CLASS)]
class Archiver extends Plugin {
/**
* Constructs an archiver plugin attribute object.
*
* @param string $id
* The archiver plugin ID.
* @param \Drupal\Core\StringTranslation\TranslatableMarkup|null $title
* The human-readable name of the archiver plugin.
* @param \Drupal\Core\StringTranslation\TranslatableMarkup|null $description
* The description of the archiver plugin.
* @param array $extensions
* An array of valid extensions for this archiver.
* @param class-string|null $deriver
* (optional) The deriver class.
*/
public function __construct(
public readonly string $id,
public readonly ?TranslatableMarkup $title = NULL,
public readonly ?TranslatableMarkup $description = NULL,
public readonly array $extensions = [],
public readonly ?string $deriver = NULL,
) {}
}
......@@ -20,11 +20,16 @@ class ScopeClosingBraceUnitTest extends CoderSniffUnitTest
*/
protected function getErrorList(string $testFile): array
{
return [
16 => 1,
23 => 1,
29 => 1,
];
switch ($testFile) {
case 'ScopeClosingBraceUnitTest.inc':
return [
16 => 1,
23 => 1,
29 => 1,
];
}
return [];
}//end getErrorList()
......@@ -46,4 +51,20 @@ class ScopeClosingBraceUnitTest extends CoderSniffUnitTest
}//end getWarningList()
/**
* Skip this test on PHP versions lower than 8 because the syntax is not allowed there.
*
* @return bool
*/
protected function shouldSkipTest()
{
if (version_compare(PHP_VERSION, '8.0.0') < 0) {
return true;
}
return false;
}//end shouldSkipTest()
}//end class
<?php
/**
* @file
* Template error test.
*
* This sniff should not run on template files, since the closing brace can be
* used with content on the line before. We just have this test case to
* demonstrate there are no fatal errors when running the fixer.
*/
?>
<table <?php if ($classes) { print 'class="'. $classes . '" '; } ?><?php print $attributes; ?>>
<?php if (!empty($title) || !empty($caption)) : ?>
<caption><?php print $caption . $title; ?></caption>
<?php endif; ?>
<?php if (!empty($header)) : ?>
<thead>
<tr>
<?php foreach ($header as $field => $label): ?>
<th <?php if ($header_classes[$field]) { print 'class="'. $header_classes[$field] . '" '; } ?> scope="col">
<?php print $label; ?>
</th>
<?php endforeach; ?>
</tr>
</thead>
<?php endif; ?>
<tbody>
<?php foreach ($rows as $row_count => $row): ?>
<tr <?php if ($row_classes[$row_count]) { print 'class="' . implode(' ', $row_classes[$row_count]) .'"'; } ?>>
<?php foreach ($row as $field => $content): ?>
<td <?php if ($field_classes[$field][$row_count]) { print 'class="'. $field_classes[$field][$row_count] . '" '; } ?><?php print drupal_attributes($field_attributes[$field][$row_count]); ?>>
<?php print $content; ?>
</td>
<?php endforeach; ?>
</tr>
<tr <?php if ($row_classes[$row_count]) { print 'class="details ' . implode(' ', $row_classes[$row_count]) .'"'; } ?>>
<td colspan="<?php print count($row); ?>">
<?php print $descriptions[$row_count]; ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php
/**
* @file
* Template error test.
*
* This sniff should not run on template files, since the closing brace can be
* used with content on the line before. We just have this test case to
* demonstrate there are no fatal errors when running the fixer.
*/
?>
<table <?php if ($classes) { print 'class="'. $classes . '" '; } ?><?php print $attributes; ?>>
<?php if (!empty($title) || !empty($caption)) : ?>
<caption><?php print $caption . $title; ?></caption>
<?php endif; ?>
<?php if (!empty($header)) : ?>
<thead>
<tr>
<?php foreach ($header as $field => $label): ?>
<th <?php if ($header_classes[$field]) { print 'class="'. $header_classes[$field] . '" '; } ?> scope="col">
<?php print $label; ?>
</th>
<?php endforeach; ?>
</tr>
</thead>
<?php endif; ?>
<tbody>
<?php foreach ($rows as $row_count => $row): ?>
<tr <?php if ($row_classes[$row_count]) { print 'class="' . implode(' ', $row_classes[$row_count]) .'"'; } ?>>
<?php foreach ($row as $field => $content): ?>
<td <?php if ($field_classes[$field][$row_count]) { print 'class="'. $field_classes[$field][$row_count] . '" '; } ?><?php print drupal_attributes($field_attributes[$field][$row_count]); ?>>
<?php print $content; ?>
</td>
<?php endforeach; ?>
</tr>
<tr <?php if ($row_classes[$row_count]) { print 'class="details ' . implode(' ', $row_classes[$row_count]) .'"'; } ?>>
<td colspan="<?php print count($row); ?>">
<?php print $descriptions[$row_count]; ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment