Get indexes of methods and properties in classy code (classes, interfaces and traits).
public getClassyElements ( ) : array[] | ||
return | array[] |
/** * {@inheritdoc} */ public function fix(\SplFileInfo $file, Tokens $tokens) { $analyzer = new TokensAnalyzer($tokens); $elements = array_reverse($analyzer->getClassyElements(), true); foreach ($elements as $index => $element) { if (!in_array($element['type'], $this->configuration, true)) { continue; // not in configuration } $this->fixElement($tokens, $index); } }
public function testGetClassyElements() { $source = <<<'PHP' <?php class Foo { public $prop0; protected $prop1; private $prop2 = 1; var $prop3 = array(1,2,3); const CONSTANT = 'constant value'; public function bar4() { $a = 5; return " ({$a})"; } public function bar5($data) { $message = $data; $example = function ($arg) use ($message) { echo $arg . ' ' . $message; }; $example('hello'); } } function test(){} class Foo2 { const CONSTANT = 'constant value'; } PHP; $tokens = Tokens::fromCode($source); $tokensAnalyzer = new TokensAnalyzer($tokens); $elements = array_values($tokensAnalyzer->getClassyElements()); $this->assertCount(8, $elements); $this->assertSame('property', $elements[0]['type']); $this->assertSame('property', $elements[1]['type']); $this->assertSame('property', $elements[2]['type']); $this->assertSame('property', $elements[3]['type']); $this->assertSame('const', $elements[4]['type']); $this->assertSame('method', $elements[5]['type']); $this->assertSame('method', $elements[6]['type']); $this->assertSame('const', $elements[7]['type']); }
/** * {@inheritdoc} */ public function fix(\SplFileInfo $file, Tokens $tokens) { $tokensAnalyzer = new TokensAnalyzer($tokens); $elements = $tokensAnalyzer->getClassyElements(); foreach (array_reverse($elements, true) as $index => $element) { if ('method' === $element['type']) { $this->overrideAttribs($tokens, $index, $this->grabAttribsBeforeMethodToken($tokens, $index)); // force whitespace between function keyword and function name to be single space char $afterToken = $tokens[++$index]; if ($afterToken->isWhitespace()) { $afterToken->setContent(' '); } } elseif ('property' === $element['type']) { $prevIndex = $tokens->getPrevTokenOfKind($index, array(';', ',', '{')); if (!$prevIndex || !$tokens[$prevIndex]->equals(',')) { $this->overrideAttribs($tokens, $index, $this->grabAttribsBeforePropertyToken($tokens, $index)); } } } }
/** * {@inheritdoc} */ public function fix(\SplFileInfo $file, Tokens $tokens) { $tokensAnalyzer = new TokensAnalyzer($tokens); $elements = $tokensAnalyzer->getClassyElements(); foreach (array_reverse($elements, true) as $index => $element) { if (!in_array($element['type'], $this->configuration, true)) { continue; } switch ($element['type']) { case 'method': $this->fixMethodVisibility($tokens, $index); break; case 'property': $this->fixPropertyVisibility($tokens, $index); break; case 'const': $this->fixConstVisibility($tokens, $index); break; } } }