Skip to content

Commit

Permalink
Merge pull request #132 from andrewnicols/multiline_attributes
Browse files Browse the repository at this point in the history
Fix docblocks before Multiline attributes
  • Loading branch information
andrewnicols authored Mar 23, 2024
2 parents 3bfeb70 + 38a3204 commit e514246
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 2 deletions.
31 changes: 31 additions & 0 deletions moodle/Tests/Sniffs/Commenting/MissingDocblockSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,23 @@ public static function docblockCorrectnessProvider(): array {
'fixtureFilename' => null,
'errors' => [
13 => 'Missing docblock for class class_only_with_attributes_incorrect_whitespace',
20 => 'Missing docblock for function method_only_with_attributes_incorrect_whitespace',
],
'warnings' => [],
],
'Interface only with attributes and incorrect whitespace' => [
'fixture' => 'interface_only_with_attributes_incorrect_whitespace',
'fixtureFilename' => null,
'errors' => [
13 => 'Missing docblock for interface interface_only_with_attributes_incorrect_whitespace',
],
'warnings' => [],
],
'Trait only with attributes and incorrect whitespace' => [
'fixture' => 'trait_only_with_attributes_incorrect_whitespace',
'fixtureFilename' => null,
'errors' => [
13 => 'Missing docblock for trait trait_only_with_attributes_incorrect_whitespace',
],
'warnings' => [],
],
Expand Down Expand Up @@ -142,6 +159,20 @@ public static function docblockCorrectnessProvider(): array {
],
];

if (version_compare(PHP_VERSION, '8.0.0') >= 0) {
$cases['Multiline attributes'] = [
'fixture' => 'docblock_with_multiline_attributes',
'fixtureFilename' => null,
'errors' => [
59 => 'Missing docblock for class class_multiline_attribute_space_between',
69 => 'Missing docblock for function method_multiline_attribute_space_between',
81 => 'Missing docblock for interface interface_multiline_attribute_space_between',
92 => 'Missing docblock for trait trait_multiline_attribute_space_between',
],
'warnings' => [],
];
}

if (version_compare(PHP_VERSION, '8.1.0') >= 0) {
$cases['Enum only (correct)'] = [
'fixture' => 'enum_only',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,12 @@
#[with_multiple_attributes, and_another_attribute]

class class_only_with_attributes_incorrect_whitespace {
/**
* Method level docblock.
*/
#[example_attribute]
#[with_multiple_attributes, and_another_attribute]

function method_only_with_attributes_incorrect_whitespace(): void {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

/**
* This file contains multiple testcases for multi line attributes.
*/

namespace MoodleHQ\MoodleCS\moodle\Tests\Sniffs\PHPUnit;

defined('MOODLE_INTERNAL') || die(); // Make this always the 1st line in all CS fixtures.

/**
* Example class.
*/
#[someattribute(
attr1: 'asdf',
attr2: 'asdf',
)]
class class_multiline_attribute {

/**
* Method attribute.
*/
#[someattribute(
attr1: 'asdf',
attr2: 'asdf',
)]
function method_multiline_attribute(): void {
}
}

/**
* Interface with multiline attributes.
*/
#[someattribute(
attr1: 'asdf',
attr2: 'asdf',
)]
interface interface_multiline_attribute {
}

/**
* Trait with multiline attributes.
*/
#[someattribute(
attr1: 'asdf',
attr2: 'asdf',
)]
trait trait_multiline_attribute {
}

/**
* Example class.
*/

#[someattribute(
attr1: 'asdf',
attr2: 'asdf',
)]
class class_multiline_attribute_space_between {

/**
* Method attribute.
*/

#[someattribute(
attr1: 'asdf',
attr2: 'asdf',
)]
function method_multiline_attribute_space_between(): void {
}
}

/**
* Interface with multiline attributes.
*/

#[someattribute(
attr1: 'asdf',
attr2: 'asdf',
)]
interface interface_multiline_attribute_space_between {
}

/**
* Trait with multiline attributes and space between.
*/

#[someattribute(
attr1: 'asdf',
attr2: 'asdf',
)]
trait trait_multiline_attribute_space_between {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace MoodleHQ\MoodleCS\moodle\Tests\Sniffs\PHPUnit;

use example;

/**
* Interface level docblock.
*/
#[example_attribute]
#[with_multiple_attributes, and_another_attribute]

interface interface_only_with_attributes_incorrect_whitespace {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace MoodleHQ\MoodleCS\moodle\Tests\Sniffs\PHPUnit;

use example;

/**
* Trait level docblock.
*/
#[example_attribute]
#[with_multiple_attributes, and_another_attribute]

trait trait_only_with_attributes_incorrect_whitespace {
}
4 changes: 2 additions & 2 deletions moodle/Util/Docblocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,12 @@ public static function getDocBlockPointer(

if ($token['code'] === T_ATTRIBUTE_END && isset($token['attribute_opener'])) {
$commentEnd = $token['attribute_opener'];
$pointerLine = $token['line'];
$pointerLine = $tokens[$commentEnd]['line'];
continue;
}

if ($token['line'] < ($pointerLine - 1)) {
// The comment msut be on the line immediately before the pointer, or immediately before the attribute. z
// The comment must be on the line immediately before the pointer, or immediately before the attribute. z
return null;
}

Expand Down

0 comments on commit e514246

Please sign in to comment.