Skip to content

Commit

Permalink
Resolve PHPStan errors
Browse files Browse the repository at this point in the history
  • Loading branch information
hosni committed Apr 2, 2024
1 parent fb8c656 commit dff2044
Show file tree
Hide file tree
Showing 28 changed files with 252 additions and 863 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
/packages/base/storage/public/frontend/*
/packages/base/storage/private/frontend/*
/packages/base/storage/private/sessions/*
/packages/base/storage/private/http_client_cookies/*
!/packages/base/storage/private/cache/.gitkeep
!/packages/base/storage/private/sessions/.gitkeep
!/packages/base/storage/protected/logs/.gitkeep
!/packages/base/storage/public/frontend/.gitkeep
!/packages/base/storage/private/http_client_cookies/.gitkeep
!/packages/base/storage/private/frontend/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
namespace packages\base\Process\Exceptions;

use packages\base\Exception;
use packages\base\Process;

class CannotStartProcessException extends Exception {
public function __construct(public readonly Process $process, string $message = "can not start process") {
parent::__construct($message);
}

public function getProcess(): Process
{
return $this->process;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
namespace packages\base\Process\Exceptions;

use packages\base\InterruptedException as LegacyInterruptedException;

class InterruptedException extends LegacyInterruptedException {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
namespace packages\base\Process\Exceptions;

use packages\base\Exception;

class NotSavedProcessException extends Exception {
public function __construct(string $message = "process does not saved") {
parent::__construct($message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
namespace packages\base\Process\Exceptions;

use packages\base\NotShellAccess;

class NotShellAccessException extends NotShellAccess {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
namespace packages\base\Process\Exceptions;

use packages\base\Exception;
use packages\base\Process;

class NotStartedProcessException extends Exception {
public function __construct(public readonly Process $process, string $message = "process does not started") {
parent::__construct($message);
}

public function getProcess(): Process
{
return $this->process;
}
}
6 changes: 5 additions & 1 deletion packages/base/libraries/background/InterruptedException.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
<?php
namespace packages\base;
class InterruptedException extends \Exception {

/**
* @deprecated use packages\base\Process\Exceptions\InterruptedException instead!
*/
class InterruptedException extends Exception {
}
5 changes: 4 additions & 1 deletion packages/base/libraries/background/NotShellAccess.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<?php
namespace packages\base;

/**
* @deprecated use packages\base\Process\Exceptions\NotShellAccessException instead!
*/
class NotShellAccess extends Exception {
public function __construct(string $message = "shell_exec() function is disiabled") {
public function __construct(string $message = "shell_exec() function is disabled") {
parent::__construct($message);
}
}
30 changes: 16 additions & 14 deletions packages/base/libraries/background/Process.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php
namespace packages\base;
use packages\base;
use packages\base\db\dbObject;

class Process extends dbObject{
use packages\base\db\DBObject;
use packages\base\view\Error;

class Process extends DBObject {
const SIGHUP = 1;
const SIGINT = 2;
const SIGQUIT = 3;
Expand Down Expand Up @@ -68,15 +69,15 @@ protected function preLoad($data){
* Runs the command in a background process.
*
*/
public function background_run(){
public function background_run() {
if(!$this->checkOS()){
throw new notShellAccess();
throw new Process\Exceptions\NotShellAccessException();
}
if(!function_exists('shell_exec')){
throw new notShellAccess();
throw new Process\Exceptions\NotShellAccessException();
}
if(!$this->id){
throw new notSavedProcess();
throw new Process\Exceptions\NotSavedProcessException();
}
$root_directory = options::get('root_directory');
$php = options::get('packages.base.process.php-bin');
Expand All @@ -90,7 +91,7 @@ public function background_run(){
$this->save();
return true;
}else{
throw new cannotStartProcess();
throw new Process\Exceptions\CannotStartProcessException($this);
}
}
public function setPID(){
Expand Down Expand Up @@ -180,9 +181,10 @@ public function isRunning(){
if($this->pid){
return file_exists('/proc/'.$this->pid);
}else{
throw new notStartedProcess();
throw new Process\Exceptions\NotStartedProcessException($this);
}
}
return false;
}
/**
* Returns if the process interrupted by anthor process.
Expand All @@ -191,7 +193,7 @@ public function isRunning(){
*/
protected function isInterrupted(): bool {
if (!$this->pid) {
throw new notStartedProcess();
throw new Process\Exceptions\NotStartedProcessException($this);
}
return cache::get("packages.base.process.".$this->pid.".interrupt") == 1;
}
Expand All @@ -203,7 +205,7 @@ protected function isInterrupted(): bool {
*/
protected function checkInterruption() {
if ($this->isInterrupted()) {
throw new InterruptedException();
throw new Process\Exceptions\InterruptedException();
}
}
/**
Expand All @@ -213,7 +215,7 @@ protected function checkInterruption() {
*/
public function interrupt() {
if (!$this->pid) {
throw new notStartedProcess();
throw new Process\Exceptions\NotStartedProcessException($this);
}
cache::set("packages.base.process.".$this->pid.".interrupt", 1);
}
Expand All @@ -231,13 +233,13 @@ public function stop($signal = self::SIGTERM, $timeout = 10) {
return !$this->isRunning();
}
}else{
throw new notStartedProcess();
throw new Process\Exceptions\NotStartedProcessException($this);
}
return false;
}
protected function checkOS(){
if(self::getOS() != self::OS_NIX){
throw new OSSupport();
throw new Process\Exceptions\NotShellAccessException();
}
return true;
}
Expand Down
1 change: 1 addition & 0 deletions packages/base/libraries/background/cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ static function getParameter($name){
* @return array
*/
public static function getParameters($params){
$params = $params ?? [];
$return = array();
for($x = 0;$x!=count($params);$x++){
if($x == 0)continue;
Expand Down
8 changes: 4 additions & 4 deletions packages/base/libraries/date/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static function setTimeZone(string $timezone) {
$log->debug("check given timezone (" . $timezone . ") is valid?");
if (!in_array($timezone, DateTimeZone::listIdentifiers(DateTimeZone::ALL))) {
$log->reply()->fatal("is not valid");
throw new TimeZoneInvalidException($timezone);
throw new Date\TimeZoneNotValid($timezone);
} else {
$log->reply("is valid");
}
Expand Down Expand Up @@ -131,8 +131,8 @@ public static function setDefaultcalendar(){
if (($option = options::load('packages.base.date')) !== false) {
$log->reply($option);
$defaultOption = array_replace_recursive($defaultOption, $option);
$log->debug("set calendar to",$foption['calendar']);
self::setCanlenderName($foption['calendar']);
$log->debug("set calendar to",$defaultOption['calendar']);
self::setCanlenderName($defaultOption['calendar']);
} else{
$log->reply("Not defined");
}
Expand Down Expand Up @@ -213,7 +213,7 @@ public static function init() {
self::setDefaultcalendar();
}
if(!self::$calendar){
throw new NoCalendarException();
throw new Date\NoCalendarException();
}
self::$inited = true;
}
Expand Down
10 changes: 7 additions & 3 deletions packages/base/libraries/date/hdate.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace packages\base\date;

use packages\base\Exception;
use packages\base\utility\safe;

class hdate implements date_interface {
Expand Down Expand Up @@ -211,7 +212,7 @@ public static function format($type, $maket= "now"): string {
else $result .= $result1;
break;
case "U":
$result .= mktime();
$result .= mktime(date('H', $need), date('i', $need), date('s', $need), date('m', $need), date('d', $need), date('Y',$need));
break;
case "Z":
$result .= self::daysOfYear($hmonth, $hday, $hyear);
Expand All @@ -238,6 +239,7 @@ public static function gregorianToHijri (int $year, int $month, int $day): array
if (self::$umalqura && $julianday > self::umstartjd && $julianday < self::umendjd) {
$i = (int) (($julianday - 1948438) / 29.53056) - ((self::umstartyear - 1) * 12);
$mjd = $julianday - self::mjd_factor;
$umdata = self::getUmalquradata();
$umdata_count = count($umdata);

for ($i = max(0, $i); $i < $umdata_count; $i++) {
Expand Down Expand Up @@ -335,7 +337,7 @@ public static function daysOfYear($hmonth, $hday, $hyear): int {
list($year, $month, $day, $dayinyear) = self::gregorianToHijri($year, $month, $day);
return $dayinyear;
}
public static function monthname($month): string {
public static function monthname(string $month): string {
switch($month) {
case('01'):return('محرم');break;
case('02'):return('صفر');break;
Expand All @@ -350,14 +352,15 @@ public static function monthname($month): string {
case('11'):return('ذیقعده');break;
case('12'):return('ذیحجه');break;
}
throw new Exception('The given month is not valid!');
}
public static function mstart($month, $day, $year): string {
list( $hyear, $hmonth, $hday ) = self::gregorianToHijri($year, $month, $day);
list( $year, $month, $day ) = self::hijriToGregorian($hyear, $hmonth, "1");
$timestamp = mktime(0, 0, 0, $month, $day, $year);
return date("w", $timestamp);
}
public static function short_monthname($month): string {
public static function short_monthname(string $month): string {
switch($month) {
case('01'):return('مح');break;
case('02'):return('صف');break;
Expand All @@ -372,6 +375,7 @@ public static function short_monthname($month): string {
case('11'):return('ذق');break;
case('12'):return('ذح');break;
}
throw new Exception('The given month is not valid!');
}
public static function mktime($hour = null, $minute = null, $second = null , $month = null, $day = null, $year = null): int {
list( $year, $month, $day ) = self::hijriToGregorian($year, $month, $day);
Expand Down
21 changes: 1 addition & 20 deletions packages/base/libraries/date/jdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public static function format($type,$maket="now"){
case "w":$result1=date("w",$need);if($transnumber==1) $result.=self::Convertnumber2farsi($result1);else $result.=$result1;break;
case "y":$result1=substr($jyear,2,4);if($transnumber==1) $result.=self::Convertnumber2farsi($result1);else $result.=$result1;break;
case "Y":$result1=$jyear;if($transnumber==1) $result.=self::Convertnumber2farsi($result1);else $result.=$result1;break;
case "U" :$result.=mktime();break;
case "U" :$result.=mktime(date('H', $need), date('i', $need), date('s', $need), date('m', $need), date('d', $need), date('Y',$need));break;
case "Z" :$result.=self::days_of_year($jmonth,$jday,$jyear);break;
case "L" :list( $tmp_year, $tmp_month, $tmp_day ) = self::jalali_to_gregorian(1384, 12, 1);echo $tmp_day;break;
default:$result.=$subtype;
Expand Down Expand Up @@ -289,25 +289,6 @@ static function jcheckdate($month,$day,$year){
}
return 0;
}
static function jtime(){
return mktime() ;
}
static function jgetdate($timestamp=""){
if($timestamp=="")$timestamp=mktime();
return array(
0=>$timestamp,
"seconds"=>self::format("s",$timestamp),
"minutes"=>self::format("i",$timestamp),
"hours"=>self::format("G",$timestamp),
"mday"=>self::format("j",$timestamp),
"wday"=>self::format("w",$timestamp),
"mon"=>self::format("n",$timestamp),
"year"=>self::format("Y",$timestamp),
"yday"=>self::days_of_year(self::format("m",$timestamp),self::format("d",$timestamp),self::format("Y",$timestamp)),
"weekday"=>self::format("l",$timestamp),
"month"=>self::format("F",$timestamp),
);
}
static function div($a,$b) {return (int) ($a / $b);}
static function jalali_to_gregorian($j_y, $j_m, $j_d){
$g_days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
Expand Down
10 changes: 7 additions & 3 deletions packages/base/libraries/db/MysqliDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ class MysqliDb
*/
protected $_lockInShareMode = false;

protected $_transaction_in_progress = false;

/**
* Key field for Map()'ed result array
* @var string
Expand Down Expand Up @@ -387,6 +389,8 @@ protected function reset()
$this->_lastInsertId = null;
$this->_updateColumns = null;
$this->_mapKey = null;

return $this;
}

/**
Expand Down Expand Up @@ -970,7 +974,7 @@ public function joinWhere($whereJoin, $whereProp, $whereValue = 'DBNULL', $opera
*
* @return dbWrapper
*/
public function joinOrWhere($whereJoin, $whereProp, $whereValue = 'DBNULL', $operator = '=', $cond = 'AND')
public function joinOrWhere($whereJoin, $whereProp, $whereValue = 'DBNULL', $operator = '=')
{
return $this->joinWhere($whereJoin, $whereProp, $whereValue, $operator, 'OR');
}
Expand Down Expand Up @@ -1997,7 +2001,7 @@ public function startTransaction()
* @uses mysqli->commit();
* @uses mysqli->autocommit(true);
*/
public function commit()
public function commit(): bool
{
$result = $this->mysqli()->commit();
$this->_transaction_in_progress = false;
Expand All @@ -2011,7 +2015,7 @@ public function commit()
* @uses mysqli->rollback();
* @uses mysqli->autocommit(true);
*/
public function rollback()
public function rollback(): bool
{
$result = $this->mysqli()->rollback();
$this->_transaction_in_progress = false;
Expand Down

0 comments on commit dff2044

Please sign in to comment.