Eventually got round to including
declare(strict_types=1); into
all my applcation/*.php files:
Edit: - see note below
File:
insert_declare.php - rough and ready but it does the job
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> Trying to insert ==> '.$ff;
echo '<br> ' .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($tmp, 6);
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($files, rglob($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
=====================//=======================
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