3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+ declare (strict_types = 1 );
6
7
7
8
namespace Magento2 \Sniffs \Legacy ;
8
9
@@ -20,7 +21,6 @@ class LayoutSniff implements Sniff
20
21
private const ERROR_CODE_NOT_ALLOWED = 'NotAllowed ' ;
21
22
private const ERROR_CODE_OBSOLETE = 'Obsolete ' ;
22
23
private const ERROR_CODE_OBSOLETE_CLASS = 'ObsoleteClass ' ;
23
- private const ERROR_CODE_ATTRIBUTE_NOT_VALID = 'AttributeNotValid ' ;
24
24
private const ERROR_CODE_METHOD_NOT_ALLOWED = 'MethodNotAllowed ' ;
25
25
private const ERROR_CODE_HELPER_ATTRIBUTE_CHARACTER_NOT_ALLOWED = 'CharacterNotAllowedInAttribute ' ;
26
26
private const ERROR_CODE_HELPER_ATTRIBUTE_CHARACTER_EXPECTED = 'CharacterExpectedInAttribute ' ;
@@ -229,7 +229,6 @@ public function process(File $phpcsFile, $stackPtr)
229
229
}
230
230
231
231
$ this ->testObsoleteReferences ($ layout , $ phpcsFile );
232
- $ this ->testObsoleteAttributes ($ layout , $ phpcsFile );
233
232
$ this ->testHeadBlocks ($ layout , $ phpcsFile );
234
233
$ this ->testOutputAttribute ($ layout , $ phpcsFile );
235
234
$ this ->testHelperAttribute ($ layout , $ phpcsFile );
@@ -240,21 +239,21 @@ public function process(File $phpcsFile, $stackPtr)
240
239
/**
241
240
* Check for obsolete block references
242
241
*
243
- * @todo missing test
244
242
* @param SimpleXMLElement $layout
245
243
* @param File $phpcsFile
246
244
*/
247
245
private function testObsoleteReferences (SimpleXMLElement $ layout , File $ phpcsFile ): void
248
246
{
249
247
foreach ($ layout as $ handle ) {
250
- if (!isset ($ this ->obsoleteReferences [$ handle ->getName ()])) {
248
+ $ attributes = $ handle ->attributes ();
249
+ if (!isset ($ this ->obsoleteReferences [(string )$ attributes ->handle ])) {
251
250
continue ;
252
251
}
253
- foreach ($ handle ->xpath ('reference ' ) as $ reference ) {
254
- if (strpos ((string )$ reference ['name ' ], $ this ->obsoleteReferences [$ handle -> getName () ]) !== false ) {
252
+ foreach ($ handle ->xpath ('// reference | //referenceContainer | //referenceBlock ' ) as $ reference ) {
253
+ if (in_array ((string )$ reference ['name ' ], $ this ->obsoleteReferences [( string ) $ attributes -> handle ]) !== false ) {
255
254
$ phpcsFile ->addError (
256
255
'The block being referenced is removed. ' ,
257
- dom_import_simplexml ($ reference )->getLineNo (),
256
+ dom_import_simplexml ($ reference )->getLineNo ()- 1 ,
258
257
self ::ERROR_CODE_OBSOLETE
259
258
);
260
259
}
@@ -317,45 +316,12 @@ private function testOutputAttribute(SimpleXMLElement $layout, File $phpcsFile):
317
316
if (!empty ($ elements )) {
318
317
$ phpcsFile ->addError (
319
318
'output="toHtml" is obsolete. Use output="1" ' ,
320
- dom_import_simplexml ($ elements [0 ])->getLineNo (),
319
+ dom_import_simplexml ($ elements [0 ])->getLineNo ()- 1 ,
321
320
self ::ERROR_CODE_OBSOLETE
322
321
);
323
322
};
324
323
}
325
324
326
- /**
327
- * Tests the attributes of the top-level Layout Node. Verifies there are no longer attributes of "parent" or "owner"
328
- *
329
- * @todo missing test
330
- * @param SimpleXMLElement $layout
331
- * @param File $phpcsFile
332
- */
333
- private function testObsoleteAttributes (SimpleXMLElement $ layout , File $ phpcsFile ): void
334
- {
335
- $ type = $ layout ['type ' ];
336
- $ parent = $ layout ['parent ' ];
337
- $ owner = $ layout ['owner ' ];
338
-
339
- if ((string )$ type === 'page ' ) {
340
- if ($ parent ) {
341
- $ phpcsFile ->addError (
342
- 'Attribute "parent" is not valid ' ,
343
- dom_import_simplexml ($ parent )->getLineNo (),
344
- self ::ERROR_CODE_ATTRIBUTE_NOT_VALID
345
- );
346
- }
347
- }
348
- if ((string )$ type === 'fragment ' ) {
349
- if ($ owner ) {
350
- $ phpcsFile ->addError (
351
- 'Attribute "owner" is not valid ' ,
352
- dom_import_simplexml ($ owner )->getLineNo (),
353
- self ::ERROR_CODE_ATTRIBUTE_NOT_VALID
354
- );
355
- }
356
- }
357
- }
358
-
359
325
/**
360
326
* Returns attribute value by attribute name
361
327
*
@@ -381,14 +347,14 @@ private function testHelperAttribute(SimpleXMLElement $layout, File $phpcsFile):
381
347
if (strpos ($ this ->getAttribute ($ action , 'helper ' ), '/ ' ) !== false ) {
382
348
$ phpcsFile ->addError (
383
349
"'helper' attribute contains '/' " ,
384
- dom_import_simplexml ($ action )->getLineNo (),
350
+ dom_import_simplexml ($ action )->getLineNo ()- 1 ,
385
351
self ::ERROR_CODE_HELPER_ATTRIBUTE_CHARACTER_NOT_ALLOWED
386
352
);
387
353
}
388
354
if (strpos ($ this ->getAttribute ($ action , 'helper ' ), ':: ' ) === false ) {
389
355
$ phpcsFile ->addError (
390
356
"'helper' attribute does not contain '::' " ,
391
- dom_import_simplexml ($ action )->getLineNo (),
357
+ dom_import_simplexml ($ action )->getLineNo ()- 1 ,
392
358
self ::ERROR_CODE_HELPER_ATTRIBUTE_CHARACTER_EXPECTED
393
359
);
394
360
}
@@ -408,7 +374,7 @@ private function testListText(SimpleXMLElement $layout, File $phpcsFile): void
408
374
$ phpcsFile ->addError (
409
375
'The class \Magento\Framework\View\Element\Text\ListText ' .
410
376
' is not supposed to be used in layout anymore. ' ,
411
- dom_import_simplexml ($ elements [0 ])->getLineNo (),
377
+ dom_import_simplexml ($ elements [0 ])->getLineNo ()- 1 ,
412
378
self ::ERROR_CODE_OBSOLETE_CLASS
413
379
);
414
380
};
@@ -430,7 +396,7 @@ private function testActionNodeMethods(SimpleXMLElement $layout, File $phpcsFile
430
396
'Call of method "%s" via layout instruction <action> is not allowed. ' ,
431
397
$ attributes ['method ' ]
432
398
),
433
- dom_import_simplexml ($ node )->getLineNo (),
399
+ dom_import_simplexml ($ node )->getLineNo ()- 1 ,
434
400
self ::ERROR_CODE_METHOD_NOT_ALLOWED
435
401
);
436
402
}
0 commit comments