Best Cucumber Common Library code snippet using AstNode.getTokenMatches
GherkinDocumentBuilder.php
Source:GherkinDocumentBuilder.php
...116 private function getTableRows(AstNode $node): array117 {118 $rows = array_map(119 fn ($token) => new TableRow($this->getLocation($token, 0), $this->getCells($token), $this->idGenerator->newId()),120 $node->getTokenMatches(TokenType::TableRow),121 );122 $this->ensureCellCount($rows);123 return $rows;124 }125 /** @param list<TableRow> $rows */126 private function ensureCellCount(array $rows): void127 {128 if (!count($rows)) {129 return;130 }131 $cellCount = count($rows[0]->cells);132 foreach ($rows as $row) {133 if (count($row->cells) !== $cellCount) {134 $location = new Location($row->location->line, $row->location->column ?? 0);135 throw new AstBuilderException('inconsistent cell count within the table', $location);136 }137 }138 }139 /**140 * @return list<TableCell>141 */142 private function getCells(TokenMatch $token): array143 {144 return array_map(145 fn ($cellItem) => new TableCell($this->getLocation($token, $cellItem->column), $cellItem->text),146 $token->items,147 );148 }149 /**150 * @return list<Tag>151 */152 private function getTags(AstNode $node): array153 {154 $tagsNode = $node->getSingle(AstNode::class, RuleType::Tags, new AstNode(RuleType::None));155 $tokens = $tagsNode->getTokenMatches(TokenType::TagLine);156 $tags = [];157 foreach ($tokens as $token) {158 foreach ($token->items as $tagItem) {159 $tags[] = new Tag(160 location: $this->getLocation($token, $tagItem->column),161 name: $tagItem->text,162 id: $this->idGenerator->newId(),163 );164 }165 }166 return $tags;167 }168 /**169 * @param array<TokenMatch> $lineTokens170 */171 private function joinMatchedTextWithLinebreaks(array $lineTokens): string172 {173 return join("\n", array_map(fn ($t) => $t->text, $lineTokens));174 }175 private function transformStepNode(AstNode $node): Step176 {177 $stepLine = $node->getTokenMatch(TokenType::StepLine);178 return new Step(179 location: $this->getLocation($stepLine, 0),180 keyword: $stepLine->keyword,181 text: $stepLine->text,182 docString: $node->getSingle(DocString::class, RuleType::DocString),183 dataTable: $node->getSingle(DataTable::class, RuleType::DataTable),184 id: $this->idGenerator->newId(),185 );186 }187 private function transformDocStringNode(AstNode $node): DocString188 {189 $separatorToken = $node->getTokenMatches(TokenType::DocStringSeparator)[0];190 $mediaType = $separatorToken->text;191 $lineTokens = $node->getTokenMatches(TokenType::Other);192 $content = $this->joinMatchedTextWithLinebreaks($lineTokens);193 return new DocString(194 location: $this->getLocation($separatorToken, 0),195 mediaType: $mediaType ?: null, // special case turns '' into null196 content: $content,197 delimiter: $separatorToken->keyword,198 );199 }200 private function transformScenarioDefinitionNode(AstNode $node): ?Scenario201 {202 $scenarioNode = $node->getSingle(AstNode::class, RuleType::Scenario);203 if (null === $scenarioNode) {204 return null;205 }206 $scenarioLine = $scenarioNode->getTokenMatch(TokenType::ScenarioLine);207 return new Scenario(208 location: $this->getLocation($scenarioLine, 0),209 tags: $this->getTags($node),210 keyword: $scenarioLine->keyword,211 name: $scenarioLine->text,212 description: $this->getDescription($scenarioNode),213 steps: $this->getSteps($scenarioNode),214 examples: $scenarioNode->getItems(Examples::class, RuleType::ExamplesDefinition),215 id: $this->idGenerator->newId(),216 );217 }218 private function transformExamplesDefinitionNode(AstNode $node): ?Examples219 {220 $examplesNode = $node->getSingle(AstNode::class, RuleType::Examples);221 if (null === $examplesNode) {222 return null;223 }224 $examplesLine = $examplesNode->getTokenMatch(TokenType::ExamplesLine);225 /** @var list<TableRow>|null $rows */226 $rows = $examplesNode->getSingleUntyped(RuleType::ExamplesTable);227 $tableHeader = is_array($rows) && count($rows) ? $rows[0] : null;228 $tableBody = (is_array($rows) && count($rows) > 0) ? array_slice($rows, 1) : [];229 return new Examples(230 location: $this->getLocation($examplesLine, 0),231 tags: $this->getTags($node),232 keyword: $examplesLine->keyword,233 name: $examplesLine->text,234 description: $this->getDescription($examplesNode),235 tableHeader: $tableHeader,236 tableBody: $tableBody,237 id: $this->idGenerator->newId(),238 );239 }240 private function transformDataTableNode(AstNode $node): DataTable241 {242 $rows = $this->getTableRows($node);243 return new DataTable($rows[0]->location, $rows);244 }245 /** @return list<TableRow> */246 private function transformExamplesTableNode(AstNode $node): array247 {248 return $this->getTableRows($node);249 }250 private function transformBackgroundNode(AstNode $node): Background251 {252 $backgroundLine = $node->getTokenMatch(TokenType::BackgroundLine);253 return new Background(254 location: $this->getLocation($backgroundLine, 0),255 keyword: $backgroundLine->keyword,256 name: $backgroundLine->text,257 description: $this->getDescription($node),258 steps: $this->getSteps($node),259 id: $this->idGenerator->newId(),260 );261 }262 private function transformDescriptionNode(AstNode $node): string263 {264 $lineTokens = $node->getTokenMatches(TokenType::Other);265 $lineText = preg_replace(266 '/(\\n\\s*)*$/u',267 '',268 $this->joinMatchedTextWithLinebreaks($lineTokens),269 );270 return $lineText;271 }272 private function transformFeatureNode(AstNode $node): ?Feature273 {274 $header = $node->getSingle(AstNode::class, RuleType::FeatureHeader, new AstNode(RuleType::FeatureHeader));275 if (!$header instanceof AstNode) {276 return null;277 }278 $tags = $this->getTags($header);...
AstNodeTest.php
Source:AstNodeTest.php
...40 self::assertSame($obj1, $item);41 }42 public function testItGetsNoTokensWhenNoneAreAdded(): void43 {44 $tokens = $this->astNode->getTokenMatches(TokenType::Empty);45 self::assertSame([], $tokens);46 }47 public function testItGetsTokensWhenTheyAreAddedByRuletype(): void48 {49 $this->astNode->add(RuleType::_Empty, $token1 = $this->getTokenMatch());50 $this->astNode->add(RuleType::_Empty, $token2 = $this->getTokenMatch());51 $tokens = $this->astNode->getTokenMatches(TokenType::Empty);52 self::assertSame([$token1, $token2], $tokens);53 }54 public function testItThrowsWhenGettingATokenThatIsNotAdded(): void55 {56 $this->expectException(\LogicException::class);57 $this->astNode->getTokenMatch(TokenType::Empty);58 }59 public function testItGetsTheFirstTokenWhenSomeAreAdded(): void60 {61 $this->astNode->add(RuleType::_Empty, $token1 = $this->getTokenMatch());62 $this->astNode->add(RuleType::_Empty, $this->getTokenMatch());63 $token = $this->astNode->getTokenMatch(TokenType::Empty);64 self::assertSame($token1, $token);65 }...
AstNode.php
Source:AstNode.php
...62 }63 /**64 * @return list<TokenMatch>65 */66 public function getTokenMatches(TokenType $tokenType): array67 {68 $items = $this->getItems(TokenMatch::class, RuleType::cast($tokenType));69 return $items;70 }71 public function getTokenMatch(TokenType $tokenType): TokenMatch72 {73 $ruleType = RuleType::cast($tokenType);74 $item = $this->getSingle(TokenMatch::class, $ruleType);75 if (!$item) {76 throw new \LogicException('Requested token type was not in stack');77 }78 return $item;79 }80}...
getTokenMatches
Using AI Code Generation
1require_once 'vendor/autoload.php';2use PhpParser\ParserFactory;3$code = file_get_contents("example.php");4$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);5$ast = $parser->parse($code);6$token = $ast[0]->stmts[0]->expr->args[0]->value->value;7$matches = $ast[0]->stmts[0]->getTokenMatches($token);8print_r($matches);9 (10 (11 (12 (13 (14 (
getTokenMatches
Using AI Code Generation
1require_once 'vendor/autoload.php';2use PhpParser\Error;3use PhpParser\Node;4use PhpParser\Node\Stmt\Function_;5use PhpParser\NodeFinder;6use PhpParser\NodeTraverser;7use PhpParser\NodeVisitorAbstract;8use PhpParser\ParserFactory;9function foo($x) {10 return $x + 1;11}12CODE;13$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);14$ast = $parser->parse($code);15$traverser = new NodeTraverser;16$traverser->addVisitor(new class extends NodeVisitorAbstract {17 public function enterNode(Node $node) {18 if ($node instanceof Function_) {19 var_dump($node->getTokens());20 }21 }22});23$traverser->traverse($ast);24array(1) {25 array(2) {26 int(0)27 int(5)28 }29}30require_once 'vendor/autoload.php';31use PhpParser\Error;32use PhpParser\Node;33use PhpParser\Node\Stmt\Function_;34use PhpParser\NodeFinder;35use PhpParser\NodeTraverser;36use PhpParser\NodeVisitorAbstract;37use PhpParser\ParserFactory;38function foo($x) {39 return $x + 1;40}41CODE;42$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);43$ast = $parser->parse($code);44$traverser = new NodeTraverser;45$traverser->addVisitor(new class extends NodeVisitorAbstract {46 public function enterNode(Node $node) {47 if ($node instanceof Function_) {48 var_dump($node->getSubNodeNames());49 }50 }51});52$traverser->traverse($ast);53array(2) {54 string(4) "name"55 string(4) "stmts"56}57require_once 'vendor/autoload.php';58use PhpParser\Error;
getTokenMatches
Using AI Code Generation
1require_once 'vendor/autoload.php';2use PhpParser\Node;3use PhpParser\NodeTraverser;4use PhpParser\NodeVisitorAbstract;5use PhpParser\ParserFactory;6$var = 10;7$var = 20;8CODE;9$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);10$traverser = new NodeTraverser;11$traverser->addVisitor(new class extends NodeVisitorAbstract {12 public function enterNode(Node $node) {13 if ($node instanceof Node\Expr\Assign) {14 $node->var->setAttribute('assign', $node->expr);15 }16 }17});18$traverser->addVisitor(new class extends NodeVisitorAbstract {19 public function enterNode(Node $node) {20 if ($node instanceof Node\Expr\Variable) {21 $assign = $node->getAttribute('assign');22 if ($assign !== null) {23 echo $assign->getAttribute('tokenStartPos'), " - ", $assign->getAttribute('tokenEndPos'), "24";25 }26 }27 }28});29$traverser->traverse($parser->parse($code));30require_once 'vendor/autoload.php';31use PhpParser\Node;32use PhpParser\NodeTraverser;33use PhpParser\NodeVisitorAbstract;34use PhpParser\ParserFactory;35$var = 10;36$var = 20;37CODE;38$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);39$traverser = new NodeTraverser;40$traverser->addVisitor(new class extends NodeVisitorAbstract {41 public function enterNode(Node $node) {42 if ($node instanceof Node\Expr\Assign) {43 $node->var->setAttribute('assign', $node->expr);44 }45 }46});47$traverser->addVisitor(new class extends NodeVisitorAbstract {48 public function enterNode(Node $node) {49 if ($node instanceof Node\Expr\Variable) {50 $assign = $node->getAttribute('assign');51 if ($assign !== null) {52 echo $assign->getTokenMatches()[0], "53";54 }55 }
getTokenMatches
Using AI Code Generation
1 $ast = \ast\parse_code('<?php2 $a = 1;3 $b = 2;4 $c = $a + $b;5 ', 50);6 $node = new \ast\Node(AST_STMT_LIST, 0, $ast->children);7 $matches = $node->getTokenMatches();8 print_r($matches);9 (10 (11 (12 (13 (14Recommended Posts: PHP | ast\parse_code() function15PHP | ast\parse_file() function16PHP | ast\Node::getChildren() method17PHP | ast\Node::getDocComment() method18PHP | ast\Node::getEndLine() method19PHP | ast\Node::getFlags() method20PHP | ast\Node::getKind() method21PHP | ast\Node::getLine() method22PHP | ast\Node::getName() method23PHP | ast\Node::getNumChildren() method
getTokenMatches
Using AI Code Generation
1$ast = new AstNode($code);2$tokenMatches = $ast->getTokenMatches(T_STRING, array('echo'));3var_dump($tokenMatches);4array(1) {5 array(2) {6 int(2)7 int(2)8 }9}10$ast = new AstNode($code);11$tokens = $ast->getTokens();12var_dump($tokens);13array(9) {14 array(2) {15 int(371)16 string(5) "<?php"17 }18 array(2) {19 int(371)20 string(5) "<?php"21 }22 array(2) {23 int(371)24 string(5) "<?php"25 }26 array(2) {27 int(371)28 string(5) "<?php"29 }30 array(2) {31 int(371)32 string(5) "<?php"33 }34 array(2) {35 int(371)36 string(5) "<?php"37 }38 array(2) {39 int(371)40 string(5) "<?php"41 }42 array(2) {43 int(371)44 string(5) "<?php"45 }46 array(2) {47 int(371)48 string(5) "<?php"49 }50}51$ast = new AstNode($code);52$tokens = $ast->getTokens(array(T_STRING, T_CONSTANT_ENCAPSED_STRING));53var_dump($
getTokenMatches
Using AI Code Generation
1$ast = new PhpParser\Parser(new PhpParser\Lexer\Emulative);2$code = file_get_contents('1.php');3$ast = $ast->parse($code);4$node = $ast[0];5$matches = $node->getTokenMatches(PhpParser\Lexer::T_STRING);6print_r($matches);7 (8 (9 (10 (11 (12 (13 (14 (15 (
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Execute automation tests with getTokenMatches on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.
Test now for FreeGet 100 minutes of automation test minutes FREE!!