How to use phpFunction class

Best Atoum code snippet using phpFunction

PHPFunctionParser.php

Source:PHPFunctionParser.php Github

copy

Full Screen

1<?php2/**3 * PHP 代码解析4 *5 * 使用方法:6 * $ps = new PHPFunctionParser($source_string);7 * $ret = $ps->parse();8 *9 * @author zhiyuan12 <zhiyuan12@staff.weibo.com>10 */11namespace Framework\Libraries;12class PHPFunctionParser {13 const BEGIN_LINE_INDEX = 'begin_line';14 const END_LINE_INDEX = 'end_line';15 const INVALID_NUM_INDEX = 'invalid_line_count';16 const NAMESPACE_INDEX = 'current_namespace';17 const CURRENT_CLASS_INDEX = 'current_class';18 const CURRENT_FUNCTION_INDEX = 'current_function';19 private $_content = '';20 private $_tokens = array();21 public function __construct($contents) {22 $this->_content = $contents;23 }24 public function parse() {25 $this->_tokens = token_get_all($this->_content);26 $sm = new FiniteStateMachine(); //初始化状态机27 $sm->setTickData($this->_tokens); //设置时钟数据28 $sm->register(Init::STATE, new Init($sm, InPHP::STATE)); //只能进入php代码一种状态29 $sm->register(InPHP::STATE, new InPHP($sm)); //可能遇到类、接口、函数或者退出php,需要状态自己判断30 $sm->register(MeetFunc::STATE, new MeetFunc($sm, InFunc::STATE)); //遇到函数后只能进入函数31 $sm->register(InFunc::STATE, new InFunc($sm, InPHP::STATE)); //在函数里结束后只能进入php32 $sm->register(MeetClass::STATE, new MeetClass($sm, InClass::STATE)); //遇到类后只能进入class33 $sm->register(InClass::STATE, new InClass($sm)); //在类内可以结束类或进入成员方法34 $sm->register(MeetMethod::STATE, new MeetMethod($sm, InMethod::STATE)); //遇到成员方法后只能进入方法35 $sm->register(InMethod::STATE, new InMethod($sm, InClass::STATE)); //成员方法结束后只能退出到类内36 $sm->register(MeetNameSpace::STATE, new MeetNameSpace($sm, InPHP::STATE)); //遇到命名空间解析后只能返回php37 $sm->setInitState(Init::STATE); //起始状态在php代码外38 $sm->run(); //运行状态机39 return $sm->getData();40 }41}42class PHPFunctionParserException extends Exception {43}44class Init extends FiniteState {45 const STATE = 0; // 初始状态,在php代码之外46 public function onStateEnter($from_state) {}47 public function onStateTick($token_key = null) {48 $token = $this->getTickData($token_key);49 if (is_array($token) && T_OPEN_TAG === $token[0]) {50 return true;51 }52 return false;53 }54 public function onStateExit() {}55}56class InPHP extends FiniteState {57 const STATE = 1; // 进入php代码58 public function onStateEnter($from_state) {}59 public function onStateTick($token_key = null) {60 $token = $this->getTickData($token_key);61 $pattern = is_array($token) ? $token[0] : $token;62 switch ($pattern) {63 case T_CLASS:64 $this->trans(MeetClass::STATE);65 break;66 case T_INTERFACE:67 break;68 case T_FUNCTION:69 $this->trans(MeetFunc::STATE);70 break;71 case T_NAMESPACE:72 $this->trans(MeetNameSpace::STATE);73 break;74 case T_STRING:75 break;76 case '{':77 case '}':78 break;79 case T_WHITESPACE:80 default:81 # code...82 break;83 }84 return false;85 }86 public function onStateExit() {}87}88class MeetFunc extends FiniteState {89 private $_name;90 private $_begin_line;91 const STATE = 2; // 碰到了函数定义,但还没进到函数里面92 public function onStateEnter($from_state) {93 $this->_name = '';94 $this->_begin_line = 0;95 }96 public function onStateTick($token_key = null) {97 $token = $this->getTickData($token_key);98 if (is_string($token) && '{' === $token) {99 $data = $this->getData();100 $f_n = trim($this->_name);101 if (isset($data[PHPFunctionParser::NAMESPACE_INDEX])) {102 $f_n = $data[PHPFunctionParser::NAMESPACE_INDEX] . "\\" . $f_n;103 }104 $data['functions'][$f_n] = array(PHPFunctionParser::BEGIN_LINE_INDEX => $this->_begin_line);105 $data[PHPFunctionParser::CURRENT_FUNCTION_INDEX] = $f_n;106 $this->setData($data);107 return true;108 }109 if (is_array($token) && 0 === $this->_begin_line) {110 $this->_begin_line = $token[2];111 }112 $this->_name .= is_string($token) ? $token : $token[1];113 }114 public function onStateExit() {}115}116class InFunc extends FiniteState {117 private $_left_braces;118 private $_invalid_line_num;119 private $_last_line_num;120 const STATE = 3; // 在函数里面了121 public function onStateEnter($from_state) {122 $this->_left_braces = 1;123 $this->_invalid_line_num = 0;124 $data = $this->getData();125 $this->_last_line_num = $data['functions'][$data[PHPFunctionParser::CURRENT_FUNCTION_INDEX]][PHPFunctionParser::BEGIN_LINE_INDEX];126 }127 public function onStateTick($token_key = null) {128 $token = $this->getTickData($token_key);129 update_line_num($this->_last_line_num, $token);130 count_invalid_line($this->_invalid_line_num, $token_key, $this->getTickData());131 update_braces($this->_left_braces, $token);132 if (0 === $this->_left_braces) {133 $data = $this->getData();134 $data['functions'][$data[PHPFunctionParser::CURRENT_FUNCTION_INDEX]][PHPFunctionParser::INVALID_NUM_INDEX] = $this->_invalid_line_num;135 $data['functions'][$data[PHPFunctionParser::CURRENT_FUNCTION_INDEX]][PHPFunctionParser::END_LINE_INDEX] = $this->_last_line_num;136 unset($data[PHPFunctionParser::CURRENT_FUNCTION_INDEX]);137 $this->setData($data);138 $this->trans(InPHP::STATE);139 }140 }141 public function onStateExit() {}142}143class MeetClass extends FiniteState {144 private $_stop_join_name = false;145 private $_name;146 private $_begin_line;147 const STATE = 4; // 碰到了类定义,但还没进到类里面148 public function onStateEnter($from_state) {149 $this->_name = '';150 $this->_begin_line = 0;151 }152 public function onStateTick($token_key = null) {153 $token = $this->getTickData($token_key);154 if (is_string($token) && '{' === $token) {155 $data = $this->getData();156 $c_n = trim($this->_name);157 if (isset($data[PHPFunctionParser::NAMESPACE_INDEX])) {158 $c_n = $data[PHPFunctionParser::NAMESPACE_INDEX] . "\\" . $c_n;159 }160 $data['classes'][$c_n] = array(PHPFunctionParser::BEGIN_LINE_INDEX => $this->_begin_line);161 $data[PHPFunctionParser::CURRENT_CLASS_INDEX] = $c_n;162 $this->setData($data);163 return true;164 } else if (is_array($token)) {165 if (0 === $this->_begin_line) {166 $this->_begin_line = $token[2];167 }168 if (T_EXTENDS === $token[0] || T_IMPLEMENTS === $token[0]) {169 $this->_stop_join_name = true;170 }171 }172 if ( ! $this->_stop_join_name) {173 $this->_name .= is_string($token) ? $token : $token[1];174 }175 }176 public function onStateExit() {}177}178class InClass extends FiniteState {179 private $_last_line_num;180 const STATE = 5; // 在类里面了181 public function onStateEnter($from_state) {182 $data = $this->getData();183 $this->_last_line_num = $data['classes'][$data[PHPFunctionParser::CURRENT_CLASS_INDEX]][PHPFunctionParser::BEGIN_LINE_INDEX];184 }185 public function onStateTick($token_key = null) {186 $token = $this->getTickData($token_key);187 update_line_num($this->_last_line_num, $token);188 if (is_string($token) && '}' === $token) {189 $data = $this->getData();190 $data['classes'][$data[PHPFunctionParser::CURRENT_CLASS_INDEX]][PHPFunctionParser::END_LINE_INDEX] = $this->_last_line_num;191 unset($data[PHPFunctionParser::CURRENT_CLASS_INDEX]);192 $this->setData($data);193 $this->trans(InPHP::STATE);194 }195 if (T_FUNCTION === $token[0]) {196 $this->trans(MeetMethod::STATE);197 }198 }199 public function onStateExit() {}200}201class MeetMethod extends FiniteState {202 private $_name;203 private $_begin_line;204 const STATE = 6; // 碰到了类中方法的定义,但还没进到方法里面205 public function onStateEnter($from_state) {206 $this->_name = '';207 $this->_begin_line = 0;208 }209 public function onStateTick($token_key = null) {210 $token = $this->getTickData($token_key);211 if (is_string($token) && '{' === $token) {212 $data = $this->getData();213 $f_n = trim($this->_name);214 $data['classes'][$data[PHPFunctionParser::CURRENT_CLASS_INDEX]]['methods'][$f_n] = array(PHPFunctionParser::BEGIN_LINE_INDEX => $this->_begin_line);215 $data[PHPFunctionParser::CURRENT_FUNCTION_INDEX] = $f_n;216 $this->setData($data);217 return true;218 }219 if (is_array($token) && 0 === $this->_begin_line) {220 $this->_begin_line = $token[2];221 }222 $this->_name .= is_string($token) ? $token : $token[1];223 }224 public function onStateExit() {}225}226class InMethod extends FiniteState {227 private $_left_braces;228 private $_last_line_num;229 private $_invalid_line_num;230 const STATE = 7; // 进到类的方法里面了231 public function onStateEnter($from_state) {232 $this->_left_braces = 1;233 $this->_invalid_line_num = 0;234 $data = $this->getData();235 $this->_last_line_num = $data['classes'][$data[PHPFunctionParser::CURRENT_CLASS_INDEX]]['methods'][$data[PHPFunctionParser::CURRENT_FUNCTION_INDEX]][PHPFunctionParser::BEGIN_LINE_INDEX];236 }237 public function onStateTick($token_key = null) {238 $token = $this->getTickData($token_key);239 update_line_num($this->_last_line_num, $token);240 count_invalid_line($this->_invalid_line_num, $token_key, $this->getTickData());241 update_braces($this->_left_braces, $token);242 if (0 === $this->_left_braces) {243 $data = $this->getData();244 $data['classes'][$data[PHPFunctionParser::CURRENT_CLASS_INDEX]]['methods'][$data[PHPFunctionParser::CURRENT_FUNCTION_INDEX]][PHPFunctionParser::END_LINE_INDEX] = $this->_last_line_num;245 $data['classes'][$data[PHPFunctionParser::CURRENT_CLASS_INDEX]]['methods'][$data[PHPFunctionParser::CURRENT_FUNCTION_INDEX]][PHPFunctionParser::INVALID_NUM_INDEX] = $this->_invalid_line_num;246 unset($data[PHPFunctionParser::CURRENT_FUNCTION_INDEX]);247 $this->setData($data);248 $this->trans(InClass::STATE);249 }250 }251 public function onStateExit() {}252}253class MeetNameSpace extends FiniteState {254 private $_name;255 const STATE = 8;256 public function onStateEnter($from_state) {257 $this->_name = '';258 }259 public function onStateTick($token_key = null) {260 $token = $this->getTickData($token_key);261 if (is_string($token) && ';' === $token) {262 $data = $this->getData();263 $data[PHPFunctionParser::NAMESPACE_INDEX] = trim($this->_name);264 $this->setData($data);265 return true;266 }267 $this->_name .= is_string($token) ? $token : $token[1];268 }269 public function onStateExit() {}270}271function update_braces(&$braces_num, $token) {272 if (is_string($token)) {273 if ('{' === $token) {274 $braces_num++;275 } else if ('}' === $token) {276 $braces_num--;277 }278 } else if (is_array($token) &&279 (T_CURLY_OPEN === $token[0] || T_DOLLAR_OPEN_CURLY_BRACES === $token[0])) {280 $braces_num++;281 }282}283function count_invalid_line(&$invalid_num, $token_key, $token_data) {284 $token = $token_data[$token_key];285 if (is_array($token)) {286 if (T_WHITESPACE === $token[0]) {287 $tmp = count_line_num($token[1]);288 if ($tmp > 0) {289 $last_token = $token_data[$token_key - 1];290 if (is_array($last_token) && count_line_num($last_token[1]) > 0) {291 $invalid_num += $tmp;292 } else {293 $invalid_num += $tmp - 1;294 }295 }296 } else if (T_DOC_COMMENT === $token[0]) {297 $invalid_num += count_line_num($token[1]);298 } else if (T_COMMENT === $token[0]) {299 $last_key = $token_key;300 while (true) {301 $last_key--;302 $last_token = $token_data[$last_key];303 if (is_array($last_token)) {304 if (T_WHITESPACE === $last_token[0]) {305 if (count_line_num($last_token[1]) > 0) {306 $invalid_num += 1;307 break;308 } else {309 continue;310 }311 } else {312 if ($last_token[2] !== $token[2]) {313 $invalid_num += 1;314 }315 break;316 }317 } else {318 break;319 }320 }321 }322 }323}324function update_line_num(&$line_num, $token) {325 if (is_array($token)) {326 $line_num = $token[2];327 if (T_WHITESPACE === $token[0]) {328 $tmp = count_line_num($token[1]);329 $line_num += $tmp;330 }331 }332}333function count_line_num($string) {334 $count1 = substr_count($string, "\r");335 $count2 = substr_count($string, "\n");336 return max($count1, $count2);337}...

Full Screen

Full Screen

StringPackage.php

Source:StringPackage.php Github

copy

Full Screen

1<?php2namespace eMacros\Package;3use eMacros\Runtime\PHPFunction;4use eMacros\Runtime\String\StringReplace;5use eMacros\Runtime\String\StringScan;6class StringPackage extends Package {7 public function __construct() {8 parent::__construct('String');9 10 //conversion11 $this['bin2hex'] = new PHPFunction('bin2hex');12 $this['hex2bin'] = new PHPFunction('hex2bin');13 $this['explode'] = new PHPFunction('explode');14 $this['implode'] = new PHPFunction('implode');15 $this['split'] = new PHPFunction('str_split');16 $this['getcsv'] = new PHPFunction('str_getcsv');17 18 //string functions19 $this['chr'] = new PHPFunction('chr');20 $this['ord'] = new PHPFunction('ord');21 $this['count-chars'] = new PHPFunction('count_chars');22 $this['repeat'] = new PHPFunction('str_repeat');23 $this['word-count'] = new PHPFunction('str_word_count');24 $this['cmp'] = new PHPFunction('strcmp');25 $this['len'] = new PHPFunction('strlen');26 $this['pos'] = new PHPFunction('strpos');27 $this['ipos'] = new PHPFunction('stripos');28 $this['str'] = new PHPFunction('strstr');29 $this['istr'] = new PHPFunction('stristr');30 $this['pbrk'] = new PHPFunction('strpbrk');31 $this['tok'] = new PHPFunction('strtok');32 33 //modification34 $this['addcslashes'] = new PHPFunction('addcslashes');35 $this['stripcslashes'] = new PHPFunction('stripcslashes');36 $this['substr'] = new PHPFunction('substr');37 $this['trim'] = new PHPFunction('trim');38 $this['ltrim'] = new PHPFunction('ltrim');39 $this['rtrim'] = new PHPFunction('rtrim');40 $this['pad'] = new PHPFunction('str_pad');41 $this['reverse'] = new PHPFunction('strrev');42 $this['shuffle'] = new PHPFunction('str_shuffle');43 $this['replace'] = new StringReplace('str_replace');44 $this['ireplace'] = new StringReplace('str_ireplace');45 46 //case47 $this['lcfirst'] = new PHPFunction('lcfirst');48 $this['ucfirst'] = new PHPFunction('ucfirst');49 $this['ucwords'] = new PHPFunction('ucwords');50 $this['to-lower'] = new PHPFunction('strtolower');51 $this['to-upper'] = new PHPFunction('strtoupper');52 53 //format54 $this['number-format'] = new PHPFunction('number_format');55 $this['sprintf'] = new PHPFunction('sprintf');56 $this['sscanf'] = new StringScan();57 58 //pad constants59 $this['PAD_LEFT'] = STR_PAD_LEFT;60 $this['PAD_RIGHT'] = STR_PAD_RIGHT;61 $this['PAD_BOTH'] = STR_PAD_BOTH;62 }63}64?>...

Full Screen

Full Screen

IdentificationOrmModelType.php

Source:IdentificationOrmModelType.php Github

copy

Full Screen

...11 parent::loadExternalType();12 $this->typeLabel = 'Identification';13 $this->typeId = 'b8b75242-ada5-4820-b486-005f7cf5bfe1';14 }15 public function getModelCode(PhpClass $phpClass, PhpFunction $phpFunction)16 {17 $this->addModelObject($phpClass, $phpFunction, IdentificationModelType::class);18 }19 public function getExternalCode(PhpClass $phpClass, PhpFunction $phpFunction)20 {21 $this->addExternlObject($phpClass, $phpFunction, IdentificationModelType::class);22 }23 public function getDataCode(PhpClass $phpClass, PhpFunction $phpFunction)24 {25 $this->addDataObject($phpClass, $phpFunction, Identification::class, IdentificationDataProperty::class);26 }27 public function getRowCode(PhpClass $phpClass)28 {29 $this->addRowObjectProperty($phpClass, Identification::class, IdentificationReaderProperty::class);30 }31}...

Full Screen

Full Screen

phpFunction

Using AI Code Generation

copy

Full Screen

1use \mageekguy\atoum\phpFunction as php;2use \mageekguy\atoum\phpFunction as php;3use \mageekguy\atoum\phpFunction as php;4use \mageekguy\atoum\phpFunction as php;5use \mageekguy\atoum\phpFunction as php;6use \mageekguy\atoum\phpFunction as php;7use \mageekguy\atoum\phpFunction as php;8use \mageekguy\atoum\phpFunction as php;9use \mageekguy\atoum\phpFunction as php;10use \mageekguy\atoum\phpFunction as php;11use \mageekguy\atoum\phpFunction as php;12use \mageekguy\atoum\phpFunction as php;13use \mageekguy\atoum\phpFunction as php;14use \mageekguy\atoum\phpFunction as php;

Full Screen

Full Screen

phpFunction

Using AI Code Generation

copy

Full Screen

1require_once 'phpFunction.php';2$phpFunction = new phpFunction();3$phpFunction->test();4require_once 'phpFunction.php';5$phpFunction = new phpFunction();6$phpFunction->test();7{8 public function test()9 {10 echo "Hello World!";11 }12}

Full Screen

Full Screen

phpFunction

Using AI Code Generation

copy

Full Screen

1require_once 'phpFunction.php';2$phpFunction = new phpFunction();3echo $phpFunction->add(1, 2);4{5 public function add($a, $b)6 {7 return $a + $b;8 }9}

Full Screen

Full Screen

phpFunction

Using AI Code Generation

copy

Full Screen

1$phpFunction = new \mageekguy\atoum\php\functionMocker\phpFunction();2$phpFunction->getFunctionMock('myFunction')->once();3myFunction();4myFunction();5$phpFunction = new \mageekguy\atoum\php\functionMocker\phpFunction();6$phpFunction->getFunctionMock('myFunction')->once();7myFunction();8$phpFunction = new \mageekguy\atoum\php\functionMocker\phpFunction();9$phpFunction->getFunctionMock('myFunction')->once();10myFunction();

Full Screen

Full Screen

phpFunction

Using AI Code Generation

copy

Full Screen

1$phpFunction = new \mageekguy\atoum\php\mocker\phpFunction('is_numeric', 'myFunction');2$phpFunction->call($arg1, $arg2);3if (myFunction($arg1, $arg2)) {4}5$phpFunction = new \mageekguy\atoum\php\mocker\phpFunction('is_numeric', 'myFunction');6$phpFunction->call($arg1, $arg2);7class MyClass {8 public function myFunction($arg1, $arg2)9 {10 if (is_numeric($arg1) && is_numeric($arg2)) {11 return true;12 }13 return false;14 }15}16$myClass = new MyClass();17if ($myClass->myFunction($arg1, $arg2)) {18}19$phpFunction = new \mageekguy\atoum\php\mocker\phpFunction('is_numeric', 'myFunction');20$phpFunction->call($arg1, $arg2);21function myFunction($arg1, $arg2)22{23 if (is_numeric($arg1) && is_numeric($arg2)) {24 return true;25 }26 return false;27}28if (myFunction($arg1, $arg2)) {29}30$phpFunction = new \mageekguy\atoum\php\mocker\phpFunction('is_numeric', 'myFunction');31$phpFunction->call($arg1, $arg2);32class MyClass {33 public function myFunction($arg

Full Screen

Full Screen

phpFunction

Using AI Code Generation

copy

Full Screen

1$phpFunction = new \atoum\test\phpFunction();2$phpFunction->getFunctionMock('namespace', 'functionName');3$phpFunction->getMockController()->functionName = 'return value';4$this->string(functionName())->isEqualTo('return value');5$phpFunction = new \atoum\test\phpFunction();6$phpFunction->getFunctionMock('namespace', 'functionName');7$phpFunction->getMockController()->functionName = 'return value';8$this->string(functionName())->isEqualTo('return value');9$phpFunction = new \atoum\test\phpFunction();10$phpFunction->getFunctionMock('namespace', 'functionName');11$phpFunction->getMockController()->functionName = 'return value';12$this->string(functionName())->isEqualTo('return value');13$phpFunction = new \atoum\test\phpFunction();14$phpFunction->getFunctionMock('namespace', 'functionName');15$phpFunction->getMockController()->functionName = 'return value';16$this->string(functionName())->isEqualTo('return value');17$phpFunction = new \atoum\test\phpFunction();18$phpFunction->getFunctionMock('namespace', 'functionName');19$phpFunction->getMockController()->functionName = 'return value';20$this->string(functionName())->isEqualTo('return value');21$phpFunction = new \atoum\test\phpFunction();22$phpFunction->getFunctionMock('namespace', 'functionName');23$phpFunction->getMockController()->functionName = 'return value';24$this->string(functionName())->isEqualTo('return value');25$phpFunction = new \atoum\test\phpFunction();26$phpFunction->getFunctionMock('namespace', 'functionName');27$phpFunction->getMockController()->

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 Atoum automation tests on LambdaTest cloud grid

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

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

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