diff --git a/system/CodeIgniter.php b/system/CodeIgniter.php index 8886be044a..91489ad1d7 100644 --- a/system/CodeIgniter.php +++ b/system/CodeIgniter.php @@ -37,6 +37,7 @@ namespace CodeIgniter; +use Closure; use CodeIgniter\Filters\Exceptions\FilterException; use CodeIgniter\HTTP\DownloadResponse; use CodeIgniter\HTTP\RedirectResponse; @@ -51,6 +52,7 @@ use CodeIgniter\HTTP\Response; use CodeIgniter\HTTP\CLIRequest; use CodeIgniter\Router\RouteCollectionInterface; use CodeIgniter\Exceptions\PageNotFoundException; +use Exception; /** * This class is the core of the framework, and will analyse the @@ -545,7 +547,7 @@ class CodeIgniter $cachedResponse = unserialize($cachedResponse); if (! is_array($cachedResponse) || ! isset($cachedResponse['output']) || ! isset($cachedResponse['headers'])) { - throw new \Exception('Error unserializing page cache'); + throw new Exception('Error unserializing page cache'); } $headers = $cachedResponse['headers']; @@ -849,7 +851,7 @@ class CodeIgniter // Is there a 404 Override available? if ($override = $this->router->get404Override()) { - if ($override instanceof \Closure) + if ($override instanceof Closure) { echo $override($e->getMessage()); } diff --git a/system/Common.php b/system/Common.php index f0907f7b98..95424cb5c7 100644 --- a/system/Common.php +++ b/system/Common.php @@ -36,10 +36,18 @@ * @filesource */ +use CodeIgniter\Config\Config; +use CodeIgniter\Files\Exceptions\FileNotFoundException; use CodeIgniter\HTTP\RedirectResponse; use CodeIgniter\HTTP\RequestInterface; use CodeIgniter\HTTP\ResponseInterface; +use CodeIgniter\HTTP\URI; +use Config\App; +use Config\Database; +use Config\Logger; use Config\Services; +use Tests\Support\Log\TestLogger; +use Zend\Escaper\Escaper; /** * Common Functions @@ -97,7 +105,7 @@ if (! function_exists('config')) */ function config(string $name, bool $getShared = true) { - return \CodeIgniter\Config\Config::get($name, $getShared); + return Config::get($name, $getShared); } } @@ -126,7 +134,7 @@ if (! function_exists('db_connnect')) */ function db_connect($db = null, bool $getShared = true) { - return \Config\Database::connect($db, $getShared); + return Database::connect($db, $getShared); } } @@ -182,6 +190,7 @@ if (! function_exists('view_cell')) * @param string|null $cacheName * * @return string + * @throws \ReflectionException */ function view_cell(string $library, $params = null, int $ttl = 0, string $cacheName = null): string { @@ -281,7 +290,7 @@ if (! function_exists('esc')) if (! in_array($context, ['html', 'js', 'css', 'url', 'attr'])) { - throw new \InvalidArgumentException('Invalid escape context provided.'); + throw new InvalidArgumentException('Invalid escape context provided.'); } if ($context === 'attr') @@ -296,12 +305,12 @@ if (! function_exists('esc')) static $escaper; if (! $escaper) { - $escaper = new \Zend\Escaper\Escaper($encoding); + $escaper = new Escaper($encoding); } if ($encoding && $escaper->getEncoding() !== $encoding) { - $escaper = new \Zend\Escaper\Escaper($encoding); + $escaper = new Escaper($encoding); } $data = $escaper->$method($data); @@ -471,7 +480,7 @@ if (! function_exists('log_message')) // for asserting that logs were called in the test code. if (ENVIRONMENT === 'testing') { - $logger = new \Tests\Support\Log\TestLogger(new \Config\Logger()); + $logger = new TestLogger(new Logger()); return $logger->log($level, $message, $context); } @@ -534,27 +543,27 @@ if (! function_exists('remove_invisible_characters')) * between ascii characters, like Java\0script. * * @param string $str - * @param boolean $url_encoded + * @param boolean $urlEncoded * * @return string */ - function remove_invisible_characters(string $str, bool $url_encoded = true): string + function remove_invisible_characters(string $str, bool $urlEncoded = true): string { - $non_displayables = []; + $nonDisplayables = []; // every control character except newline (dec 10), // carriage return (dec 13) and horizontal tab (dec 09) - if ($url_encoded) + if ($urlEncoded) { - $non_displayables[] = '/%0[0-8bcef]/'; // url encoded 00-08, 11, 12, 14, 15 - $non_displayables[] = '/%1[0-9a-f]/'; // url encoded 16-31 + $nonDisplayables[] = '/%0[0-8bcef]/'; // url encoded 00-08, 11, 12, 14, 15 + $nonDisplayables[] = '/%1[0-9a-f]/'; // url encoded 16-31 } - $non_displayables[] = '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S'; // 00-08, 11, 12, 14-31, 127 + $nonDisplayables[] = '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S'; // 00-08, 11, 12, 14-31, 127 do { - $str = preg_replace($non_displayables, '', $str, -1, $count); + $str = preg_replace($nonDisplayables, '', $str, -1, $count); } while ($count); @@ -611,7 +620,7 @@ if (! function_exists('helper')) if (empty($path)) { - throw \CodeIgniter\Files\Exceptions\FileNotFoundException::forFileNotFound($filename); + throw FileNotFoundException::forFileNotFound($filename); } $includes[] = $path; @@ -687,7 +696,7 @@ if (! function_exists('app_timezone')) */ function app_timezone(): string { - $config = config(\Config\App::class); + $config = config(App::class); return $config->appTimezone; } @@ -706,7 +715,7 @@ if (! function_exists('csrf_token')) */ function csrf_token(): string { - $config = config(\Config\App::class); + $config = config(App::class); return $config->CSRFTokenName; } @@ -797,7 +806,7 @@ if (! function_exists('force_https')) $uri = $request->uri; $uri->setScheme('https'); - $uri = \CodeIgniter\HTTP\URI::createURIString( + $uri = URI::createURIString( $uri->getScheme(), $uri->getAuthority(true), $uri->getPath(), // Absolute URIs should use a "/" for an empty path $uri->getQuery(), $uri->getFragment() ); @@ -936,12 +945,13 @@ if (! function_exists('is_really_writable')) * the file, based on the read-only attribute. is_writable() is also unreliable * on Unix servers if safe_mode is on. * - * @link https://bugs.php.net/bug.php?id=54709 + * @link https://bugs.php.net/bug.php?id=54709 * * @param string $file * * @return boolean * + * @throws \Exception * @codeCoverageIgnore Not practical to test, as travis runs on linux */ function is_really_writable(string $file): bool @@ -996,7 +1006,7 @@ if (! function_exists('slash_item')) */ function slash_item(string $item): ?string { - $config = config(\Config\App::class); + $config = config(App::class); $configItem = $config->{$item}; if (! isset($configItem) || empty(trim($configItem))) diff --git a/system/ComposerScripts.php b/system/ComposerScripts.php index d7ebade162..e8813f78f2 100644 --- a/system/ComposerScripts.php +++ b/system/ComposerScripts.php @@ -38,6 +38,8 @@ namespace CodeIgniter; +use ReflectionClass; + /** * ComposerScripts * @@ -76,7 +78,7 @@ class ComposerScripts * * @return boolean */ - protected static function moveFile(string $source, string $destination) + protected static function moveFile(string $source, string $destination): bool { $source = realpath($source); @@ -105,7 +107,7 @@ class ComposerScripts */ protected static function getClassFilePath(string $class) { - $reflector = new \ReflectionClass($class); + $reflector = new ReflectionClass($class); return $reflector->getFileName(); } diff --git a/system/Model.php b/system/Model.php index 586e97f2b6..64c193dc16 100644 --- a/system/Model.php +++ b/system/Model.php @@ -38,6 +38,7 @@ namespace CodeIgniter; +use Closure; use CodeIgniter\Exceptions\ModelException; use Config\Database; use CodeIgniter\I18n\Time; @@ -47,6 +48,9 @@ use CodeIgniter\Database\BaseConnection; use CodeIgniter\Database\ConnectionInterface; use CodeIgniter\Validation\ValidationInterface; use CodeIgniter\Database\Exceptions\DataException; +use ReflectionClass; +use ReflectionProperty; +use stdClass; /** * Class Model @@ -354,11 +358,10 @@ class Model /** * Fetches the column of database from $this->table * - * @param string $column_name Column name + * @param string $columnName * * @return array|null The resulting row of data, or null if no data found. * - * @throws \CodeIgniter\Database\Exceptions\DataException */ public function findColumn(string $columnName) { @@ -536,8 +539,8 @@ class Model } else { - $mirror = new \ReflectionClass($data); - $props = $mirror->getProperties(\ReflectionProperty::IS_PUBLIC | \ReflectionProperty::IS_PROTECTED); + $mirror = new ReflectionClass($data); + $props = $mirror->getProperties(ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED); $properties = []; @@ -619,10 +622,15 @@ class Model $this->tempData = []; } + if (empty($data)) + { + throw DataException::forEmptyDataset('insert'); + } + // If $data is using a custom class with public or protected // properties representing the table elements, we need to grab // them as an array. - if (is_object($data) && ! $data instanceof \stdClass) + if (is_object($data) && ! $data instanceof stdClass) { $data = static::classToArray($data, $this->primaryKey, $this->dateFormat, false); } @@ -668,11 +676,6 @@ class Model $data = $this->trigger('beforeInsert', ['data' => $data]); - if (empty($data)) - { - throw DataException::forEmptyDataset('insert'); - } - // Must use the set() method to ensure objects get converted to arrays $result = $this->builder() ->set($data['data'], '', $escape) @@ -749,10 +752,15 @@ class Model $this->tempData = []; } + if (empty($data)) + { + throw DataException::forEmptyDataset('update'); + } + // If $data is using a custom class with public or protected // properties representing the table elements, we need to grab // them as an array. - if (is_object($data) && ! $data instanceof \stdClass) + if (is_object($data) && ! $data instanceof stdClass) { $data = static::classToArray($data, $this->primaryKey, $this->dateFormat); } @@ -790,11 +798,6 @@ class Model $data = $this->trigger('beforeUpdate', ['id' => $id, 'data' => $data]); - if (empty($data)) - { - throw DataException::forEmptyDataset('update'); - } - $builder = $this->builder(); if ($id) @@ -956,9 +959,9 @@ class Model * @param null $data * @param boolean $returnSQL * - * @return boolean TRUE on success, FALSE on failure + * @return mixed */ - public function replace($data = null, bool $returnSQL = false): bool + public function replace($data = null, bool $returnSQL = false) { // Validate data before saving. if (! empty($data) && $this->skipValidation === false) @@ -1019,7 +1022,7 @@ class Model * * @throws \CodeIgniter\Database\Exceptions\DataException */ - public function chunk(int $size, \Closure $userFunc) + public function chunk(int $size, Closure $userFunc) { $total = $this->builder() ->countAllResults(false); @@ -1317,7 +1320,7 @@ class Model * Validate the data against the validation rules (or the validation group) * specified in the class property, $validationRules. * - * @param array $data + * @param array|object $data * * @return boolean */