idx
int64
0
60.3k
question
stringlengths
101
6.21k
target
stringlengths
7
803
27,300
public function parents ( $ current = false ) { $ instances = [ ] ; $ instance = $ current ? $ this : $ this -> _parent ; while ( $ instance !== null ) { $ instances [ ] = $ instance ; $ instance = $ instance -> _parent ; } return array_reverse ( $ instances ) ; }
Return all parent block instances .
27,301
public function messages ( ) { $ messages = [ ] ; $ instances = $ this -> parents ( true ) ; foreach ( $ instances as $ instance ) { $ messages [ ] = $ instance -> message ( ) ; } return $ messages ; }
Return all messages upon the root .
27,302
public function log ( $ type = null , $ data = [ ] ) { if ( ! func_num_args ( ) ) { return $ this -> _log ; } $ this -> report ( $ type , $ this -> log ( ) -> add ( $ type , $ data ) ) ; }
Dispatch a report .
27,303
protected function _process ( $ options = [ ] ) { $ suite = $ this -> suite ( ) ; if ( $ suite -> root ( ) -> focused ( ) && ! $ this -> focused ( ) ) { return ; } $ this -> _passed = true ; if ( $ this -> excluded ( ) ) { $ this -> log ( ) -> type ( 'excluded' ) ; $ this -> summary ( ) -> log ( $ this -> log ( ) ) ; $ this -> report ( 'specEnd' , $ this -> log ( ) ) ; return ; } $ result = null ; $ suite :: push ( $ this ) ; if ( $ suite :: $ PHP >= 7 && ! defined ( 'HHVM_VERSION' ) ) { try { $ this -> _blockStart ( ) ; try { $ result = $ this -> _execute ( ) ; } catch ( Throwable $ exception ) { $ this -> _exception ( $ exception ) ; } $ this -> _blockEnd ( ) ; } catch ( Throwable $ exception ) { $ this -> _exception ( $ exception , true ) ; $ this -> _blockEnd ( ! $ exception instanceof SkipException ) ; } } else { try { $ this -> _blockStart ( ) ; try { $ result = $ this -> _execute ( ) ; } catch ( Exception $ exception ) { $ this -> _exception ( $ exception ) ; } $ this -> _blockEnd ( ) ; } catch ( Exception $ exception ) { $ this -> _exception ( $ exception , true ) ; $ this -> _blockEnd ( ! $ exception instanceof SkipException ) ; } } $ suite :: pop ( ) ; return $ this -> _return = $ result ; }
Block processing .
27,304
protected function _exception ( $ exception , $ inEachHook = false ) { if ( $ exception instanceof SkipException ) { ! $ inEachHook ? $ this -> log ( ) -> type ( 'skipped' ) : $ this -> _skipChildren ( $ exception ) ; return ; } $ this -> _passed = false ; $ this -> log ( ) -> type ( 'errored' ) ; $ this -> log ( ) -> exception ( $ exception ) ; }
Manage catched exception .
27,305
protected function _emitFocus ( ) { $ this -> summary ( ) -> add ( 'focused' , $ this ) ; $ instances = $ this -> parents ( true ) ; foreach ( $ instances as $ instance ) { $ instance -> type ( 'focus' ) ; } }
Apply focus up to the root .
27,306
protected function _check ( $ reference ) { $ isString = is_string ( $ reference ) ; if ( $ isString ) { if ( ! class_exists ( $ reference ) ) { throw new InvalidArgumentException ( "Can't Spy the unexisting class `{$reference}`." ) ; } $ reflection = Inspector :: inspect ( $ reference ) ; } else { $ reflection = Inspector :: inspect ( get_class ( $ reference ) ) ; } if ( ! $ reflection -> isInternal ( ) ) { return ; } if ( ! $ isString ) { throw new InvalidArgumentException ( "Can't Spy built-in PHP instances, create a test double using `Double::instance()`." ) ; } }
Check the actual value can receive messages .
27,307
protected function _reference ( $ reference ) { if ( ! is_string ( $ reference ) ) { return $ reference ; } $ pos = strrpos ( $ reference , '\\' ) ; if ( $ pos !== false ) { $ namespace = substr ( $ reference , 0 , $ pos ) ; $ basename = substr ( $ reference , $ pos + 1 ) ; } else { $ namespace = null ; $ basename = $ reference ; } $ substitute = null ; $ reference = Monkey :: patched ( $ namespace , $ basename , false , $ substitute ) ; return $ substitute ? : $ reference ; }
Return the actual reference which must be used .
27,308
protected function _watch ( $ message ) { $ reference = $ message -> reference ( ) ; if ( ! $ reference ) { Suite :: register ( $ message -> name ( ) ) ; return $ message ; } if ( is_object ( $ reference ) ) { Suite :: register ( get_class ( $ reference ) ) ; } Suite :: register ( Suite :: hash ( $ reference ) ) ; return $ message ; }
Watch a message .
27,309
public function resolve ( ) { $ startIndex = $ this -> _ordered ? Calls :: lastFindIndex ( ) : 0 ; $ report = Calls :: find ( $ this -> _messages , $ startIndex , $ this -> times ( ) ) ; $ this -> _report = $ report ; $ this -> _buildDescription ( $ startIndex ) ; return $ report [ 'success' ] ; }
Resolves the matching .
27,310
public function suiteStart ( $ suite = null ) { $ messages = $ suite -> messages ( ) ; if ( count ( $ messages ) === 1 ) { return ; } if ( count ( $ messages ) === 2 ) { $ this -> write ( "\n" ) ; } $ message = end ( $ messages ) ; $ this -> write ( "{$message}\n" ) ; $ this -> _indent ++ ; }
Callback called on a suite start .
27,311
public function given ( $ name , $ closure ) { if ( isset ( static :: $ _blacklist [ $ name ] ) ) { throw new Exception ( "Sorry `{$name}` is a reserved keyword, it can't be used as a scope variable." ) ; } $ given = new Given ( $ closure ) ; if ( array_key_exists ( $ name , $ this -> _given ) ) { $ given -> { $ name } = $ this -> _given [ $ name ] ( Suite :: current ( ) -> scope ( ) ) ; } $ this -> _given [ $ name ] = $ given ; return $ this ; }
Sets a lazy loaded data .
27,312
public function add ( $ name , $ metrics ) { $ parts = $ this -> _parseName ( $ name ) ; $ this -> _merge ( $ metrics ) ; $ current = $ this ; $ length = count ( $ parts ) ; foreach ( $ parts as $ index => $ part ) { list ( $ name , $ type ) = $ part ; if ( ! isset ( $ current -> _children [ $ name ] ) ) { $ current -> _children [ $ name ] = new static ( [ 'name' => $ name , 'parent' => $ current , 'type' => $ type ] ) ; } uksort ( $ current -> _children , function ( $ a , $ b ) { $ isFunction1 = substr ( $ a , - 2 ) === '()' ; $ isFunction2 = substr ( $ b , - 2 ) === '()' ; if ( $ isFunction1 === $ isFunction2 ) { return strcmp ( $ a , $ b ) ; } return $ isFunction1 ? - 1 : 1 ; } ) ; $ current = $ current -> _children [ $ name ] ; $ current -> _merge ( $ metrics , $ index === $ length - 1 ) ; } }
Adds some metrics to the current metrics .
27,313
public function get ( $ name = null ) { $ parts = $ this -> _parseName ( $ name ) ; $ child = $ this ; foreach ( $ parts as $ part ) { list ( $ name ) = $ part ; if ( ! isset ( $ child -> _children [ $ name ] ) ) { return ; } $ child = $ child -> _children [ $ name ] ; } return $ child ; }
Gets the metrics from a name .
27,314
public function children ( $ name = null ) { $ child = $ this -> get ( $ name ) ; if ( ! $ child ) { return [ ] ; } return $ child -> _children ; }
Gets the children of the current metrics .
27,315
protected function _parseName ( $ name ) { $ result = [ ] ; $ parts = preg_split ( '~([^\\\]*\\\?)~' , $ name , - 1 , PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY ) ; $ last = array_pop ( $ parts ) ; if ( ! $ last ) { return [ ] ; } foreach ( $ parts as $ name ) { $ result [ ] = [ $ name , 'namespace' ] ; } if ( strpos ( $ last , '::' ) !== false ) { list ( $ name , $ subname ) = explode ( '::' , $ last , 2 ) ; $ result [ ] = [ $ name , 'class' ] ; $ result [ ] = [ $ subname , 'method' ] ; } elseif ( preg_match ( '~\(\)$~' , $ last ) ) { $ result [ ] = [ $ last , 'function' ] ; } else { $ result [ ] = [ $ last , substr ( $ last , - 1 ) === '\\' ? 'namespace' : 'class' ] ; } return $ result ; }
Gets meta info of a metrics from a name reference ..
27,316
protected function _merge ( $ metrics = [ ] , $ line = false ) { $ defaults = [ 'loc' => [ ] , 'nlloc' => [ ] , 'lloc' => [ ] , 'cloc' => [ ] , 'files' => [ ] , 'methods' => 0 , 'cmethods' => 0 ] ; $ metrics += $ defaults ; foreach ( [ 'loc' , 'nlloc' , 'lloc' , 'cloc' , 'coverage' , 'files' , 'methods' , 'cmethods' ] as $ name ) { $ metrics [ $ name ] += $ this -> _metrics [ $ name ] ; } if ( ! $ line ) { unset ( $ metrics [ 'line' ] ) ; } $ this -> data ( $ metrics ) ; }
Merges some given metrics to the existing metrics .
27,317
public static function parameters ( $ class , $ method , $ data = null ) { $ params = [ ] ; $ reflexion = Inspector :: inspect ( $ class ) ; $ parameters = $ reflexion -> getMethod ( $ method ) -> getParameters ( ) ; if ( $ data === null ) { return $ parameters ; } foreach ( $ data as $ key => $ value ) { $ name = $ key ; if ( $ parameters ) { $ parameter = array_shift ( $ parameters ) ; $ name = $ parameter -> getName ( ) ; } $ params [ $ name ] = $ value ; } foreach ( $ parameters as $ parameter ) { if ( $ parameter -> isDefaultValueAvailable ( ) ) { $ params [ $ parameter -> getName ( ) ] = $ parameter -> getDefaultValue ( ) ; } } return $ params ; }
Gets the parameters array of a class method .
27,318
public static function typehint ( $ parameter ) { $ typehint = '' ; if ( $ parameter -> getClass ( ) ) { $ typehint = '\\' . $ parameter -> getClass ( ) -> getName ( ) ; } elseif ( preg_match ( '/.*?\[ \<[^\>]+\> (?:HH\\\)?(\w+)(.*?)\$/' , ( string ) $ parameter , $ match ) ) { $ typehint = $ match [ 1 ] ; if ( $ typehint === 'integer' ) { $ typehint = 'int' ; } elseif ( $ typehint === 'boolean' ) { $ typehint = 'bool' ; } elseif ( $ typehint === 'mixed' ) { $ typehint = '' ; } } return $ typehint ; }
Returns the type hint of a ReflectionParameter instance .
27,319
public function prev ( ) { $ value = prev ( $ this -> _data ) ; return key ( $ this -> _data ) !== null ? $ value : null ; }
Moves backward to the previous item .
27,320
public function parse ( $ argv , $ override = true ) { $ exists = [ ] ; $ override ? $ this -> _values = $ this -> _defaults : $ exists = array_fill_keys ( array_keys ( $ this -> _values ) , true ) ; foreach ( $ argv as $ arg ) { if ( $ arg === '--' ) { break ; } if ( $ arg [ 0 ] === '-' ) { list ( $ name , $ value ) = $ this -> _parse ( ltrim ( $ arg , '-' ) ) ; if ( $ override || ! isset ( $ exists [ $ name ] ) ) { $ this -> add ( $ name , $ value ) ; } } } return $ this -> get ( ) ; }
Parses a command line argv .
27,321
public function exists ( $ name ) { list ( $ key , $ extra ) = $ this -> _splitOptionName ( $ name ) ; if ( isset ( $ this -> _values [ $ key ] ) && is_array ( $ this -> _values [ $ key ] ) && array_key_exists ( $ extra , $ this -> _values [ $ key ] ) ) { return true ; } if ( isset ( $ this -> _options [ $ name ] ) ) { return isset ( $ this -> _options [ $ name ] [ 'default' ] ) ; } return false ; }
Checks if an option has been setted .
27,322
public function set ( $ name , $ value ) { list ( $ key , $ extra ) = $ this -> _splitOptionName ( $ name ) ; if ( $ extra && ! isset ( $ this -> _options [ $ key ] ) ) { $ this -> option ( $ key , [ 'group' => true , 'array' => true ] ) ; } return $ this -> _values [ $ key ] [ $ extra ] = $ value ; }
Sets the value of a specific option .
27,323
public function get ( $ name = null ) { if ( func_num_args ( ) ) { return $ this -> _get ( $ name ) ; } $ result = [ ] ; foreach ( $ this -> _values as $ key => $ data ) { foreach ( $ data as $ extra => $ value ) { if ( $ extra === '' ) { $ result [ $ key ] = $ this -> _get ( $ key ) ; } else { $ result [ $ key ] [ $ extra ] = $ this -> _get ( $ key . ':' . $ extra ) ; } } } return $ result ; }
Gets the value of a specific option .
27,324
public function cast ( $ value , $ type , $ array = false ) { if ( is_array ( $ value ) ) { $ result = [ ] ; foreach ( $ value as $ key => $ item ) { $ result [ $ key ] = $ this -> cast ( $ item , $ type ) ; } return $ result ; } if ( $ type === 'boolean' ) { $ value = ( $ value === 'false' || $ value === '0' || $ value === false || $ value === null ) ? false : true ; } elseif ( $ type === 'numeric' ) { $ value = $ value !== null ? ( int ) $ value + 0 : 1 ; } elseif ( $ type === 'string' ) { $ value = ( $ value !== true && $ value !== null ) ? ( string ) $ value : null ; } if ( $ array ) { return $ value ? ( array ) $ value : [ ] ; } return $ value ; }
Casts a value according to the option attributes .
27,325
protected function _closeCurly ( ) { $ current = $ this -> _states [ 'current' ] ; $ this -> _codeNode ( ) ; $ current -> close = '}' ; if ( $ current -> type === 'function' ) { if ( $ current -> isClosure ) { $ current -> close .= $ this -> _stream -> next ( [ ')' , ';' , ',' , ']' ] ) ; $ this -> _states [ 'num' ] += substr_count ( $ current -> close , "\n" ) ; } } elseif ( $ current -> type === 'namespace' ) { $ this -> _flushUses ( ) ; } $ this -> _states [ 'current' ] = $ current -> parent ; if ( ! $ this -> _states [ 'lines' ] ) { return ; } $ current -> lines [ 'stop' ] = $ this -> _states [ 'num' ] ; $ current -> parent -> lines [ 'stop' ] = $ this -> _states [ 'num' ] ; }
Manage curly brackets .
27,326
protected function _declareNode ( ) { $ this -> _codeNode ( ) ; $ body = $ this -> _stream -> current ( ) . $ this -> _stream -> next ( [ ';' , '{' ] ) ; if ( preg_match ( '~ticks~i' , $ body , $ matches ) ) { $ isBlock = substr ( $ body , - 1 ) === '{' ; if ( $ isBlock ) { $ body = substr ( $ body , 0 , - 1 ) ; } $ node = new NodeDef ( $ body , 'declare' ) ; $ this -> _contextualize ( $ node ) ; if ( $ isBlock ) { $ this -> _states [ 'body' ] .= '{' ; $ this -> _states [ 'current' ] = $ this -> _codeNode ( ) ; } return $ node ; } $ this -> _states [ 'body' ] .= $ body ; $ node = new BlockDef ( $ body , 'declare' ) ; $ node -> hasMethods = false ; $ this -> _states [ 'current' ] = $ this -> _root ; $ this -> _contextualize ( $ node ) ; return $ this -> _states [ 'current' ] = $ node ; }
Build a declare node .
27,327
protected function _namespaceNode ( ) { $ this -> _codeNode ( ) ; $ this -> _flushUses ( ) ; $ body = $ this -> _stream -> current ( ) ; $ name = $ this -> _stream -> next ( [ ';' , '{' ] ) ; $ this -> _states [ 'body' ] .= $ body ; $ node = new BlockDef ( $ body . $ name , 'namespace' ) ; $ node -> hasMethods = false ; $ node -> name = trim ( substr ( $ name , 0 , - 1 ) ) ; $ this -> _states [ 'current' ] = $ this -> _root ; $ this -> _contextualize ( $ node ) ; return $ this -> _states [ 'current' ] = $ node -> namespace = $ node ; }
Build a namespace node .
27,328
protected function _flushUses ( ) { if ( $ this -> _states [ 'current' ] && $ this -> _states [ 'current' ] -> namespace ) { $ this -> _states [ 'current' ] -> namespace -> uses = $ this -> _states [ 'uses' ] ; $ this -> _states [ 'uses' ] = [ ] ; } }
Attache the founded uses to the current namespace .
27,329
protected function _traitNode ( ) { $ this -> _codeNode ( ) ; $ token = $ this -> _stream -> current ( true ) ; $ body = $ token [ 1 ] ; $ body .= $ this -> _stream -> skipWhitespaces ( ) ; $ body .= $ name = $ this -> _stream -> current ( ) ; $ body .= $ this -> _stream -> next ( [ ';' , '{' ] ) ; $ this -> _states [ 'body' ] .= $ body ; $ node = new BlockDef ( $ body , 'trait' ) ; $ node -> name = $ name ; return $ this -> _states [ 'current' ] = $ this -> _contextualize ( $ node ) ; }
Build a trait node .
27,330
protected function _classNode ( ) { if ( substr ( $ this -> _states [ 'body' ] , - 2 ) === '::' ) { $ this -> _states [ 'body' ] .= 'class' ; return ; } $ this -> _codeNode ( ) ; $ token = $ this -> _stream -> current ( true ) ; $ body = $ token [ 1 ] ; $ body .= $ this -> _stream -> skipWhitespaces ( ) ; $ body .= $ name = $ this -> _stream -> current ( ) ; if ( $ name !== '{' ) { $ body .= $ this -> _stream -> next ( [ '{' , T_EXTENDS , T_IMPLEMENTS ] ) ; } else { $ name = '' ; } $ token = $ this -> _stream -> current ( true ) ; $ extends = '' ; $ implements = '' ; if ( $ token [ 0 ] === T_EXTENDS ) { $ body .= $ this -> _stream -> skipWhitespaces ( ) ; $ body .= $ extends = $ this -> _stream -> skipWhile ( [ T_STRING , T_NS_SEPARATOR ] ) ; $ body .= $ this -> _stream -> current ( ) ; if ( $ this -> _stream -> current ( ) !== '{' ) { $ body .= $ this -> _stream -> next ( '{' ) ; } } elseif ( $ token [ 0 ] === T_IMPLEMENTS ) { $ body .= $ implements = $ this -> _stream -> next ( '{' ) ; $ implements = substr ( $ implements , 0 , - 1 ) ; } $ node = new BlockDef ( $ body , 'class' ) ; $ node -> name = $ name ; $ node -> extends = $ this -> _normalizeClass ( $ extends ) ; $ node -> implements = $ this -> _normalizeImplements ( $ implements ) ; $ this -> _states [ 'body' ] .= $ body ; return $ this -> _states [ 'current' ] = $ this -> _contextualize ( $ node ) ; }
Build a class node .
27,331
protected function _normalizeClass ( $ name ) { if ( ! $ name || $ name [ 0 ] === '\\' ) { return $ name ; } if ( $ this -> _states [ 'uses' ] ) { $ tokens = explode ( '\\' , $ name , 2 ) ; if ( isset ( $ this -> _states [ 'uses' ] [ $ tokens [ 0 ] ] ) ) { $ prefix = $ this -> _states [ 'uses' ] [ $ tokens [ 0 ] ] ; return count ( $ tokens ) === 2 ? '\\' . $ prefix . '\\' . $ tokens [ 1 ] : '\\' . $ prefix ; } } $ current = $ this -> _states [ 'current' ] ; $ prefix = '\\' ; if ( $ current -> namespace ) { $ prefix .= $ current -> namespace -> name . '\\' ; } return $ prefix . $ name ; }
Normalizes a class name .
27,332
protected function _codeNode ( $ type = null , $ coverable = false ) { $ body = $ this -> _states [ 'body' ] ; if ( $ body === '' ) { return ; } $ node = new NodeDef ( $ body , $ type ? : $ this -> _codeType ( ) ) ; return $ this -> _contextualize ( $ node , $ coverable ) ; }
Build a code node .
27,333
protected function _stringNode ( $ delimiter = '' , $ heredoc = false ) { $ this -> _codeNode ( ) ; $ token = $ this -> _stream -> current ( true ) ; if ( ! $ delimiter ) { $ this -> _states [ 'body' ] = $ token [ 1 ] ; } elseif ( $ delimiter === '"' ) { $ this -> _states [ 'body' ] = $ token [ 1 ] . $ this -> _stream -> next ( '"' ) ; } else { $ this -> _states [ 'body' ] = $ token [ 1 ] . $ this -> _stream -> nextSequence ( $ delimiter ) ; } if ( $ heredoc ) { $ this -> _states [ 'body' ] .= $ this -> _stream -> next ( [ ';' ] ) ; } $ node = new NodeDef ( $ this -> _states [ 'body' ] , 'string' ) ; $ this -> _contextualize ( $ node ) ; return $ node ; }
Build a string node .
27,334
protected function _commentNode ( ) { $ this -> _codeNode ( ) ; $ token = $ this -> _stream -> current ( true ) ; $ this -> _states [ 'body' ] = $ token [ 1 ] ; $ node = new NodeDef ( $ this -> _states [ 'body' ] , 'comment' ) ; return $ this -> _contextualize ( $ node ) ; }
Build a comment node .
27,335
protected function _contextualize ( $ node , $ coverable = false ) { $ parent = $ this -> _states [ 'current' ] ; $ node -> namespace = $ parent -> namespace ; $ node -> function = $ parent -> function ; $ node -> parent = $ parent ; $ node -> coverable = $ parent -> hasMethods ? false : $ coverable ; $ parent -> tree [ ] = $ node ; $ this -> _assignLines ( $ node ) ; $ node -> inPhp = $ this -> _states [ 'php' ] ; $ this -> _states [ 'body' ] = '' ; return $ node ; }
Contextualize a node .
27,336
protected function _initLines ( $ content ) { if ( ! $ this -> _states [ 'lines' ] ) { return ; } $ lines = explode ( "\n" , $ content ) ; $ nbLines = count ( $ lines ) ; if ( $ this -> _states [ 'lines' ] ) { for ( $ i = 0 ; $ i < $ nbLines ; $ i ++ ) { $ this -> _root -> lines [ 'content' ] [ $ i ] = [ 'body' => $ lines [ $ i ] , 'nodes' => [ ] , 'coverable' => false ] ; } } }
Adds lines stores for root node .
27,337
protected function _assignLines ( $ node ) { if ( ! $ this -> _states [ 'lines' ] ) { return ; } $ body = $ node -> body ; $ num = $ this -> _states [ 'num' ] ; $ lines = explode ( "\n" , $ body ) ; $ nb = count ( $ lines ) - 1 ; $ this -> _states [ 'num' ] += $ nb ; foreach ( $ lines as $ i => $ line ) { $ this -> _assignLine ( $ num + $ i , $ node , $ line ) ; } $ node -> parent -> lines [ 'stop' ] = $ this -> _states [ 'num' ] - ( trim ( $ lines [ $ nb ] ) ? 0 : 1 ) ; }
Assign the node to some lines and makes them availaible at the root node .
27,338
protected function _assignLine ( $ index , $ node , $ line ) { if ( $ node -> lines [ 'start' ] === null ) { $ node -> lines [ 'start' ] = $ index ; } $ node -> lines [ 'stop' ] = $ index ; if ( trim ( $ line ) ) { $ this -> _root -> lines [ 'content' ] [ $ index ] [ 'nodes' ] [ ] = $ node ; } }
Assign a node to a specific line .
27,339
protected function _assignCoverable ( ) { if ( ! $ this -> _states [ 'lines' ] ) { return ; } foreach ( $ this -> _root -> lines [ 'content' ] as $ index => $ value ) { $ this -> _root -> lines [ 'content' ] [ $ index ] [ 'coverable' ] = $ this -> _isCoverable ( $ index ) ; } }
Assign coverable data to lines .
27,340
protected function _isCoverable ( $ index ) { $ coverable = false ; foreach ( $ this -> _root -> lines [ 'content' ] [ $ index ] [ 'nodes' ] as $ node ) { if ( $ node -> coverable && ( $ node -> lines [ 'stop' ] === $ index ) ) { $ coverable = true ; } } return $ coverable ; }
Checks if a specific line is coverable .
27,341
public static function debug ( $ content ) { $ root = is_object ( $ content ) ? $ content : static :: parse ( $ content , [ 'lines' => true ] ) ; $ result = '' ; $ abbr = [ 'file' => 'file' , 'open' => 'open' , 'close' => 'close' , 'declare' => 'declare' , 'namespace' => 'namespace' , 'use' => 'use' , 'class' => 'class' , 'interface' => 'interface' , 'trait' => 'trait' , 'function' => 'function' , 'signature' => 'signature' , 'attribute' => 'a' , 'code' => 'c' , 'comment' => 'd' , 'plain' => 'p' , 'string' => 's' ] ; foreach ( $ root -> lines [ 'content' ] as $ num => $ content ) { $ start = $ stop = $ line = $ num + 1 ; $ result .= '#' . str_pad ( $ line , 6 , ' ' ) ; $ types = [ ] ; foreach ( $ content [ 'nodes' ] as $ node ) { $ types [ ] = $ abbr [ $ node -> type ] ; $ stop = max ( $ stop , $ node -> lines [ 'stop' ] + 1 ) ; } $ result .= $ content [ 'coverable' ] ? '*' : ' ' ; $ result .= '[' . str_pad ( join ( ',' , $ types ) , 19 , ' ' , STR_PAD_BOTH ) . "]" ; $ result .= ' ' . str_pad ( "#{$start} > #{$stop}" , 16 , ' ' ) . "|" ; $ result .= $ content [ 'body' ] . "\n" ; } return $ result ; }
Returns a reader - friendly output for debug purpose .
27,342
public static function scan ( $ path , $ options = [ ] ) { $ defaults = [ 'iterator' => RecursiveIteratorIterator :: SELF_FIRST , 'skipDots' => true , 'leavesOnly' => false , 'followSymlinks' => true , 'recursive' => true ] ; $ options += $ defaults ; $ paths = ( array ) $ path ; $ dirFlags = static :: _dirFlags ( $ options ) ; $ iteratorFlags = static :: _iteratorFlags ( $ options ) ; $ result = [ ] ; foreach ( $ paths as $ path ) { $ result = array_merge ( $ result , static :: _scan ( $ path , $ options , $ dirFlags , $ iteratorFlags ) ) ; } return $ result ; }
Scans one or many directories for files .
27,343
protected static function _scan ( $ path , $ options , $ dirFlags , $ iteratorFlags ) { if ( ! file_exists ( $ path ) ) { throw new Exception ( "Unexisting path `{$path}`." ) ; } if ( ! is_dir ( $ path ) ) { return [ $ path ] ; } $ worker = new RecursiveDirectoryIterator ( $ path , $ dirFlags ) ; if ( $ options [ 'recursive' ] ) { $ worker = new RecursiveIteratorIterator ( $ worker , $ iteratorFlags ) ; } $ filter = new static ( $ worker ) ; $ filter -> filter ( $ options ) ; $ result = [ ] ; foreach ( $ filter as $ key => $ value ) { $ result [ ] = $ key ; } return $ result ; }
Scans a given directory for files .
27,344
public static function _dirFlags ( $ options ) { $ flag = $ options [ 'followSymlinks' ] ? FilesystemIterator :: FOLLOW_SYMLINKS : 0 ; $ flag |= $ options [ 'skipDots' ] ? FilesystemIterator :: SKIP_DOTS : 0 ; $ flag |= FilesystemIterator :: UNIX_PATHS ; return $ flag ; }
Returns FilesystemIterator flags from Dir options .
27,345
public static function tempnam ( $ path = null , $ prefix = '' ) { if ( $ path === null ) { $ path = sys_get_temp_dir ( ) ; } if ( $ tempfile = tempnam ( $ path , $ prefix ) ) { unlink ( $ tempfile ) ; mkdir ( $ tempfile ) ; } return $ tempfile ; }
Creates a directory with unique file name .
27,346
public function filter ( $ options = [ ] ) { $ defaults = array ( 'include' => [ '*' ] , 'exclude' => [ ] , 'type' => [ ] ) ; $ options += $ defaults ; $ this -> _exclude = ( array ) $ options [ 'exclude' ] ; $ this -> _include = ( array ) $ options [ 'include' ] ; $ this -> _types = ( array ) $ options [ 'type' ] ; }
Applies some filters to a FilterIterator instance .
27,347
public function accept ( ) { $ path = $ this -> current ( ) -> getPathname ( ) ; if ( $ this -> _excluded ( $ path ) ) { return false ; } if ( ! $ this -> _included ( $ path ) ) { return false ; } return $ this -> _matchType ( ) ; }
Checks if a file passes the setted filters .
27,348
protected function _excluded ( $ path ) { foreach ( $ this -> _exclude as $ exclude ) { if ( fnmatch ( $ exclude , $ path ) ) { return true ; } } return false ; }
Checks if a file passes match an excluded path .
27,349
protected function _matchType ( ) { if ( ! $ this -> _types ) { return true ; } $ file = $ this -> current ( ) ; foreach ( $ this -> _types as $ type ) { $ method = 'is' . ucfirst ( $ type ) ; if ( $ file -> $ method ( ) ) { return true ; } } return false ; }
Checks if a file passes match the allowed type .
27,350
public function stats ( ) { if ( $ this -> _stats !== null ) { return $ this -> _stats ; } Suite :: push ( $ this ) ; $ builder = function ( $ block ) { $ block -> load ( ) ; $ normal = 0 ; $ inactive = 0 ; $ focused = 0 ; $ excluded = 0 ; foreach ( $ block -> children ( ) as $ child ) { if ( $ block -> excluded ( ) ) { $ child -> type ( 'exclude' ) ; } if ( $ child instanceof Group ) { $ result = $ child -> stats ( ) ; if ( $ child -> focused ( ) && ! $ result [ 'focused' ] ) { $ focused += $ result [ 'normal' ] ; $ excluded += $ result [ 'excluded' ] ; $ child -> broadcastFocus ( ) ; } elseif ( ! $ child -> enabled ( ) ) { $ inactive += $ result [ 'normal' ] ; $ focused += $ result [ 'focused' ] ; $ excluded += $ result [ 'excluded' ] ; } else { $ normal += $ result [ 'normal' ] ; $ focused += $ result [ 'focused' ] ; $ excluded += $ result [ 'excluded' ] ; } } else { switch ( $ child -> type ( ) ) { case 'exclude' : $ excluded ++ ; break ; case 'focus' : $ focused ++ ; break ; default : $ normal ++ ; break ; } } } return compact ( 'normal' , 'inactive' , 'focused' , 'excluded' ) ; } ; if ( Suite :: $ PHP >= 7 && ! defined ( 'HHVM_VERSION' ) ) { try { $ stats = $ builder ( $ this ) ; } catch ( Throwable $ exception ) { $ this -> log ( ) -> type ( 'errored' ) ; $ this -> log ( ) -> exception ( $ exception ) ; $ stats = [ 'normal' => 0 , 'focused' => 0 , 'excluded' => 0 ] ; } } else { $ stats = $ builder ( $ this ) ; } Suite :: pop ( ) ; return $ stats ; }
Builds the group stats .
27,351
public function partition ( $ index , $ total ) { $ index = ( integer ) $ index ; $ total = ( integer ) $ total ; if ( ! $ index || ! $ total || $ index > $ total ) { throw new Exception ( "Invalid partition parameters: {$index}/{$total}" ) ; } $ groups = [ ] ; $ partitions = [ ] ; $ partitionsTotal = [ ] ; for ( $ i = 0 ; $ i < $ total ; $ i ++ ) { $ partitions [ $ i ] = [ ] ; $ partitionsTotal [ $ i ] = 0 ; } $ children = $ this -> children ( ) ; foreach ( $ children as $ key => $ child ) { $ groups [ $ key ] = $ child -> stats ( ) [ 'normal' ] ; $ child -> enabled ( false ) ; } asort ( $ groups ) ; foreach ( $ groups as $ key => $ value ) { $ i = array_search ( min ( $ partitionsTotal ) , $ partitionsTotal ) ; $ partitions [ $ i ] [ ] = $ key ; $ partitionsTotal [ $ i ] += $ groups [ $ key ] ; } foreach ( $ partitions [ $ index - 1 ] as $ key ) { $ children [ $ key ] -> enabled ( true ) ; } }
Splits the specs in different partitions and only enable one .
27,352
public function load ( ) { if ( $ this -> _loaded ) { return ; } $ this -> _loaded = true ; if ( ! $ closure = $ this -> closure ( ) ) { return ; } return $ this -> _suite -> runBlock ( $ this , $ closure , 'group' ) ; }
Load the group .
27,353
protected function _execute ( ) { if ( ! $ this -> enabled ( ) && ! $ this -> focused ( ) ) { return ; } foreach ( $ this -> _children as $ child ) { if ( $ this -> suite ( ) -> failfast ( ) ) { break ; } $ this -> _passed = $ child -> process ( ) && $ this -> _passed ; } }
Group execution helper .
27,354
protected function _blockEnd ( $ runAfterAll = true ) { if ( $ runAfterAll ) { if ( Suite :: $ PHP >= 7 && ! defined ( 'HHVM_VERSION' ) ) { try { $ this -> runCallbacks ( 'afterAll' , false ) ; } catch ( Throwable $ exception ) { $ this -> _exception ( $ exception ) ; } } else { try { $ this -> runCallbacks ( 'afterAll' , false ) ; } catch ( Exception $ exception ) { $ this -> _exception ( $ exception ) ; } } } $ this -> suite ( ) -> autoclear ( ) ; $ type = $ this -> log ( ) -> type ( ) ; if ( $ type === 'failed' || $ type === 'errored' ) { $ this -> _passed = false ; $ this -> suite ( ) -> failure ( ) ; $ this -> summary ( ) -> log ( $ this -> log ( ) ) ; } $ this -> report ( 'suiteEnd' , $ this ) ; }
End group block execution helper .
27,355
public function runCallbacks ( $ name , $ recursive = true ) { $ instances = $ recursive ? $ this -> parents ( true ) : [ $ this ] ; if ( strncmp ( $ name , 'after' , 5 ) === 0 ) { $ instances = array_reverse ( $ instances ) ; } foreach ( $ instances as $ instance ) { foreach ( $ instance -> _callbacks [ $ name ] as $ closure ) { $ this -> _suite -> runBlock ( $ this , $ closure , $ name ) ; } } }
Runs a callback .
27,356
public function broadcastFocus ( ) { foreach ( $ this -> _children as $ child ) { if ( $ child -> type ( ) !== 'normal' ) { continue ; } $ child -> type ( 'focus' ) ; if ( $ child instanceof Group ) { $ child -> broadcastFocus ( ) ; } } }
Apply focus downward to the leaf .
27,357
public function expectation ( ) { $ total = 0 ; foreach ( $ this -> _logs as $ key => $ value ) { foreach ( $ value as $ log ) { $ total += count ( $ log -> children ( ) ) ; } } return $ total ; }
Return the total number of expectations .
27,358
public function add ( $ type , $ value ) { if ( ! isset ( $ this -> _data [ $ type ] ) ) { $ this -> _data [ $ type ] = [ ] ; } $ this -> _data [ $ type ] [ ] = $ value ; return $ this ; }
Add a data to a specific key .
27,359
public function log ( $ log ) { $ type = $ log -> type ( ) ; if ( ! isset ( $ this -> _logs [ $ type ] ) ) { $ this -> _logs [ $ type ] = [ ] ; } $ this -> _logs [ $ type ] [ ] = $ log ; return $ this ; }
Ingest a log .
27,360
public function logs ( $ type = null ) { if ( func_num_args ( ) ) { return isset ( $ this -> _logs [ $ type ] ) ? $ this -> _logs [ $ type ] : [ ] ; } $ logs = [ ] ; foreach ( $ this -> _logs as $ key => $ value ) { $ logs = array_merge ( $ logs , $ value ) ; } return $ logs ; }
Get log report
27,361
public function metrics ( ) { $ this -> _start = microtime ( true ) ; $ result = $ this -> _collector -> metrics ( ) ; $ this -> _time = microtime ( true ) - $ this -> _start ; return $ result ; }
Gets the metrics about the coverage result .
27,362
protected function _renderMetrics ( $ metrics , $ verbosity ) { $ maxLabelWidth = null ; if ( $ verbosity === 1 ) { return ; } $ metricsReport = $ this -> _getMetricsReport ( $ metrics -> children ( ) , $ verbosity , 0 , 3 , $ maxLabelWidth ) ; $ name = $ metrics -> name ( ) ? : '\\' ; $ maxLabelWidth = max ( strlen ( $ name ) + 1 , $ maxLabelWidth ) ; $ maxLabelWidth += 4 ; $ stats = $ metrics -> data ( ) ; $ percent = number_format ( $ stats [ 'percent' ] , 2 ) ; $ style = $ this -> _style ( $ percent ) ; $ maxLineWidth = strlen ( $ stats [ 'lloc' ] ) ; $ this -> write ( str_repeat ( ' ' , $ maxLabelWidth ) ) ; $ this -> write ( ' ' ) ; $ this -> write ( str_pad ( 'Lines' , $ maxLineWidth * 2 + 3 , ' ' , STR_PAD_BOTH ) ) ; $ this -> write ( str_pad ( '%' , 12 , ' ' , STR_PAD_LEFT ) ) ; $ this -> write ( "\n\n" ) ; $ this -> write ( str_pad ( ' ' . $ name , $ maxLabelWidth ) ) ; $ this -> write ( ' ' ) ; $ this -> write ( str_pad ( "{$stats['cloc']}" , $ maxLineWidth , ' ' , STR_PAD_LEFT ) ) ; $ this -> write ( ' / ' ) ; $ this -> write ( str_pad ( "{$stats['lloc']}" , $ maxLineWidth , ' ' , STR_PAD_LEFT ) ) ; $ this -> write ( ' ' ) ; $ this -> write ( str_pad ( "{$percent}%" , 7 , ' ' , STR_PAD_LEFT ) , $ style ) ; $ this -> write ( "\n" ) ; $ this -> _renderMetricsReport ( $ metricsReport , $ maxLabelWidth , $ maxLineWidth , 0 ) ; }
Outputs some metrics info where the metric is not the total coverage .
27,363
protected function _getMetricsReport ( $ children , $ verbosity , $ depth = 0 , $ tab = 3 , & $ maxWidth = null ) { $ list = [ ] ; foreach ( $ children as $ child ) { $ type = $ child -> type ( ) ; if ( $ verbosity === 2 && $ type !== 'namespace' ) { continue ; } if ( $ verbosity === 3 && ( $ type === 'function' || $ type === 'method' ) ) { continue ; } $ name = $ child -> name ( ) ; if ( $ name !== '\\' ) { $ pos = strrpos ( $ name , '\\' , $ type === 'namespace' ? - 2 : 0 ) ; $ basename = substr ( $ name , $ pos !== false ? $ pos + 1 : 0 ) ; } else { $ basename = '\\' ; } $ len = strlen ( $ basename ) + ( $ depth + 1 ) * $ tab ; if ( $ len > $ maxWidth ) { $ maxWidth = $ len ; } $ list [ $ basename ] = [ 'metrics' => $ child , 'children' => $ this -> _getMetricsReport ( $ child -> children ( ) , $ verbosity , $ depth + 1 , $ tab , $ maxWidth ) ] ; } return $ list ; }
Extract some metrics reports to display according to a verbosity parameter .
27,364
protected function _renderCoverage ( $ metrics ) { $ stats = $ metrics -> data ( ) ; foreach ( $ stats [ 'files' ] as $ file ) { $ this -> write ( "File: {$file}" . "\n\n" ) ; $ lines = file ( $ file ) ; $ coverage = $ this -> _collector -> export ( $ file ) ; if ( isset ( $ stats [ 'line' ] ) ) { $ start = $ stats [ 'line' ] [ 'start' ] ; $ stop = $ stats [ 'line' ] [ 'stop' ] ; } else { $ start = 0 ; $ stop = count ( $ lines ) - 1 ; } for ( $ i = $ start ; $ i <= $ stop ; $ i ++ ) { $ value = isset ( $ coverage [ $ i ] ) ? $ coverage [ $ i ] : null ; $ line = str_pad ( $ i + 1 , 6 , ' ' , STR_PAD_LEFT ) ; $ line .= ':' . str_pad ( $ value , 6 , ' ' ) ; $ line .= $ lines [ $ i ] ; if ( $ value ) { $ this -> write ( $ line , 'n;green' ) ; } elseif ( $ value === 0 ) { $ this -> write ( $ line , 'n;red' ) ; } else { $ this -> write ( $ line ) ; } } $ this -> write ( "\n\n" ) ; } }
Outputs the coverage report of a metrics instance .
27,365
public function stop ( $ summary ) { $ this -> write ( "Coverage Summary\n----------------\n" ) ; $ verbosity = $ this -> _verbosity ; $ metrics = is_numeric ( $ this -> _verbosity ) ? $ this -> metrics ( ) : $ this -> metrics ( ) -> get ( $ verbosity ) ; if ( ! $ metrics ) { $ this -> write ( "\nUnexisting namespace/reference: `{$this->_verbosity}`, coverage can't be generated.\n\n" , "n;yellow" ) ; return ; } $ this -> _renderMetrics ( $ metrics , $ verbosity ) ; $ this -> write ( "\n" ) ; if ( is_string ( $ verbosity ) ) { $ this -> _renderCoverage ( $ metrics ) ; $ this -> write ( "\n" ) ; } $ name = $ metrics -> name ( ) ; $ stats = $ metrics -> data ( ) ; $ percent = number_format ( $ stats [ 'percent' ] , 2 ) ; $ this -> write ( str_repeat ( ' ' , substr_count ( $ name , '\\' ) ) ) ; $ this -> write ( 'Total: ' ) ; $ this -> write ( "{$percent}% " , $ this -> _style ( $ percent ) ) ; $ this -> write ( "({$stats['cloc']}/{$stats['lloc']})" ) ; $ this -> write ( "\n" ) ; $ time = number_format ( $ this -> _time , 3 ) ; $ this -> write ( "\nCoverage collected in {$time} seconds\n\n\n" ) ; }
Callback called at the end of the process .
27,366
public static function actual ( $ actual ) { if ( is_string ( $ actual ) ) { return strlen ( $ actual ) ; } elseif ( is_array ( $ actual ) || $ actual instanceof Countable ) { return count ( $ actual ) ; } }
Normalize the actual value in the expected format .
27,367
public function match ( $ call , $ withArgs = true ) { if ( preg_match ( '/^::.*/' , $ call [ 'name' ] ) ) { $ call [ 'static' ] = true ; $ call [ 'name' ] = substr ( $ call [ 'name' ] , 2 ) ; } if ( isset ( $ call [ 'static' ] ) ) { if ( $ call [ 'static' ] !== $ this -> _static ) { return false ; } } if ( $ call [ 'name' ] !== $ this -> _name ) { return false ; } if ( $ withArgs ) { return $ this -> matchArgs ( $ call [ 'args' ] ) ; } return true ; }
Check if this message is compatible with passed call array .
27,368
public function matchArgs ( $ args ) { if ( $ this -> _args === null || $ args === null ) { return true ; } $ arg = $ this -> _classes [ 'arg' ] ; foreach ( $ this -> _args as $ expected ) { $ actual = array_shift ( $ args ) ; if ( $ expected instanceof $ arg ) { if ( ! $ expected -> match ( $ actual ) ) { return false ; } } elseif ( $ actual !== $ expected ) { return false ; } } return true ; }
Check if this stub is compatible with passed args .
27,369
public static function expected ( $ expected , $ code = 0 ) { if ( $ expected === null || is_string ( $ expected ) ) { return new AnyException ( $ expected , $ code ) ; } return $ expected ; }
Normalise the expected value as an Exception .
27,370
public static function _matchException ( $ actual , $ exception ) { if ( ! $ actual ) { return false ; } if ( $ exception instanceof AnyException ) { $ code = $ exception -> getCode ( ) ? $ actual -> getCode ( ) : $ exception -> getCode ( ) ; $ class = get_class ( $ actual ) ; } else { $ code = $ actual -> getCode ( ) ; $ class = get_class ( $ exception ) ; } if ( get_class ( $ actual ) !== $ class ) { return false ; } $ sameCode = $ code === $ exception -> getCode ( ) ; $ sameMessage = static :: _sameMessage ( $ actual -> getMessage ( ) , $ exception -> getMessage ( ) ) ; return $ sameCode && $ sameMessage ; }
Compares if two exception are similar .
27,371
public static function _sameMessage ( $ actual , $ expected ) { if ( preg_match ( '~^(?P<char>\~|/|@|#).*?(?P=char)$~' , ( string ) $ expected ) ) { $ same = preg_match ( $ expected , $ actual ) ; } else { $ same = $ actual === $ expected ; } return $ same || ! $ expected ; }
Compare if exception messages are similar .
27,372
public function argsToParams ( ) { $ args = [ ] ; foreach ( $ this -> args as $ key => $ value ) { $ value = is_int ( $ key ) ? $ value : $ key ; preg_match ( "/(\\\$[\\\a-z_\\x7f-\\xff][a-z0-9_\\x7f-\\xff]*)/i" , $ value , $ match ) ; $ args [ ] = $ match [ 1 ] ; } return join ( ', ' , $ args ) ; }
Returns function s arguments into a list of callable parameters
27,373
public function factory ( $ name , $ definition ) { if ( ! is_string ( $ definition ) && ! $ definition instanceof Closure ) { throw new BoxException ( "Error `{$name}` is not a closure definition dependency can't use it as a factory definition." ) ; } $ this -> _set ( $ name , $ definition , 'factory' ) ; }
Defining a factory .
27,374
protected function _set ( $ name , $ definition , $ type ) { if ( $ definition instanceof Closure ) { $ definition = $ definition -> bindTo ( $ this , get_class ( $ this ) ) ; } $ this -> _definitions [ $ name ] = compact ( 'definition' , 'type' ) ; }
Stores a dependency definition .
27,375
public function get ( $ name ) { if ( ! isset ( $ this -> _definitions [ $ name ] ) ) { throw new BoxException ( "Unexisting `{$name}` definition dependency." ) ; } extract ( $ this -> _definitions [ $ name ] ) ; if ( $ type === 'singleton' ) { return $ definition ; } $ params = func_get_args ( ) ; array_shift ( $ params ) ; if ( $ type === 'service' ) { return $ definition = $ this -> _service ( $ name , $ definition , $ params ) ; } return $ definition = $ this -> _factory ( $ definition , $ params ) ; }
Gets a shared variable or an new instance .
27,376
public function wrap ( $ name ) { if ( ! isset ( $ this -> _definitions [ $ name ] ) ) { throw new BoxException ( "Unexisting `{$name}` definition dependency." ) ; } if ( ! $ this -> _definitions [ $ name ] [ 'definition' ] instanceof Closure ) { throw new BoxException ( "Error `{$name}` is not a closure definition dependency can't be wrapped." ) ; } $ params = func_get_args ( ) ; array_shift ( $ params ) ; $ wrapper = $ this -> _classes [ 'wrapper' ] ; return new $ wrapper ( [ 'box' => $ this , 'name' => $ name , 'params' => $ params ] ) ; }
Returns a dependency container .
27,377
protected function _service ( $ name , $ definition , $ params ) { if ( $ definition instanceof Closure ) { $ type = 'singleton' ; $ definition = call_user_func_array ( $ definition , $ params ) ; $ this -> _definitions [ $ name ] = compact ( 'definition' , 'type' ) ; } return $ definition ; }
Process a shared definition .
27,378
protected function _factory ( $ definition , $ params ) { if ( is_string ( $ definition ) ) { if ( $ params ) { $ refl = new ReflectionClass ( $ definition ) ; return $ refl -> newInstanceArgs ( $ params ) ; } else { return new $ definition ( ) ; } } return call_user_func_array ( $ definition , $ params ) ; }
Process a setted definition .
27,379
public function get ( ) { if ( $ this -> __resolved ) { return $ this -> __dependency ; } $ this -> __resolved = true ; $ params = func_num_args ( ) === 0 ? $ this -> __params : func_get_args ( ) ; array_unshift ( $ params , $ this -> __name ) ; return $ this -> __dependency = call_user_func_array ( [ $ this -> __box , 'get' ] , $ params ) ; }
Resolve the dependency .
27,380
protected function _flushVariables ( $ node ) { if ( ! $ this -> _variables [ $ this -> _depth ] ) { return ; } $ body = '' ; foreach ( $ this -> _variables [ $ this -> _depth ] as $ variable ) { $ body .= $ variable [ 'name' ] . $ variable [ 'patch' ] ; } if ( ! $ node -> inPhp ) { $ body = '<?php ' . $ body . ' ?>' ; } $ patch = new NodeDef ( $ body , 'code' ) ; $ patch -> parent = $ node ; $ patch -> function = $ node -> function ; $ patch -> namespace = $ node -> namespace ; array_unshift ( $ node -> tree , $ patch ) ; $ this -> _variables [ $ this -> _depth ] = [ ] ; }
Flush stored variables in the passed node .
27,381
protected function _addClosingParenthesis ( $ pos , $ index , $ parent ) { $ count = 0 ; $ nodes = $ parent -> tree ; $ total = count ( $ nodes ) ; for ( $ i = $ index ; $ i < $ total ; $ i ++ ) { $ node = $ nodes [ $ i ] ; if ( ! $ node -> processable || $ node -> type !== 'code' ) { if ( ! $ node -> close ) { continue ; } $ code = & $ node -> close ; } else { $ code = & $ node -> body ; } $ len = strlen ( $ code ) ; while ( $ pos < $ len ) { if ( $ count === 0 && $ code [ $ pos ] === ';' ) { $ code = substr_replace ( $ code , ');' , $ pos , 1 ) ; return true ; } elseif ( $ code [ $ pos ] === '(' ) { $ count ++ ; } elseif ( $ code [ $ pos ] === ')' ) { $ count -- ; if ( $ count === 0 ) { $ code = substr_replace ( $ code , $ code [ $ pos ] . ')' , $ pos , 1 ) ; return true ; } } $ pos ++ ; } $ pos = 0 ; } return false ; }
Add a closing parenthesis
27,382
public static function blacklisted ( $ name = null ) { if ( ! func_num_args ( ) ) { return array_keys ( static :: $ _blacklist ) ; } return isset ( static :: $ _blacklist [ strtolower ( $ name ) ] ) ; }
Check if a function is part of the blacklisted ones .
27,383
public static function expected ( $ expected ) { if ( $ expected === 'bool' ) { $ expected = 'boolean' ; } if ( $ expected === 'int' ) { $ expected = 'integer' ; } if ( $ expected === 'float' ) { $ expected = 'double' ; } return strtolower ( $ expected ) ; }
Normalises the expected value .
27,384
public static function instance ( $ options = [ ] ) { $ class = static :: classname ( $ options ) ; if ( isset ( $ options [ 'args' ] ) ) { $ refl = new ReflectionClass ( $ class ) ; $ instance = $ refl -> newInstanceArgs ( $ options [ 'args' ] ) ; } else { $ instance = new $ class ( ) ; } if ( isset ( $ options [ 'stubMethods' ] ) && is_array ( $ options [ 'stubMethods' ] ) ) { foreach ( $ options [ 'stubMethods' ] as $ name => $ return ) { allow ( $ instance ) -> toReceive ( $ name ) -> andReturn ( $ return ) ; } } if ( isset ( $ options [ 'fakeMethods' ] ) && is_array ( $ options [ 'fakeMethods' ] ) ) { foreach ( $ options [ 'fakeMethods' ] as $ name => $ callback ) { allow ( $ instance ) -> toReceive ( $ name ) -> andRun ( $ callback ) ; } } return $ instance ; }
Creates a polyvalent instance .
27,385
public static function classname ( $ options = [ ] ) { $ defaults = [ 'class' => 'Kahlan\Spec\Plugin\Double\Double' . static :: $ _index ++ ] ; $ options += $ defaults ; if ( ! static :: $ _pointcut ) { $ pointcut = static :: $ _classes [ 'pointcut' ] ; static :: $ _pointcut = new $ pointcut ( ) ; } if ( ! class_exists ( $ options [ 'class' ] , false ) ) { $ parser = static :: $ _classes [ 'parser' ] ; $ code = static :: generate ( $ options ) ; $ nodes = $ parser :: parse ( $ code ) ; $ code = $ parser :: unparse ( static :: $ _pointcut -> process ( $ nodes ) ) ; eval ( '?>' . $ code ) ; } return $ options [ 'class' ] ; }
Creates a polyvalent static class .
27,386
public static function generate ( $ options = [ ] ) { $ defaults = [ 'class' => 'Kahlan\Spec\Plugin\Double\Double' . static :: $ _index ++ , 'extends' => '' , 'implements' => [ ] , 'uses' => [ ] , 'methods' => [ ] , 'layer' => null , 'openTag' => true , 'closeTag' => true ] ; $ options += $ defaults ; if ( $ options [ 'extends' ] || $ options [ 'implements' ] ) { $ options += [ 'magicMethods' => false ] ; } else { $ options += [ 'magicMethods' => true ] ; } $ class = $ options [ 'class' ] ; $ namespace = '' ; if ( ( $ pos = strrpos ( $ class , '\\' ) ) !== false ) { $ namespace = substr ( $ class , 0 , $ pos ) ; $ class = substr ( $ class , $ pos + 1 ) ; } if ( $ namespace ) { $ namespace = "namespace {$namespace};\n" ; } $ uses = static :: _generateUses ( $ options [ 'uses' ] ) ; $ extends = static :: _generateExtends ( $ options [ 'extends' ] ) ; $ implements = static :: _generateImplements ( $ options [ 'implements' ] ) ; $ methods = static :: _generateMethodStubs ( $ options [ 'methods' ] , $ options [ 'magicMethods' ] ) ; if ( $ options [ 'extends' ] ) { $ methods += static :: _generateClassMethods ( $ options [ 'extends' ] , $ options [ 'layer' ] ) ; } $ methods += static :: _generateInterfaceMethods ( $ options [ 'implements' ] ) ; $ methods = $ methods ? ' ' . join ( "\n " , $ methods ) : '' ; $ openTag = $ options [ 'openTag' ] ? "<?php\n" : '' ; $ closeTag = $ options [ 'closeTag' ] ? "?>" : '' ; return $ openTag . $ namespace . <<<EOTclass {$class}{$extends}{$implements} {{$uses}{$methods}}$closeTagEOT ; }
Creates a class definition .
27,387
protected static function _generateUses ( $ uses ) { if ( ! $ uses ) { return '' ; } $ traits = [ ] ; foreach ( ( array ) $ uses as $ use ) { if ( ! trait_exists ( $ use ) ) { throw new MissingImplementationException ( "Unexisting trait `{$use}`" ) ; } $ traits [ ] = '\\' . ltrim ( $ use , '\\' ) ; } return ' use ' . join ( ', ' , $ traits ) . ';' ; }
Creates a use definition .
27,388
protected static function _generateMethodStubs ( $ methods , $ defaults = true ) { $ result = [ ] ; $ methods = $ methods !== null ? ( array ) $ methods : [ ] ; if ( $ defaults ) { $ methods = array_merge ( $ methods , array_keys ( static :: _getMagicMethods ( ) ) ) ; } $ methods = array_unique ( $ methods ) ; $ magicMethods = static :: _getMagicMethods ( ) ; foreach ( $ methods as $ name ) { if ( isset ( $ magicMethods [ $ name ] ) ) { $ result [ $ name ] = $ magicMethods [ $ name ] ; } else { $ static = $ return = '' ; if ( $ name [ 0 ] === '&' ) { $ return = '$r = null; return $r;' ; } if ( preg_match ( '/^&?::.*/' , $ name ) ) { $ static = 'static ' ; $ name = substr ( $ name , 2 ) ; } $ result [ $ name ] = "public {$static}function {$name}() {{$return}}" ; } } return $ result ; }
Creates method stubs .
27,389
protected static function _generateInterfaceMethods ( $ interfaces , $ mask = 255 ) { if ( ! $ interfaces ) { return [ ] ; } $ result = [ ] ; foreach ( ( array ) $ interfaces as $ interface ) { if ( ! interface_exists ( $ interface ) ) { throw new MissingImplementationException ( "Unexisting interface `{$interface}`" ) ; } $ reflection = Inspector :: inspect ( $ interface ) ; $ methods = $ reflection -> getMethods ( $ mask ) ; foreach ( $ methods as $ method ) { $ result [ $ method -> getName ( ) ] = static :: _generateMethod ( $ method ) ; } } return $ result ; }
Creates method definitions from an interface array .
27,390
protected static function _generateMethod ( $ method , $ callParent = false ) { $ result = join ( ' ' , Reflection :: getModifierNames ( $ method -> getModifiers ( ) ) ) ; $ result = preg_replace ( '/abstract\s*/' , '' , $ result ) ; $ name = $ method -> getName ( ) ; $ parameters = static :: _generateSignature ( $ method ) ; $ type = static :: _generateReturnType ( $ method ) ; $ body = "{$result} function {$name}({$parameters}) {$type}{" ; if ( $ callParent ) { $ parameters = static :: _generateParameters ( $ method ) ; $ return = 'return ' ; if ( $ method -> isConstructor ( ) || $ method -> isDestructor ( ) ) { $ return = '' ; } $ body .= "{$return}parent::{$name}({$parameters});" ; } return $ body . "}" ; }
Creates a method definition from a ReflectionMethod instance .
27,391
protected static function _generateReturnType ( $ method ) { if ( Suite :: $ PHP < 7 ) { return '' ; } $ type = $ method -> getReturnType ( ) ; $ allowsNull = '' ; if ( $ type ) { if ( method_exists ( $ type , 'allowsNull' ) && $ type -> allowsNull ( ) ) { $ allowsNull = '?' ; } if ( ! $ type -> isBuiltin ( ) ) { $ type = '\\' . $ type ; } if ( defined ( 'HHVM_VERSION' ) ) { $ type = preg_replace ( '~\\\?HH\\\(mixed|void)?~' , '' , $ type ) ; } } return $ type ? ": {$allowsNull}{$type} " : '' ; }
Extract the return type of a method .
27,392
protected static function _generateSignature ( $ method ) { $ params = [ ] ; $ isVariadic = Suite :: $ PHP >= 7 ? $ method -> isVariadic ( ) : false ; foreach ( $ method -> getParameters ( ) as $ num => $ parameter ) { $ typehint = Inspector :: typehint ( $ parameter ) ; $ name = $ parameter -> getName ( ) ; $ name = ( $ name && $ name !== '...' ) ? $ name : 'param' . $ num ; $ reference = $ parameter -> isPassedByReference ( ) ? '&' : '' ; $ default = '' ; if ( $ parameter -> isDefaultValueAvailable ( ) ) { $ default = var_export ( $ parameter -> getDefaultValue ( ) , true ) ; $ default = ' = ' . preg_replace ( '/\s+/' , '' , $ default ) ; } elseif ( $ parameter -> isOptional ( ) ) { if ( $ isVariadic && $ parameter -> isVariadic ( ) ) { $ reference = '...' ; $ default = '' ; } else { $ default = ' = NULL' ; } } $ allowsNull = '' ; $ typehint = $ typehint ? $ typehint . ' ' : $ typehint ; if ( Suite :: $ PHP >= 7 ) { $ type = $ parameter -> getType ( ) ; if ( $ type && method_exists ( $ type , 'allowsNull' ) && $ type -> allowsNull ( ) ) { $ allowsNull = '?' ; } } $ params [ ] = "{$allowsNull}{$typehint}{$reference}\${$name}{$default}" ; } return join ( ', ' , $ params ) ; }
Creates a parameters signature of a ReflectionMethod instance .
27,393
protected static function _generateParameters ( $ method ) { $ params = [ ] ; foreach ( $ method -> getParameters ( ) as $ num => $ parameter ) { $ name = $ parameter -> getName ( ) ; $ name = ( $ name && $ name !== '...' ) ? $ name : 'param' . $ num ; $ params [ ] = "\${$name}" ; } return join ( ', ' , $ params ) ; }
Creates a parameters list from a ReflectionMethod instance .
27,394
public static function apply ( $ context , $ method , $ filter ) { static :: $ _cachedFilters = [ ] ; if ( is_object ( $ context ) ) { $ instance = $ context ; $ class = get_class ( $ context ) ; } else { $ class = $ context ; $ instance = null ; } $ filter = $ filter -> bindTo ( $ instance , $ class ) ; $ ref = static :: _ref ( $ context , $ method ) ; if ( ! isset ( static :: $ _filters [ $ ref ] ) ) { static :: $ _filters [ $ ref ] = [ $ filter ] ; } else { array_unshift ( static :: $ _filters [ $ ref ] , $ filter ) ; } return $ ref . '|' . ( count ( static :: $ _filters [ $ ref ] ) - 1 ) ; }
Applies a filter .
27,395
protected static function _ref ( $ context , $ method ) { if ( is_string ( $ context ) ) { return $ context . "::{$method}" ; } return get_class ( $ context ) . '#' . spl_object_hash ( $ context ) . "::{$method}" ; }
Generates a reference id from a provided callable definition .
27,396
public static function detach ( $ context , $ method = null ) { if ( func_num_args ( ) === 2 ) { $ value = static :: _ref ( $ context , $ method ) ; } else { $ value = $ context ; } if ( strpos ( $ value , '|' ) !== false ) { list ( $ ref , $ num ) = explode ( '|' , $ value ) ; if ( ! isset ( static :: $ _filters [ $ ref ] [ $ num ] ) ) { throw new Exception ( "Unexisting `'{$value}'` filter id." ) ; } $ result = static :: $ _filters [ $ ref ] [ $ num ] ; unset ( static :: $ _filters [ $ ref ] [ $ num ] ) ; } else { if ( ! isset ( static :: $ _filters [ $ value ] ) ) { throw new Exception ( "Unexisting `'{$value}'` filter reference id." ) ; } $ result = static :: $ _filters [ $ value ] ; unset ( static :: $ _filters [ $ value ] ) ; } return $ result ; }
Detaches a filter .
27,397
public static function run ( $ context , $ method , $ params , $ callback , $ filters = [ ] ) { if ( static :: $ _enabled ) { $ filters = array_merge ( $ filters , static :: filters ( $ context , $ method ) ) ; } $ filters [ ] = $ callback ; $ generator = static :: _generator ( $ filters ) ; $ next = function ( ) use ( $ params , $ generator , & $ next ) { $ args = func_get_args ( ) + $ params ; $ closure = $ generator -> current ( ) ; $ generator -> next ( ) ; array_unshift ( $ args , $ next ) ; return call_user_func_array ( $ closure , $ args ) ; } ; return call_user_func_array ( $ next , $ params ) ; }
Cuts the normal execution of a method to run all applied filter for the method .
27,398
public static function color ( $ string , $ options = null ) { if ( $ options === null ) { return $ string ; } if ( is_string ( $ options ) ) { $ options = explode ( ';' , $ options ) ; if ( strlen ( $ options [ 0 ] ) === 1 ) { $ options = array_pad ( $ options , 3 , 'default' ) ; $ options = array_combine ( [ 'style' , 'color' , 'background' ] , $ options ) ; } else { $ options = [ 'color' => reset ( $ options ) ] ; } } $ options += [ 'style' => 'default' , 'color' => 'default' , 'background' => 'default' ] ; $ format = "\e[" ; $ format .= static :: _vtstyle ( $ options [ 'style' ] ) . ';' ; $ format .= static :: _vtcolor ( $ options [ 'color' ] ) . ';' ; $ format .= static :: _vtbackground ( $ options [ 'background' ] ) . 'm' ; return $ format . $ string . "\e[0m" ; }
Return a VT100 colored string .
27,399
public static function toString ( $ value , $ options = [ ] ) { $ defaults = [ 'quote' => '"' , 'object' => [ 'method' => '__toString' ] , 'array' => [ 'indent' => 1 , 'char' => ' ' , 'multiplier' => 4 ] ] ; $ options += $ defaults ; $ options [ 'array' ] += $ defaults [ 'array' ] ; $ options [ 'object' ] += $ defaults [ 'object' ] ; if ( $ value instanceof Closure ) { return '`Closure`' ; } if ( is_array ( $ value ) ) { return static :: _arrayToString ( $ value , $ options ) ; } if ( is_object ( $ value ) ) { return static :: _objectToString ( $ value , $ options ) ; } return static :: dump ( $ value , $ options [ 'quote' ] ) ; }
Generate a string representation of arbitrary data .