How to use getSingle method of AstNode class

Best Gherkin-php code snippet using AstNode.getSingle

GherkinDocumentBuilder.php

Source:GherkinDocumentBuilder.php Github

copy

Full Screen

...63 }64 }65 public function getResult(): GherkinDocument66 {67 $document = $this->currentNode()->getSingle(GherkinDocument::class, Ruletype::GherkinDocument);68 if (null === $document) {69 throw new LogicException('GherkinDocument was not built from source, but no parse errors');70 }71 return $document;72 }73 public function reset(string $uri): void74 {75 $this->stack = [new AstNode(RuleType::None)];76 }77 private function currentNode(): AstNode78 {79 return $this->stack[array_key_last($this->stack)];80 }81 /**82 * @return object|string|list<object>|null83 */84 private function getTransformedNode(AstNode $node): object|string|array|null85 {86 return match ($node->ruleType) {87 RuleType::Step => $this->transformStepNode($node),88 RuleType::DocString => $this->transformDocStringNode($node),89 RuleType::ScenarioDefinition => $this->transformScenarioDefinitionNode($node),90 RuleType::ExamplesDefinition => $this->transformExamplesDefinitionNode($node),91 RuleType::ExamplesTable => $this->transformExamplesTableNode($node),92 RuleType::DataTable => $this->transformDataTableNode($node),93 Ruletype::Background => $this->transformBackgroundNode($node),94 RuleType::Description => $this->transformDescriptionNode($node),95 RuleType::Feature => $this->transformFeatureNode($node),96 RuleType::Rule => $this->transformRuleNode($node),97 RuleType::GherkinDocument => $this->transformGherkinDocumentNode($node),98 default => $node,99 };100 }101 private function getLocation(TokenMatch $token, int $column): MessageLocation102 {103 $column = ($column === 0) ? $token->location->column : $column;104 return new MessageLocation($token->location->line, $column);105 }106 private function getDescription(AstNode $node): string107 {108 return (string) $node->getSingleUntyped(RuleType::Description, "");109 }110 /** @return list<Step> */111 private function getSteps(AstNode $node): array112 {113 return $node->getitems(Step::class, RuleType::Step);114 }115 /** @return list<TableRow> */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);279 $featureLine = $header->getTokenMatch(TokenType::FeatureLine);280 $children = [];281 $background = $node->getSingle(Background::class, RuleType::Background);282 if ($background instanceof Background) {283 $children[] = new FeatureChild(background: $background);284 }285 foreach ($node->getItems(Scenario::class, RuleType::ScenarioDefinition) as $scenario) {286 $children[] = new FeatureChild(scenario: $scenario);287 }288 foreach ($node->getItems(Rule::class, RuleType::Rule) as $rule) {289 $children[] = new FeatureChild($rule, null, null);290 }291 $language = $featureLine->gherkinDialect->getLanguage();292 return new Feature(293 location: $this->getLocation($featureLine, 0),294 tags: $tags,295 language: $language,296 keyword: $featureLine->keyword,297 name: $featureLine->text,298 description: $this->getDescription($header),299 children: $children,300 );301 }302 private function transformRuleNode(AstNode $node): Rule303 {304 $header = $node->getSingle(AstNode::class, RuleType::RuleHeader, new AstNode(RuleType::RuleHeader));305 $ruleLine = $header->getTokenMatch(TokenType::RuleLine);306 $children = [];307 $tags = $this->getTags($header);308 $background = $node->getSingle(Background::class, RuleType::Background);309 if ($background) {310 $children[] = new RuleChild(background: $background);311 }312 $scenarios = $node->getItems(Scenario::class, RuleType::ScenarioDefinition);313 foreach ($scenarios as $scenario) {314 $children[] = new RuleChild(scenario: $scenario);315 }316 return new Rule(317 location: $this->getLocation($ruleLine, 0),318 tags: $tags,319 keyword: $ruleLine->keyword,320 name: $ruleLine->text,321 description: $this->getDescription($header),322 children: $children,323 id: $this->idGenerator->newId(),324 );325 }326 private function transformGherkinDocumentNode(AstNode $node): GherkinDocument327 {328 $feature = $node->getSingle(Feature::class, RuleType::Feature);329 return new GherkinDocument(330 uri: $this->uri,331 feature: $feature,332 comments: $this->comments,333 );334 }335}...

Full Screen

Full Screen

AstNodeTest.php

Source:AstNodeTest.php Github

copy

Full Screen

...28 self::assertSame([$obj1, $obj2], $this->astNode->getItems(stdClass::class, RuleType::None));29 }30 public function testItGetsDefaultResultWhenNoItemsAdded(): void31 {32 $item = $this->astNode->getSingle(stdClass::class, RuleType::None, $obj = new stdClass());33 self::assertSame($obj, $item);34 }35 public function testItGetsFirstSingleItemWhenMultipleAdded(): void36 {37 $this->astNode->add(RuleType::None, $obj1 = new stdClass());38 $this->astNode->add(RuleType::None, $obj2 = new stdClass());39 $item = $this->astNode->getSingle(stdClass::class, RuleType::None, $obj3 = new stdClass());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 }...

Full Screen

Full Screen

AstNode.php

Source:AstNode.php Github

copy

Full Screen

...44 * @param S|null $defaultValue45 *46 * @psalm-return ($defaultValue is null ? S|null : S )47 */48 public function getSingle(string $expectedType, RuleType $ruleType, ?object $defaultValue = null): mixed49 {50 $items = $this->getItems($expectedType, $ruleType);51 return $items[0] ?? $defaultValue;52 }53 /** needed for non-object return */54 public function getSingleUntyped(RuleType $ruleType, mixed $defaultValue = null): mixed55 {56 $items =$this->subItems[$ruleType->name] ?? [];57 /**58 * Force the type because we trust the parser, could be validated instead59 * @var list $items60 */61 return $items[0] ?? $defaultValue;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}...

Full Screen

Full Screen

getSingle

Using AI Code Generation

copy

Full Screen

1$ast = new AstNode();2$ast->getSingle('function', '1.php');3$ast = new AstNode();4$ast->getSingle('function', '2.php');5$ast = new AstNode();6$ast->getSingle('function', '3.php');7$ast = new AstNode();8$ast->getSingle('function', '4.php');9$ast = new AstNode();10$ast->getSingle('function', '5.php');11$ast = new AstNode();12$ast->getSingle('function', '6.php');13$ast = new AstNode();14$ast->getSingle('function', '7.php');15$ast = new AstNode();16$ast->getSingle('function', '8.php');17$ast = new AstNode();18$ast->getSingle('function', '9.php');19$ast = new AstNode();20$ast->getSingle('function', '10.php');21$ast = new AstNode();22$ast->getSingle('function', '11.php');23$ast = new AstNode();24$ast->getSingle('function', '12.php');25$ast = new AstNode();26$ast->getSingle('function', '13.php');

Full Screen

Full Screen

getSingle

Using AI Code Generation

copy

Full Screen

1$ast = new AstNode();2$ast->getSingle("test.php", "echo");3$ast = new AstNode();4$ast->getMultiple("test.php", "echo");5$ast = new AstNode();6$ast->getMultiple("test.php", "echo");7$ast = new AstNode();8$ast->getMultiple("test.php", "echo");9$ast = new AstNode();10$ast->getMultiple("test.php", "echo");11$ast = new AstNode();12$ast->getMultiple("test.php", "echo");13$ast = new AstNode();14$ast->getMultiple("test.php", "echo");15$ast = new AstNode();16$ast->getMultiple("test.php", "echo");17$ast = new AstNode();18$ast->getMultiple("test.php", "echo");19$ast = new AstNode();20$ast->getMultiple("test.php", "echo");21$ast = new AstNode();22$ast->getMultiple("test.php", "echo");23$ast = new AstNode();24$ast->getMultiple("test.php", "echo");25$ast = new AstNode();26$ast->getMultiple("test.php", "echo");

Full Screen

Full Screen

getSingle

Using AI Code Generation

copy

Full Screen

1$node = $ast->getSingle('Class');2$node = $ast->getSingle('Class', array('name' => 'MyClass'));3$node = $ast->getSingle('Class', array('name' => 'MyClass', 'type' => 'abstract'));4$node = $ast->getSingle('Class', array('name' => 'MyClass', 'type' => 'abstract', 'extends' => 'MyParent'));5$node = $ast->getSingle('Class', array('name' => 'MyClass', 'type' => 'abstract', 'implements' => array('MyInterface', 'MyOtherInterface')));6$node = $ast->getSingle('Class', array('name' => 'MyClass', 'type' => 'abstract', 'extends' => 'MyParent', 'implements' => array('MyInterface', 'MyOtherInterface')));7$node = $ast->getSingle('Class', array('name' => 'MyClass', 'type' => 'abstract', 'extends' => 'MyParent', 'implements' => array('MyInterface', 'MyOtherInterface')));8$node = $ast->getSingle('Class', array('name' => 'MyClass', 'type' => 'abstract', 'extends' => 'MyParent', 'implements' => array('MyInterface', 'MyOtherInterface')));9$node = $ast->getSingle('Class', array('name' => 'MyClass', 'type' => 'abstract', 'extends' => 'MyParent', 'implements' => array('MyInterface', 'MyOtherInterface')));10$node = $ast->getSingle('Class',

Full Screen

Full Screen

getSingle

Using AI Code Generation

copy

Full Screen

1$ast = new AstNode($file);2$ast->parse();3$ast->getSingle('Assign');4$ast = new AstNode($file);5$ast->parse();6$ast->getSingle('Assign');7$ast = new AstNode($file);8$ast->parse();9$ast->getSingle('Assign');10$ast = new AstNode($file);11$ast->parse();12$ast->getSingle('Assign');13$ast = new AstNode($file);14$ast->parse();15$ast->getSingle('Assign');16$ast = new AstNode($file);17$ast->parse();18$ast->getSingle('Assign');19$ast = new AstNode($file);20$ast->parse();21$ast->getSingle('Assign');22$ast = new AstNode($file);23$ast->parse();24$ast->getSingle('Assign');25$ast = new AstNode($file);26$ast->parse();27$ast->getSingle('Assign');28$ast = new AstNode($file);29$ast->parse();30$ast->getSingle('Assign');31$ast = new AstNode($file);32$ast->parse();33$ast->getSingle('Assign');34$ast = new AstNode($file);35$ast->parse();36$ast->getSingle('Assign');

Full Screen

Full Screen

getSingle

Using AI Code Generation

copy

Full Screen

1$node = AstNode::getSingle($id);2$children = $node->getChildren();3$parents = $node->getParents();4$ancestors = $node->getAncestors();5$descendants = $node->getDescendants();6$previousSiblings = $node->getPreviousSiblings();7$nextSiblings = $node->getNextSiblings();8$previousSibling = $node->getPreviousSibling();9$nextSibling = $node->getNextSibling();10$firstDescendant = $node->getFirstDescendant();11$lastDescendant = $node->getLastDescendant();12$firstChild = $node->getFirstChild();13$lastChild = $node->getLastChild();14$firstParent = $node->getFirstParent();

Full Screen

Full Screen

getSingle

Using AI Code Generation

copy

Full Screen

1require_once 'ast.php';2$node = AstNode::getSingle('1.php');3var_dump($node);4object(AstNode)#1 (3) {5 int(367)6 int(0)7 array(0) {8 }9}10{11 public $x = 10;12 public $y = 20;13 public $z = 30;14}15object(AstNode)#1 (3) {16 int(367)17 int(0)18 array(2) {19 object(AstNode)#2 (3) {20 int(368)21 int(0)22 array(1) {23 object(AstNode)#3 (3) {24 int(366)25 int(0)26 array(3) {27 object(AstNode)#4 (3) {28 int(365)29 int(0)30 array(1) {31 object(AstNode)#5 (3) {32 int(364)33 int(0)34 array(1) {35 object(AstNode)#6 (3) {36 int(363)37 int(0)38 array(1) {39 object(AstNode)#7 (3) {40 int(362)41 int(0)42 array(1) {43 object(AstNode)#8 (3) {44 int(361)45 int(0)46 array(1) {47 object(AstNode)#9 (3

Full Screen

Full Screen

getSingle

Using AI Code Generation

copy

Full Screen

1require_once 'ast_node.php';2$ast = new AstNode('1.php');3$ast->parse();4$node = $ast->getSingle('FunctionCall', 'echo', 'String');5var_dump($node);6require_once 'ast_node.php';7$ast = new AstNode('2.php');8$ast->parse();9$node = $ast->getSingle('FunctionCall', 'echo', 'String');10var_dump($node);11require_once 'ast_node.php';12$ast = new AstNode('3.php');13$ast->parse();14$node = $ast->getSingle('FunctionCall', 'echo', 'String');15var_dump($node);16require_once 'ast_node.php';17$ast = new AstNode('4.php');18$ast->parse();19$node = $ast->getSingle('FunctionCall', 'echo', 'String');20var_dump($node);21require_once 'ast_node.php';22$ast = new AstNode('5.php');23$ast->parse();24$node = $ast->getSingle('FunctionCall', 'echo', 'String');25var_dump($node);26require_once 'ast_node.php';27$ast = new AstNode('6.php');28$ast->parse();29$node = $ast->getSingle('FunctionCall', 'echo', 'String');30var_dump($node);31require_once 'ast_node.php';32$ast = new AstNode('7.php');33$ast->parse();34$node = $ast->getSingle('FunctionCall', 'echo', 'String');35var_dump($node);36require_once 'ast_node.php';37$ast = new AstNode('8.php');38$ast->parse();39$node = $ast->getSingle('FunctionCall', 'echo', 'String');40var_dump($node);

Full Screen

Full Screen

getSingle

Using AI Code Generation

copy

Full Screen

1$ast = new AstNode();2$ast->getSingle('1.php', 3, 3, 3, 3);3 (4 (5 [value] => (6 (7 [value] => (8 (9 [value] => (10 (11 [value] => (12 (13 [value] => (14 (15 [value] => (16 (17 [value] => (18 (19 [value] => (20 (21 [value] => (22 (23 [value] => (24 (25 [value] => (26 (27 [value] => (28 (29 [value] => (30 (31 [value] => (32 (33 [value] => (34 (35 [value] => (36 (37 [value] => (

Full Screen

Full Screen

getSingle

Using AI Code Generation

copy

Full Screen

1$astNode = new AstNode();2$astNode->getSingle('1.php', 'print', '1.php');3public function getSingle($file, $function, $file_name)4{5 $ast = $this->getAst($file);6 $this->search($ast, $function, $file_name);7}8public function getAst($file)9{10 $parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);11 $code = file_get_contents($file);12 $ast = $parser->parse($code);13 return $ast;14}15public function search($ast, $function, $file_name)16{17 if ($ast instanceof Node) {18 if ($ast->name === $function) {19 echo $file_name . ' ' . $function . ' ' . $ast->getLine();20 }21 foreach ($ast->getSubNodeNames() as $name) {22 $this->search($ast->$name, $function, $file_name);23 }24 } elseif (is_array($ast)) {25 foreach ($ast as $node) {26 $this->search($node, $function, $file_name);27 }28 }29}30array(1) {31 class PHPParser_Node_Stmt_Class#5 (8) {32 string(5) "Class"33 int(0)34 array(3) {35 class PHPParser_Node_Stmt_ClassMethod#6 (8) {36 int(1)37 bool(false)38 string(6) "method"39 array(1) {

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Gherkin-php automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Trigger getSingle code on LambdaTest Cloud Grid

Execute automation tests with getSingle on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful