Debian Codegraph @

vendor/wikimedia/less.php/lib/Less/Visitor.php

raw
1 <?php
2 /**
3 * @private
4 */
5 class Less_Visitor {
6
7 /** @var array */
8 protected $_visitFnCache = [];
9
10 public function __construct() {
11 $this->_visitFnCache = get_class_methods( get_class( $this ) );
12 $this->_visitFnCache = array_flip( $this->_visitFnCache );
13 }
14
15 public function visitObj( $node ) {
16 static $funcNames = [];
17
18 if ( !$node instanceof Less_Tree ) {
19 return $node;
20 }
21
22 // Map a class name like "Less_Tree_Foo_Bar" to method like "visitFooBar".
23 //
24 // We do this by taking the last part of the class name (instead of doing
25 // a find-replace from "Less_Tree" to "visit"), so that we support codemod
26 // tools (such as Strauss and Mozart), which may modify our code in-place
27 // to add a namespace or class prefix.
28 // "MyVendor\Something_Less_Tree_Foo_Bar" should also map to "FooBar".
29 //
30 // https://packagist.org/packages/brianhenryie/strauss
31 // https://packagist.org/packages/coenjacobs/mozart
32 $class = get_class( $node );
33 $funcName = $funcNames[$class] ??= 'visit' . str_replace( [ '_', '\\' ], '',
34 substr( $class, strpos( $class, 'Less_Tree_' ) + 10 )
35 );
36
37 if ( isset( $this->_visitFnCache[$funcName] ) ) {
38 $visitDeeper = true;
39 $newNode = $this->$funcName( $node, $visitDeeper );
40 if ( $this instanceof Less_VisitorReplacing ) {
41 $node = $newNode;
42 }
43
44 if ( $visitDeeper && $node instanceof Less_Tree ) {
45 $node->accept( $this );
46 }
47
48 $funcName .= 'Out';
49 if ( isset( $this->_visitFnCache[$funcName] ) ) {
50 $this->$funcName( $node );
51 }
52
53 } else {
54 $node->accept( $this );
55 }
56
57 return $node;
58 }
59
60 public function visitArray( &$nodes ) {
61 // NOTE: The use of by-ref in a normal (non-replacing) Visitor may be surprising,
62 // but upstream relies on this for Less_ImportVisitor, which modifies values of
63 // `$importParent->rules` yet is not a replacing visitor.
64 foreach ( $nodes as &$node ) {
65 $this->visitObj( $node );
66 }
67 return $nodes;
68 }
69 }