33 * Copyright © Magento, Inc. All rights reserved.
44 * See COPYING.txt for license details.
55 */
6+ declare (strict_types = 1 );
67
78namespace Magento2 \Sniffs \Legacy ;
89
@@ -20,7 +21,6 @@ class LayoutSniff implements Sniff
2021 private const ERROR_CODE_NOT_ALLOWED = 'NotAllowed ' ;
2122 private const ERROR_CODE_OBSOLETE = 'Obsolete ' ;
2223 private const ERROR_CODE_OBSOLETE_CLASS = 'ObsoleteClass ' ;
23- private const ERROR_CODE_ATTRIBUTE_NOT_VALID = 'AttributeNotValid ' ;
2424 private const ERROR_CODE_METHOD_NOT_ALLOWED = 'MethodNotAllowed ' ;
2525 private const ERROR_CODE_HELPER_ATTRIBUTE_CHARACTER_NOT_ALLOWED = 'CharacterNotAllowedInAttribute ' ;
2626 private const ERROR_CODE_HELPER_ATTRIBUTE_CHARACTER_EXPECTED = 'CharacterExpectedInAttribute ' ;
@@ -229,7 +229,6 @@ public function process(File $phpcsFile, $stackPtr)
229229 }
230230
231231 $ this ->testObsoleteReferences ($ layout , $ phpcsFile );
232- $ this ->testObsoleteAttributes ($ layout , $ phpcsFile );
233232 $ this ->testHeadBlocks ($ layout , $ phpcsFile );
234233 $ this ->testOutputAttribute ($ layout , $ phpcsFile );
235234 $ this ->testHelperAttribute ($ layout , $ phpcsFile );
@@ -240,21 +239,21 @@ public function process(File $phpcsFile, $stackPtr)
240239 /**
241240 * Check for obsolete block references
242241 *
243- * @todo missing test
244242 * @param SimpleXMLElement $layout
245243 * @param File $phpcsFile
246244 */
247245 private function testObsoleteReferences (SimpleXMLElement $ layout , File $ phpcsFile ): void
248246 {
249247 foreach ($ layout as $ handle ) {
250- if (!isset ($ this ->obsoleteReferences [$ handle ->getName ()])) {
248+ $ attributes = $ handle ->attributes ();
249+ if (!isset ($ this ->obsoleteReferences [(string )$ attributes ->handle ])) {
251250 continue ;
252251 }
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 ) {
255254 $ phpcsFile ->addError (
256255 'The block being referenced is removed. ' ,
257- dom_import_simplexml ($ reference )->getLineNo (),
256+ dom_import_simplexml ($ reference )->getLineNo ()- 1 ,
258257 self ::ERROR_CODE_OBSOLETE
259258 );
260259 }
@@ -317,45 +316,12 @@ private function testOutputAttribute(SimpleXMLElement $layout, File $phpcsFile):
317316 if (!empty ($ elements )) {
318317 $ phpcsFile ->addError (
319318 'output="toHtml" is obsolete. Use output="1" ' ,
320- dom_import_simplexml ($ elements [0 ])->getLineNo (),
319+ dom_import_simplexml ($ elements [0 ])->getLineNo ()- 1 ,
321320 self ::ERROR_CODE_OBSOLETE
322321 );
323322 };
324323 }
325324
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-
359325 /**
360326 * Returns attribute value by attribute name
361327 *
@@ -381,14 +347,14 @@ private function testHelperAttribute(SimpleXMLElement $layout, File $phpcsFile):
381347 if (strpos ($ this ->getAttribute ($ action , 'helper ' ), '/ ' ) !== false ) {
382348 $ phpcsFile ->addError (
383349 "'helper' attribute contains '/' " ,
384- dom_import_simplexml ($ action )->getLineNo (),
350+ dom_import_simplexml ($ action )->getLineNo ()- 1 ,
385351 self ::ERROR_CODE_HELPER_ATTRIBUTE_CHARACTER_NOT_ALLOWED
386352 );
387353 }
388354 if (strpos ($ this ->getAttribute ($ action , 'helper ' ), ':: ' ) === false ) {
389355 $ phpcsFile ->addError (
390356 "'helper' attribute does not contain '::' " ,
391- dom_import_simplexml ($ action )->getLineNo (),
357+ dom_import_simplexml ($ action )->getLineNo ()- 1 ,
392358 self ::ERROR_CODE_HELPER_ATTRIBUTE_CHARACTER_EXPECTED
393359 );
394360 }
@@ -408,7 +374,7 @@ private function testListText(SimpleXMLElement $layout, File $phpcsFile): void
408374 $ phpcsFile ->addError (
409375 'The class \Magento\Framework\View\Element\Text\ListText ' .
410376 ' is not supposed to be used in layout anymore. ' ,
411- dom_import_simplexml ($ elements [0 ])->getLineNo (),
377+ dom_import_simplexml ($ elements [0 ])->getLineNo ()- 1 ,
412378 self ::ERROR_CODE_OBSOLETE_CLASS
413379 );
414380 };
@@ -430,7 +396,7 @@ private function testActionNodeMethods(SimpleXMLElement $layout, File $phpcsFile
430396 'Call of method "%s" via layout instruction <action> is not allowed. ' ,
431397 $ attributes ['method ' ]
432398 ),
433- dom_import_simplexml ($ node )->getLineNo (),
399+ dom_import_simplexml ($ node )->getLineNo ()- 1 ,
434400 self ::ERROR_CODE_METHOD_NOT_ALLOWED
435401 );
436402 }
0 commit comments