Exemple #1
0
 public function testFloor()
 {
     $this->assertTrue(CMathi::floor(1.2) === 1);
     $this->assertTrue(CMathi::floor(2.8) === 2);
     $this->assertTrue(CMathi::floor(3.0) === 3);
     $this->assertTrue(CMathi::floor(-4.2) === -5);
     $this->assertTrue(CMathi::floor(-5.0) === -5);
     $this->assertTrue(CMathi::floor(3) === 3);
     $this->assertTrue(CMathi::floor(-4) === -4);
 }
Exemple #2
0
 /**
  * Calculates the difference between a point in time and another such point, measured in a specified time unit as
  * a positive or negative value.
  *
  * The difference is calculated by subtracting the provided point in time from *this* point in time and as an
  * integer number, rounding *down* from the fractional difference if needed for a positive value or rounding *up*
  * for a negative one.
  *
  * Differences between points in time are not tied to any time zone. Any days that were added by leap years between
  * the points are counted in.
  *
  * @param  CTime $fromTime The second point in time for comparison.
  * @param  enum $timeUnit The time unit in which the difference is to be calculated. Can be `SECOND`, `MINUTE`,
  * `HOUR`, `DAY`, `WEEK`, `MONTH`, or `YEAR`.
  *
  * @return int The difference between the two points in time, measured in the time unit specified and as a positive
  * value if *this* point in time goes after the second one and as a negative value if *this* point in time goes
  * before the second one (except if the result is zero).
  */
 public function signedDiff(CTime $fromTime, $timeUnit)
 {
     assert('is_enum($timeUnit)', vs(isset($this), get_defined_vars()));
     $dtz = new DateTimeZone(CTimeZone::UTC);
     $thisDt = new DateTime();
     $thisDt->setTimestamp($this->UTime());
     $thisDt->setTimezone($dtz);
     $fromDt = new DateTime();
     $fromDt->setTimestamp($fromTime->UTime());
     $fromDt->setTimezone($dtz);
     $dti = $fromDt->diff($thisDt, false);
     assert('is_number($dti->days)', vs(isset($this), get_defined_vars()));
     $intDiffInSeconds = CMathi::abs($dti->days) * self::SECONDS_PER_DAY + CMathi::abs($dti->h) * self::SECONDS_PER_HOUR + CMathi::abs($dti->i) * self::SECONDS_PER_MINUTE + CMathi::abs($dti->s);
     $floatDiffInSeconds = (double) $intDiffInSeconds;
     $diff;
     switch ($timeUnit) {
         case self::SECOND:
             $diff = $intDiffInSeconds;
             break;
         case self::MINUTE:
             $diff = CMathi::floor($floatDiffInSeconds / self::SECONDS_PER_MINUTE);
             break;
         case self::HOUR:
             $diff = CMathi::floor($floatDiffInSeconds / self::SECONDS_PER_HOUR);
             break;
         case self::DAY:
             $diff = CMathi::floor($floatDiffInSeconds / self::SECONDS_PER_DAY);
             break;
         case self::WEEK:
             $diff = CMathi::floor($floatDiffInSeconds / self::SECONDS_PER_WEEK);
             break;
         case self::MONTH:
             $diff = CMathi::floor($floatDiffInSeconds / self::SECONDS_PER_MONTH);
             break;
         case self::YEAR:
             $diff = CMathi::floor($floatDiffInSeconds / self::SECONDS_PER_YEAR);
             break;
         default:
             assert('false', vs(isset($this), get_defined_vars()));
             break;
     }
     if ($dti->invert == 1) {
         $diff = -$diff;
     }
     return $diff;
 }
Exemple #3
0
 /**
  * @ignore
  */
 public static function onThirdPartyUpdateByPackageManager()
 {
     if (!self::isInCliMode()) {
         // This method can be run in CLI mode only.
         assert('false', vs(isset($this), get_defined_vars()));
     }
     $timeoutPause = new CTimeoutPause();
     CShell::speak("Processing third-party components ...");
     $tpDps = CFile::listDirectories(CFilePath::absolute($GLOBALS["PHRED_PATH_TO_THIRD_PARTY"]));
     $ignorePackages = CConfiguration::option("upd.thirdPartyOopWrappingIgnorePackages");
     $ignorePackagesL2 = CArray::filter($ignorePackages, function ($package) {
         return CString::find($package, "/");
     });
     $newTpDps = CArray::make();
     $len = CArray::length($tpDps);
     for ($i = 0; $i < $len; $i++) {
         $tpDp = $tpDps[$i];
         $dirName = CFilePath::name($tpDp);
         if (!CArray::find($ignorePackages, $dirName)) {
             $dpHasL2DirsToIgnore = CArray::find($ignorePackagesL2, $dirName, function ($packageL2, $dirName) {
                 return CString::equals(CFilePath::directory($packageL2), $dirName);
             });
             if (!$dpHasL2DirsToIgnore) {
                 CArray::push($newTpDps, $tpDp);
             } else {
                 $tpSubDps = CFile::listDirectories($tpDp);
                 $tpSubDps = CArray::filter($tpSubDps, function ($subDp) use($ignorePackagesL2) {
                     return !CArray::find($ignorePackagesL2, $subDp, function ($packageL2, $subDp) {
                         return CString::endsWith($subDp, $packageL2);
                     });
                 });
                 CArray::pushArray($newTpDps, $tpSubDps);
             }
         }
     }
     $tpDps = $newTpDps;
     $wrapProtectedMethods = CConfiguration::option("upd.thirdPartyOopWrappingInProtectedMethods");
     $wrapPrivateMethods = CConfiguration::option("upd.thirdPartyOopWrappingInPrivateMethods");
     static $s_stdPhpTag = "<?php";
     static $s_progressResolution = 0.05;
     $prevProgressDivR = 0;
     $tpDpsLen = CArray::length($tpDps);
     for ($i0 = 0; $i0 < $tpDpsLen; $i0++) {
         $tpFps = CFile::reFindFilesRecursive($tpDps[$i0], "/\\.php\\d?\\z/");
         $tpFpsLen = CArray::length($tpFps);
         for ($i1 = 0; $i1 < $tpFpsLen; $i1++) {
             $fileCode = CFile::read($tpFps[$i1]);
             if (!CString::find($fileCode, self::$ms_thirdPartyAlreadyOopWrappedMark)) {
                 $parser = new PhpParser\Parser(new PhpParser\Lexer());
                 try {
                     // Parse the code.
                     $statements = $parser->parse($fileCode);
                     // Wrap the code into OOP.
                     $traverser = new PhpParser\NodeTraverser();
                     $mainVisitor = new CMainVisitor($wrapProtectedMethods, $wrapPrivateMethods);
                     $traverser->addVisitor($mainVisitor);
                     $statements = $traverser->traverse($statements);
                     $wrappedCode = (new PhpParser\PrettyPrinter\Standard())->prettyPrint($statements);
                     $phpTagPos = CString::indexOf($wrappedCode, $s_stdPhpTag);
                     if ($phpTagPos == -1) {
                         $wrappedCode = "{$s_stdPhpTag}\n\n{$wrappedCode}";
                         $phpTagPos = 0;
                     }
                     $wrappedCode = CString::insert($wrappedCode, $phpTagPos + CString::length($s_stdPhpTag), "\n\n" . self::$ms_thirdPartyAlreadyOopWrappedMark);
                     // Save.
                     CFile::write($tpFps[$i1], $wrappedCode);
                 } catch (PhpParser\Error $parserError) {
                     CShell::say("\nPhpParser: " . $tpFps[$i1] . ", at line " . $parserError->getRawLine() . ": " . $parserError->getRawMessage());
                 }
             }
             $progress = (double) ($i0 / $tpDpsLen + 1 / $tpDpsLen * $i1 / $tpFpsLen);
             $progressDivR = CMathi::floor($progress / $s_progressResolution);
             if ($progressDivR != $prevProgressDivR) {
                 $perc = CMathi::round($progressDivR * $s_progressResolution * 100);
                 CShell::speak("{$perc}%");
             }
             $prevProgressDivR = $progressDivR;
         }
     }
     CShell::speak("100%");
     CShell::say("Done.");
     $timeoutPause->end();
 }