Skip to content

Commit 9834741

Browse files
committed
fix(detection): support multi-line trait use statements
Fixes issue #129: trait use statements formatted across multiple lines were not being detected, causing false "not used" warnings on their imports. - Handle multi-line trait use by continuing to read lines until ; or { is found - Same pattern used for multi-line grouped namespace imports - Add test for multi-line trait use detection
1 parent 717b867 commit 9834741

2 files changed

Lines changed: 24 additions & 3 deletions

File tree

src/core/PhpClassDetector.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,8 @@ export class PhpClassDetector {
323323
const lines = text.split('\n');
324324
let pastClassDeclaration = false;
325325

326-
for (const line of lines) {
326+
for (let i = 0; i < lines.length; i++) {
327+
const line = lines[i];
327328
if (!pastClassDeclaration) {
328329
if (/^\s*(?:abstract\s+|final\s+|readonly\s+)*(?:class|trait|interface|enum)\s+\w+/.test(line)) {
329330
pastClassDeclaration = true;
@@ -332,7 +333,19 @@ export class PhpClassDetector {
332333
}
333334

334335
if (/^\s*use\s+/.test(line) && !/^\s*use\s*\(/.test(line)) {
335-
const match = line.match(/^\s*use\s+(.+?)\s*[;{]/);
336+
// Handle multi-line trait use statements
337+
let fullLine = line;
338+
if (!line.match(/[;{]/)) {
339+
for (let j = i + 1; j < lines.length; j++) {
340+
fullLine += ' ' + lines[j].trim();
341+
if (lines[j].match(/[;{]/)) {
342+
i = j;
343+
break;
344+
}
345+
}
346+
}
347+
348+
const match = fullLine.match(/^\s*use\s+(.+?)\s*[;{]/);
336349
if (match) {
337350
const traits = match[1].split(',');
338351
for (const trait of traits) {

src/test/PhpClassDetector.test.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,14 @@ class UserController extends Controller {
377377
assert.ok(result.includes('A'));
378378
assert.ok(result.includes('B'));
379379
});
380+
381+
it('should detect multi-line trait use statements', () => {
382+
const text = `class Foo {\n use Sluggable,\n HasMedia,\n HasPreferences;\n}`;
383+
const result = detector.getFromTraitUse(text);
384+
assert.ok(result.includes('Sluggable'));
385+
assert.ok(result.includes('HasMedia'));
386+
assert.ok(result.includes('HasPreferences'));
387+
});
380388
});
381389

382390
describe('edge cases - empty and boundary inputs', () => {
@@ -892,7 +900,7 @@ class Foo {}`;
892900
assert.ok(result.includes('Bar'));
893901
});
894902

895-
it('should not detect class names in PHPDoc free-text descriptions (#124)', () => {
903+
it('should not detect class names in PHPDoc free-text descriptions', () => {
896904
const text = `<?php
897905
/**
898906
* Returns a Logger or Formatter based on the config

0 commit comments

Comments
 (0)