@@ -30,8 +30,8 @@ function is_valid_target_dir( $target_dir, $source_dir ) {
3030 return false ;
3131 }
3232
33- // A Windows drive root, such as `C:` or `C:\ `.
34- if ( preg_match ( '/^[a-z]: \\\\?$/i ' , $ target_dir ) ) {
33+ // A Windows drive root, such as `C:`, `C:\`, or `C:/ `.
34+ if ( preg_match ( '/^[a-z]:[ \\\\\/] ?$/i ' , $ target_dir ) ) {
3535 return false ;
3636 }
3737
@@ -119,8 +119,8 @@ function is_php_file_step( $line ) {
119119 * @return bool Whether extraction completed successfully.
120120 */
121121function extract_feature_php ( $ source_dir , $ target_dir ) {
122- $ source_dir = rtrim ( $ source_dir , '/ ' );
123- $ target_dir = rtrim ( $ target_dir , '/ ' );
122+ $ source_dir = rtrim ( str_replace ( '\\' , ' / ' , $ source_dir ) , '/ ' );
123+ $ target_dir = rtrim ( str_replace ( '\\' , ' / ' , $ target_dir ) , '/ ' );
124124
125125 if ( ! is_dir ( $ source_dir ) ) {
126126 fwrite ( STDERR , sprintf ( 'Source directory "%s" does not exist. ' , $ source_dir ) . PHP_EOL );
@@ -141,7 +141,7 @@ function extract_feature_php( $source_dir, $target_dir ) {
141141
142142 foreach ( $ iterator as $ file ) {
143143 if ( $ file ->isFile () && 'feature ' === $ file ->getExtension () ) {
144- $ filepath = $ file ->getPathname ();
144+ $ filepath = str_replace ( '\\' , ' / ' , $ file ->getPathname () );
145145 $ relative = substr ( $ filepath , strlen ( $ source_dir ) + 1 );
146146 $ lines = file ( $ filepath );
147147
@@ -327,8 +327,8 @@ function strip_extraction_padding( $temp_lines, $code_start, $had_php_tag ) {
327327 * @return bool Whether all blocks were synced successfully.
328328 */
329329function update_feature_php ( $ source_dir , $ target_dir ) {
330- $ source_dir = rtrim ( $ source_dir , '/ ' );
331- $ target_dir = rtrim ( $ target_dir , '/ ' );
330+ $ source_dir = rtrim ( str_replace ( '\\' , ' / ' , $ source_dir ) , '/ ' );
331+ $ target_dir = rtrim ( str_replace ( '\\' , ' / ' , $ target_dir ) , '/ ' );
332332
333333 if ( ! is_dir ( $ target_dir ) ) {
334334 fwrite ( STDERR , sprintf ( 'Target directory "%s" does not exist. ' , $ target_dir ) . PHP_EOL );
@@ -342,7 +342,7 @@ function update_feature_php( $source_dir, $target_dir ) {
342342
343343 foreach ( $ iterator as $ file ) {
344344 if ( $ file ->isFile () && 'php ' === $ file ->getExtension () ) {
345- $ temp_filepath = $ file ->getPathname ();
345+ $ temp_filepath = str_replace ( '\\' , ' / ' , $ file ->getPathname () );
346346 $ temp_filename = $ file ->getFilename ();
347347
348348 if ( ! preg_match ( EXTRACTED_FILE_PATTERN , $ temp_filename , $ matches ) ) {
@@ -366,6 +366,8 @@ function update_feature_php( $source_dir, $target_dir ) {
366366
367367 foreach ( $ files_by_feature as $ feature_path => $ blocks ) {
368368 if ( ! file_exists ( $ feature_path ) ) {
369+ fwrite ( STDERR , sprintf ( 'Feature file "%s" does not exist. ' , $ feature_path ) . PHP_EOL );
370+ $ success = false ;
369371 continue ;
370372 }
371373
@@ -385,17 +387,33 @@ function ( $a, $b ) {
385387 }
386388
387389 foreach ( $ blocks as $ block ) {
388- $ code_start = $ block ['docstring_start ' ] + 1 ;
389- $ code_end = $ block ['docstring_end ' ] - 1 ;
390- $ temp_lines = file ( $ block ['temp_filepath ' ] );
390+ $ docstring_start = $ block ['docstring_start ' ];
391+ $ docstring_end = $ block ['docstring_end ' ];
392+ $ code_start = $ docstring_start + 1 ;
393+ $ code_end = $ docstring_end - 1 ;
394+ $ temp_lines = file ( $ block ['temp_filepath ' ] );
391395
392396 if ( false === $ temp_lines ) {
393397 fwrite ( STDERR , sprintf ( 'Could not read "%s". ' , $ block ['temp_filepath ' ] ) . PHP_EOL );
394398 $ success = false ;
395399 continue ;
396400 }
397401
398- if ( ! isset ( $ feature_lines [ $ code_start ] ) || $ code_start > $ code_end ) {
402+ if (
403+ $ code_start > $ code_end
404+ || ! isset ( $ feature_lines [ $ docstring_start ] )
405+ || ! isset ( $ feature_lines [ $ docstring_end ] )
406+ || ( 0 !== strpos ( trim ( $ feature_lines [ $ docstring_start ] ), '""" ' ) && 0 !== strpos ( trim ( $ feature_lines [ $ docstring_start ] ), "''' " ) )
407+ || ( 0 !== strpos ( trim ( $ feature_lines [ $ docstring_end ] ), '""" ' ) && 0 !== strpos ( trim ( $ feature_lines [ $ docstring_end ] ), "''' " ) )
408+ || 0 === $ docstring_start
409+ || ! isset ( $ feature_lines [ $ docstring_start - 1 ] )
410+ || ! is_php_file_step ( $ feature_lines [ $ docstring_start - 1 ] )
411+ ) {
412+ fwrite (
413+ STDERR ,
414+ sprintf ( 'Unexpected content in "%s", not syncing this block. ' , $ feature_path ) . PHP_EOL
415+ );
416+ $ success = false ;
399417 continue ;
400418 }
401419
0 commit comments