Add declare(strict_types=1), fix type hints

pull/3844/head
Julio Montoya 5 years ago
parent 7b0d7d4e0d
commit 06a8ecaf2b
  1. 6
      src/CoreBundle/Component/HTMLPurifier/Filter/AllowIframes.php
  2. 20
      src/CoreBundle/Component/Resource/Settings.php
  3. 38
      src/CoreBundle/Component/Resource/Template.php
  4. 28
      src/CoreBundle/Component/Utils/AssetDirectoryNamer.php
  5. 73
      src/CoreBundle/Component/Utils/ChamiloApi.php
  6. 9
      src/CoreBundle/Component/Utils/Glide.php

@ -1,5 +1,7 @@
<?php <?php
declare(strict_types=1);
/* For licensing terms, see /license.txt */ /* For licensing terms, see /license.txt */
namespace Chamilo\CoreBundle\Component\HTMLPurifier\Filter; namespace Chamilo\CoreBundle\Component\HTMLPurifier\Filter;
@ -73,8 +75,8 @@ class AllowIframes extends HTMLPurifier_Filter
} }
return '<iframe '.$matches[1].$extra.'></iframe>'; return '<iframe '.$matches[1].$extra.'></iframe>';
} else {
return '';
} }
return '';
} }
} }

@ -1,23 +1,21 @@
<?php <?php
declare(strict_types=1);
/* For licensing terms, see /license.txt */ /* For licensing terms, see /license.txt */
namespace Chamilo\CoreBundle\Component\Resource; namespace Chamilo\CoreBundle\Component\Resource;
class Settings class Settings
{ {
/** @var bool */ public bool $allowNodeCreation;
public $allowNodeCreation; public bool $allowResourceCreation;
/** @var bool */ public bool $allowResourceUpload;
public $allowResourceCreation; public bool $allowResourceEdit;
/** @var bool */ public bool $allowDownloadAll;
public $allowResourceUpload; public bool $allowDiskSpace;
/** @var bool */
public $allowResourceEdit;
public $allowDownloadAll;
public $allowDiskSpace;
// Shows an extra ckeditor input to save the HTML content into a ResourceFile. // Shows an extra ckeditor input to save the HTML content into a ResourceFile.
public $allowToSaveEditorToResourceFile; public bool $allowToSaveEditorToResourceFile;
public $templates; public $templates;
public function __construct() public function __construct()

@ -1,22 +1,36 @@
<?php <?php
declare(strict_types=1);
/* For licensing terms, see /license.txt */ /* For licensing terms, see /license.txt */
namespace Chamilo\CoreBundle\Component\Resource; namespace Chamilo\CoreBundle\Component\Resource;
use InvalidArgumentException;
class Template class Template
{ {
protected $index; protected string $index;
protected $list;
protected $edit; protected string $list;
protected $viewResource;
protected $new; protected string $edit;
protected $newFolder;
protected $diskSpace; protected string $viewResource;
protected $info;
protected $infoAjax; protected string $new;
protected $preview;
protected $upload; protected string $newFolder;
protected string $diskSpace;
protected string $info;
protected string $infoAjax;
protected string $preview;
protected string $upload;
public function __construct() public function __construct()
{ {
@ -46,7 +60,7 @@ class Template
return $this->$action; return $this->$action;
} }
throw new \InvalidArgumentException("No template found for action: $action"); throw new InvalidArgumentException("No template found for action: $action");
} }
public function setIndex(string $index): self public function setIndex(string $index): self

@ -1,28 +1,28 @@
<?php <?php
declare(strict_types=1);
/* For licensing terms, see /license.txt */ /* For licensing terms, see /license.txt */
namespace Chamilo\CoreBundle\Component\Utils; namespace Chamilo\CoreBundle\Component\Utils;
use Chamilo\CoreBundle\Entity\Asset; use Chamilo\CoreBundle\Entity\Asset;
use InvalidArgumentException;
use Symfony\Component\PropertyAccess\PropertyAccess; use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface; use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Vich\UploaderBundle\Mapping\PropertyMapping; use Vich\UploaderBundle\Mapping\PropertyMapping;
use Vich\UploaderBundle\Naming\ConfigurableInterface; use Vich\UploaderBundle\Naming\ConfigurableInterface;
use Vich\UploaderBundle\Naming\DirectoryNamerInterface; use Vich\UploaderBundle\Naming\DirectoryNamerInterface;
use function array_merge;
use function implode;
use function substr;
class AssetDirectoryNamer implements DirectoryNamerInterface, ConfigurableInterface class AssetDirectoryNamer implements DirectoryNamerInterface, ConfigurableInterface
{ {
/** protected PropertyAccessorInterface $propertyAccessor;
* @var PropertyAccessorInterface private string $propertyPath;
*/ private int $charsPerDir = 2;
protected $propertyAccessor; private int $dirs = 1;
/**
* @var string
*/
private $propertyPath;
private $charsPerDir = 2;
private $dirs = 1;
public function __construct(?PropertyAccessorInterface $propertyAccessor) public function __construct(?PropertyAccessorInterface $propertyAccessor)
{ {
@ -37,12 +37,12 @@ class AssetDirectoryNamer implements DirectoryNamerInterface, ConfigurableInterf
public function configure(array $options): void public function configure(array $options): void
{ {
if (empty($options['property'])) { if (empty($options['property'])) {
throw new \InvalidArgumentException('Option "property" is missing or empty.'); throw new InvalidArgumentException('Option "property" is missing or empty.');
} }
$this->propertyPath = $options['property']; $this->propertyPath = $options['property'];
$options = \array_merge(['chars_per_dir' => $this->charsPerDir, 'dirs' => $this->dirs], $options); $options = array_merge(['chars_per_dir' => $this->charsPerDir, 'dirs' => $this->dirs], $options);
$this->charsPerDir = $options['chars_per_dir']; $this->charsPerDir = $options['chars_per_dir'];
$this->dirs = $options['dirs']; $this->dirs = $options['dirs'];
@ -57,12 +57,12 @@ class AssetDirectoryNamer implements DirectoryNamerInterface, ConfigurableInterf
if (Asset::EXTRA_FIELD === $category) { if (Asset::EXTRA_FIELD === $category) {
for ($i = 0, $start = 0; $i < $this->dirs; $i++, $start += $this->charsPerDir) { for ($i = 0, $start = 0; $i < $this->dirs; $i++, $start += $this->charsPerDir) {
$parts[] = \substr($fileName, $start, $this->charsPerDir); $parts[] = substr($fileName, $start, $this->charsPerDir);
} }
} else { } else {
$parts[] = $fileName; $parts[] = $fileName;
} }
return \implode('/', $parts); return implode('/', $parts);
} }
} }

@ -1,10 +1,19 @@
<?php <?php
declare(strict_types=1);
/* For licensing terms, see /license.txt */ /* For licensing terms, see /license.txt */
namespace Chamilo\CoreBundle\Component\Utils; namespace Chamilo\CoreBundle\Component\Utils;
use ChamiloSession as Session; use ChamiloSession as Session;
use Database;
use DateInterval;
use DateTime;
use DateTimeZone;
use Display;
use Exception;
use Template;
class ChamiloApi class ChamiloApi
{ {
@ -83,7 +92,7 @@ class ChamiloApi
$accessUrlId = $accessUrl; $accessUrlId = $accessUrl;
} }
} }
$themeDir = \Template::getThemeDir($theme); $themeDir = Template::getThemeDir($theme);
$customLogoPath = $themeDir."images/header-logo-custom$accessUrlId.png"; $customLogoPath = $themeDir."images/header-logo-custom$accessUrlId.png";
$svgIcons = api_get_setting('icons_mode_svg'); $svgIcons = api_get_setting('icons_mode_svg');
@ -91,25 +100,18 @@ class ChamiloApi
$customLogoPathSVG = substr($customLogoPath, 0, -3).'svg'; $customLogoPathSVG = substr($customLogoPath, 0, -3).'svg';
if (file_exists(api_get_path(SYS_PUBLIC_PATH)."css/$customLogoPathSVG")) { if (file_exists(api_get_path(SYS_PUBLIC_PATH)."css/$customLogoPathSVG")) {
if ($getSysPath) { if ($getSysPath) {
$logoPath = api_get_path(SYS_PUBLIC_PATH)."css/$customLogoPathSVG"; return api_get_path(SYS_PUBLIC_PATH)."css/$customLogoPathSVG";
return $logoPath;
} }
$logoPath = api_get_path(WEB_CSS_PATH).$customLogoPathSVG;
return $logoPath; return api_get_path(WEB_CSS_PATH).$customLogoPathSVG;
} }
} }
if (file_exists(api_get_path(SYS_PUBLIC_PATH)."css/$customLogoPath")) { if (file_exists(api_get_path(SYS_PUBLIC_PATH)."css/$customLogoPath")) {
if ($getSysPath) { if ($getSysPath) {
$logoPath = api_get_path(SYS_PUBLIC_PATH)."css/$customLogoPath"; return api_get_path(SYS_PUBLIC_PATH)."css/$customLogoPath";
return $logoPath;
} }
$logoPath = api_get_path(WEB_CSS_PATH).$customLogoPath; return api_get_path(WEB_CSS_PATH).$customLogoPath;
return $logoPath;
} }
$originalLogoPath = $themeDir.'images/header-logo.png'; $originalLogoPath = $themeDir.'images/header-logo.png';
@ -117,26 +119,19 @@ class ChamiloApi
$originalLogoPathSVG = $themeDir.'images/header-logo.svg'; $originalLogoPathSVG = $themeDir.'images/header-logo.svg';
if (file_exists(api_get_path(SYS_CSS_PATH).$originalLogoPathSVG)) { if (file_exists(api_get_path(SYS_CSS_PATH).$originalLogoPathSVG)) {
if ($getSysPath) { if ($getSysPath) {
$logoPath = api_get_path(SYS_CSS_PATH).$originalLogoPathSVG; return api_get_path(SYS_CSS_PATH).$originalLogoPathSVG;
return $logoPath;
} }
$logoPath = api_get_path(WEB_CSS_PATH).$originalLogoPathSVG;
return $logoPath; return api_get_path(WEB_CSS_PATH).$originalLogoPathSVG;
} }
} }
if (file_exists(api_get_path(SYS_CSS_PATH).$originalLogoPath)) { if (file_exists(api_get_path(SYS_CSS_PATH).$originalLogoPath)) {
if ($getSysPath) { if ($getSysPath) {
$logoPath = api_get_path(SYS_CSS_PATH).$originalLogoPath; return api_get_path(SYS_CSS_PATH).$originalLogoPath;
return $logoPath;
} }
$logoPath = api_get_path(WEB_CSS_PATH).$originalLogoPath; return api_get_path(WEB_CSS_PATH).$originalLogoPath;
return $logoPath;
} }
$logoPath = ''; $logoPath = '';
} }
@ -167,10 +162,10 @@ class ChamiloApi
$siteName = api_get_setting('siteName'); $siteName = api_get_setting('siteName');
if (null === $logoPath) { if (null === $logoPath) {
$headerLogo = \Display::url($siteName, api_get_path(WEB_PATH).'index.php'); $headerLogo = Display::url($siteName, api_get_path(WEB_PATH).'index.php');
if (!empty($institutionUrl) && !empty($institution)) { if (!empty($institutionUrl) && !empty($institution)) {
$headerLogo .= ' - '.\Display::url($institution, $institutionUrl); $headerLogo .= ' - '.Display::url($institution, $institutionUrl);
} }
$courseInfo = api_get_course_info(); $courseInfo = api_get_course_info();
@ -178,7 +173,7 @@ class ChamiloApi
$headerLogo .= '<span class="extLinkSeparator"> - </span>'; $headerLogo .= '<span class="extLinkSeparator"> - </span>';
if (!empty($courseInfo['extLink']['url'])) { if (!empty($courseInfo['extLink']['url'])) {
$headerLogo .= \Display::url( $headerLogo .= Display::url(
$courseInfo['extLink']['name'], $courseInfo['extLink']['name'],
$courseInfo['extLink']['url'], $courseInfo['extLink']['url'],
['class' => 'extLink'] ['class' => 'extLink']
@ -188,12 +183,12 @@ class ChamiloApi
} }
} }
return \Display::tag('h2', $headerLogo, ['class' => 'text-left']); return Display::tag('h2', $headerLogo, ['class' => 'text-left']);
} }
$image = \Display::img($logoPath, $institution, $imageAttributes); $image = Display::img($logoPath, $institution, $imageAttributes);
return \Display::url($image, api_get_path(WEB_PATH).'index.php'); return Display::url($image, api_get_path(WEB_PATH).'index.php');
} }
/** /**
@ -208,7 +203,7 @@ class ChamiloApi
{ {
foreach ($tags as $tag) { foreach ($tags as $tag) {
$string2 = preg_replace('#</\b'.$tag.'\b[^>]*>#i', ' ', $string); $string2 = preg_replace('#</\b'.$tag.'\b[^>]*>#i', ' ', $string);
if ($string2 != $string) { if ($string2 !== $string) {
$string = preg_replace('/<\b'.$tag.'\b[^>]*>/i', ' ', $string2); $string = preg_replace('/<\b'.$tag.'\b[^>]*>/i', ' ', $string2);
} }
} }
@ -227,14 +222,14 @@ class ChamiloApi
*/ */
public static function addOrSubTimeToDateTime($time, $datetime = 'now', $operation = true) public static function addOrSubTimeToDateTime($time, $datetime = 'now', $operation = true)
{ {
$date = new \DateTime($datetime); $date = new DateTime($datetime);
$hours = $minutes = $seconds = 0; $hours = $minutes = $seconds = 0;
sscanf($time, '%d:%d:%d', $hours, $minutes, $seconds); sscanf($time, '%d:%d:%d', $hours, $minutes, $seconds);
$timeSeconds = isset($seconds) ? $hours * 3600 + $minutes * 60 + $seconds : $hours * 60 + $minutes; $timeSeconds = isset($seconds) ? $hours * 3600 + $minutes * 60 + $seconds : $hours * 60 + $minutes;
if ($operation) { if ($operation) {
$date->add(new \DateInterval('PT'.$timeSeconds.'S')); $date->add(new DateInterval('PT'.$timeSeconds.'S'));
} else { } else {
$date->sub(new \DateInterval('PT'.$timeSeconds.'S')); $date->sub(new DateInterval('PT'.$timeSeconds.'S'));
} }
return $date->format('Y-m-d H:i:s'); return $date->format('Y-m-d H:i:s');
@ -250,10 +245,10 @@ class ChamiloApi
public static function getCourseIdByDirectory($directory = null) public static function getCourseIdByDirectory($directory = null)
{ {
if (!empty($directory)) { if (!empty($directory)) {
$directory = \Database::escape_string($directory); $directory = Database::escape_string($directory);
$row = \Database::select( $row = Database::select(
'id', 'id',
\Database::get_main_table(TABLE_MAIN_COURSE), Database::get_main_table(TABLE_MAIN_COURSE),
['where' => ['directory = ?' => [$directory]]], ['where' => ['directory = ?' => [$directory]]],
'first' 'first'
); );
@ -386,16 +381,16 @@ class ChamiloApi
* @param string|null $utcTime Optional. The time to ve converted. * @param string|null $utcTime Optional. The time to ve converted.
* See api_get_local_time. * See api_get_local_time.
* *
* @throws \Exception * @throws Exception
* *
* @return \DateTime * @return DateTime
*/ */
public static function getServerMidnightTime($utcTime = null) public static function getServerMidnightTime($utcTime = null)
{ {
$localTime = api_get_local_time($utcTime); $localTime = api_get_local_time($utcTime);
$localTimeZone = api_get_timezone(); $localTimeZone = api_get_timezone();
$localMidnight = new \DateTime($localTime, new \DateTimeZone($localTimeZone)); $localMidnight = new DateTime($localTime, new DateTimeZone($localTimeZone));
$localMidnight->modify('midnight'); $localMidnight->modify('midnight');
return $localMidnight; return $localMidnight;

@ -1,5 +1,7 @@
<?php <?php
declare(strict_types=1);
/* For licensing terms, see /license.txt */ /* For licensing terms, see /license.txt */
namespace Chamilo\CoreBundle\Component\Utils; namespace Chamilo\CoreBundle\Component\Utils;
@ -8,13 +10,10 @@ use League\Glide\Responses\SymfonyResponseFactory;
use League\Glide\Server; use League\Glide\Server;
use League\Glide\ServerFactory; use League\Glide\ServerFactory;
/**
* Class Glide.
*/
class Glide class Glide
{ {
protected $server; protected Server $server;
protected $filters; protected array $filters;
/** /**
* Glide constructor. * Glide constructor.

Loading…
Cancel
Save