Welcome Guest, Not a member yet? Register   Sign In
Spot the BUGS :)
#1

(This post was last modified: 07-02-2018, 08:34 PM by John_Betong. Edit Reason: replaced incorrect minus with = )

Eventually got round to including declare(strict_types=1); into all my applcation/*.php files:
Edit: - see note below

File: insert_declare.phprough and ready but it does the job Smile
PHP Code:
<?php 
/*
  file: insert_declare.php
*/

declare(strict_types=1);

ini_set('html_errors''TRUE');
ini_set('display_errors''TRUE');
ini_set('display_startup_errors''TRUE');
error_reporting(-1);

$ffs rglob('application/*.php');
# fred( getcwd() );
# fred($ffs);
$cntHas 0;
$cntNot 0;
foreach(
$ffs as $i2 => $ff):
    
$tmp file_get_contents($ff);
    if( 
strpos($tmp'declare(strict_types=1);') ):
        echo 
'<br>Already ==> '.$ff;
        
$cntHas++;
    else:    
        echo 
'<br> &nbsp;&nbsp;&nbsp;Trying to insert ==> '.$ff;
        echo 
'<br>&nbsp;&nbsp;&nbsp;' .replace($ff'declare(strict_types=1);');
        
$cntNot++;
    endif;    
endforeach;    
echo 
'<br>$cntHas ==> ' .$cntHas;
echo 
'<br>$cntNot ==> ' .$cntNot;

# ======================================
function replace($fileName$insert='declare(strict_types=1);')
:
string
{
    
$result 'Problem'// DEFAULT

    
$tmp         file_get_contents($fileName);
    
$subScr substr($tmp6);  
    
echo 'XXX ==> ' .strlen($tmp);

    
$txt         "<?php \n" 
                    
.        $insert ."\n" 
                    
.    $subScr;

    
$ok FALSE;
    try {
        
$rsc     fopen($fileName"w"); #  or die("Unable to open file!");
        
if($rsc):
            
$ok  fwrite($rsc$txt);
        endif;    
    } catch (
Exception $e) {
 
   echo 'Caught exception: ==>';
 
   echo '<pre>';    
 
       print_r($e);
 
   echo '</pre>';    
    }
    if(
$ok):
        
$result 'Success ==> ' .$fileName;
        
fclose($rsc);
    endif;    

    return 
$result;
}

# ======================================
# Does not support flag GLOB_BRACE
# ======================================
function rglob($pattern$flags 0)
:array
{
    
$files glob($pattern$flags); 
    foreach (
glob(dirname($pattern).'/*'GLOB_ONLYDIR|GLOB_NOSORT) as $dir) {
     
   $files array_merge($filesrglob($dir.'/'.basename($pattern), $flags));
    }

    return 
$files;
}

# ====================================
function fred($val=NULL)
:
string
{
    echo 
'<div class="width:88%; margin:1em auto">';
    echo 
'<pre>';
        
print_r($val);
    echo 
'</pre>';

    return 
'';


I managed to:

1. recursively search the application folder
2. inserted declare(strict_types=1); into 40 files
3. fixed 7 BUGS in 5 files

Modifications available if you cannot Spot the BUGS Smile

   =====================//=======================

Edit:
To clarify my previous confusing comment - the tests were performed on a copy of the latest CI4 application folder. 

STEPS TO PERFORM TEST:
1. "CI4/application" folder was copied to my latest project
2. made necessary setup configuration modifications 
3. CI4 "application/Views/Welcome_message.php" rendered OK
4. ran the "insert_declare.php" script 
5. "Welcome_message" page failed to render modified scripts 
6. fixed 7 BUGS in 5 files
7. "Welcome_message" page now rendering OK

Please note that using the new PHP7  declare statement makes debugging far easier. All errors and warnings are shown which may as well be fixed now because PHP future version upgrades may cause your beloved site to fail - most probably late on Friday afternoon Smile
Reply




Theme © iAndrew 2016 - Forum software by © MyBB