Browse Source

chore: coding standards + phpstan

pull/27/head
JamesRezo 3 weeks ago
parent
commit
abcc9dee33
  1. 4
      loader/index.php
  2. 4
      loader/src/Api/V2.php
  3. 2
      loader/src/Api/Versions.php
  4. 16
      loader/src/Application.php
  5. 1
      loader/src/Check/CheckInterface.php
  6. 1
      loader/src/Check/Extensions.php
  7. 4
      loader/src/Check/Filesystem.php
  8. 5
      loader/src/Cleaner.php
  9. 1
      loader/src/Config/AbstractConfig.php
  10. 1
      loader/src/Config/Config.php
  11. 3
      loader/src/Config/Custom.php
  12. 2
      loader/src/Config/Internal.php
  13. 8
      loader/src/Deprecations.php
  14. 2
      loader/src/Filesystem.php
  15. 2
      loader/src/I18n/Languages.php
  16. 15
      loader/src/I18n/Translator.php
  17. 8
      loader/src/Remote.php
  18. 10
      loader/src/Route/ArchiveDecompress.php
  19. 3
      loader/src/Route/ArchiveDownload.php
  20. 29
      loader/src/Route/ArchiveSelection.php
  21. 3
      loader/src/Route/RouteInterface.php
  22. 4
      loader/src/Route/SelfUpdate.php
  23. 7
      loader/src/Spip.php
  24. 2
      loader/src/Template/Error.php
  25. 3
      loader/src/Template/Page.php
  26. 23
      loader/src/Template/Template.php
  27. 9
      loader/src/Template/Utils.php
  28. 4
      phpcs.xml.dist
  29. 7
      phpstan-baseline.neon
  30. 2
      phpstan.neon.dist

4
loader/index.php

@ -5,7 +5,7 @@ use Spip\Loader\Config\Config;
use Spip\Loader\Config\Internal;
use Spip\Loader\Config\Custom;
use Spip\Loader\Deprecations;
use Spip\Loader\Template\Error;
use Spip\Loader\Template\Error as TemplateError;
error_reporting(E_ALL);
ini_set('display_errors', 1);
@ -32,7 +32,7 @@ try {
$app = new Application($config);
$app->run();
} catch (\Exception $e) {
$template = Error::fromException($e);
$template = TemplateError::fromException($e);
$template->sendHeaders();
echo $template->toHTML();
}

4
loader/src/Api/V2.php

@ -6,7 +6,6 @@ use Spip\Loader\Filesystem;
use Spip\Loader\Remote;
class V2 {
const JSON_FILE = 'spip_loader_list.json';
const API_VERSION = 2;
@ -61,7 +60,7 @@ class V2 {
}
if ($liste['api'] !== self::API_VERSION) {
trigger_error(sprintf("Api version %s returns from %s; Expected %s.", $liste['api'], self::JSON_FILE, self::API_VERSION), \E_USER_WARNING);
trigger_error(sprintf('Api version %s returns from %s; Expected %s.', $liste['api'], self::JSON_FILE, self::API_VERSION), \E_USER_WARNING);
return [
'default_branch' => null,
'versions' => [],
@ -70,5 +69,4 @@ class V2 {
return $liste;
}
}

2
loader/src/Api/Versions.php

@ -3,7 +3,6 @@
namespace Spip\Loader\Api;
class Versions {
/** @var V2 */
private $api;
@ -117,5 +116,4 @@ class Versions {
$branch = implode('.', $branch);
return $branch;
}
}

16
loader/src/Application.php

@ -10,7 +10,6 @@ use Spip\Loader\I18n\Languages;
use Spip\Loader\I18n\Translator;
class Application {
/** @var string */
private $lang;
@ -73,13 +72,14 @@ class Application {
/** @return bool */
protected function checks() {
foreach([
foreach (
[
Check\Authentication::class,
Check\Filesystem::class,
Check\Extensions::class,
] as $checkClass)
{
$check = new $checkClass;
] as $checkClass
) {
$check = new $checkClass();
if (!$check($this->config, $this->spip, $this->page)) {
return false;
}
@ -112,14 +112,16 @@ class Application {
$this->spip
);
foreach([
foreach (
[
// etape finale: deploiement de l'archive
Route\ArchiveDecompress::class,
// etape intermediaire: charger l'archive
Route\ArchiveDownload::class,
// etape intiale, afficher la page de presentation
Route\ArchiveSelection::class,
] as $routeClass) {
] as $routeClass
) {
if ($routeClass::match($_REQUEST)) {
$route = new $routeClass();
$route->handle($this->config, $this->versions, $this->spip, $this->page, [

1
loader/src/Check/CheckInterface.php

@ -1,6 +1,7 @@
<?php
namespace Spip\Loader\Check;
use Spip\Loader\Config\Config;
use Spip\Loader\Spip;
use Spip\Loader\Template\Page;

1
loader/src/Check/Extensions.php

@ -11,7 +11,6 @@ class Extensions implements CheckInterface {
if (!self::verifie_zlib_ok()) {
// on ne peut pas decompresser
throw new \Exception('Fonctions zip non disponibles');
return false;
}
return true;

4
loader/src/Check/Filesystem.php

@ -58,7 +58,7 @@ class Filesystem implements CheckInterface {
@mkdir($working_install, $config->get('chmod'), true);
}
if (!file_exists($working_install)) {
throw new \RuntimeException(sprintf("Impossible de créer le répertoire: %s", $install_tmp));
throw new \RuntimeException(sprintf('Impossible de créer le répertoire: %s', $install_tmp));
}
}
return true;
@ -76,7 +76,7 @@ class Filesystem implements CheckInterface {
@mkdir($working_install, $config->get('chmod'), true);
}
if (!file_exists($working_install)) {
throw new \RuntimeException(sprintf("Impossible de créer le répertoire: %s", $install_dir));
throw new \RuntimeException(sprintf('Impossible de créer le répertoire: %s', $install_dir));
}
}
return true;

5
loader/src/Cleaner.php

@ -1,10 +1,10 @@
<?php
namespace Spip\Loader;
use Spip\Loader\Iterator\SuperflusFilterIterator;
class Cleaner {
private $chmod;
private $dir_install;
private $zip_path_remove;
@ -145,9 +145,8 @@ class Cleaner {
return $liste;
}
function nettoyer_racine($fichier) {
public function nettoyer_racine($fichier) {
@unlink($fichier);
return true;
}
}

1
loader/src/Config/AbstractConfig.php

@ -3,7 +3,6 @@
namespace Spip\Loader\Config;
abstract class AbstractConfig implements ConfigInterface {
/** à remplir pour les sous classes */
protected $config = [];

1
loader/src/Config/Config.php

@ -3,7 +3,6 @@
namespace Spip\Loader\Config;
final class Config extends AbstractConfig {
public function __construct(Internal $internal, Custom $custom) {
$this->config = array_merge($internal->toArray(), $custom->toArray());
}

3
loader/src/Config/Custom.php

@ -3,7 +3,6 @@
namespace Spip\Loader\Config;
final class Custom extends AbstractConfig {
protected $config = [
# liste d’id_auteur (table spip_auteurs).
'authorized.users' => [1],
@ -37,7 +36,7 @@ final class Custom extends AbstractConfig {
'chmod' => 0755,
# le Cleaner ne place pas dans le répertoire obsolète
# un répertoire qui contiendrait un fichier avec ce nom.
# un répertoire qui contiendrait un fichier avec ce nom.
'cleaner.keep.directory' => '.spip_loader_keep',
# Si définit, utilisera le chemin zip indiqué,

2
loader/src/Config/Internal.php

@ -3,7 +3,6 @@
namespace Spip\Loader\Config;
final class Internal extends AbstractConfig {
protected $config = [
# File that runs the application into the phar (or outside)
# always index.php
@ -75,6 +74,5 @@ final class Internal extends AbstractConfig {
$this->set('url.frontend', dirname($script_url) . '/');
# $this->set('url.root', dirname($script_url, \Phar::running(false) ? 2 : 1) . '/'); # php 7.0+
$this->set('url.root', (\Phar::running(false) ? dirname(dirname($script_url)) : dirname($script_url)) . '/');
}
}

8
loader/src/Deprecations.php

@ -51,12 +51,12 @@ class Deprecations {
}
if (defined('_CHEMIN_FICHIER_ZIP')) {
$this->config->set('archive.zip.path', _CHEMIN_FICHIER_ZIP);
$this->config->set('archive.zip.path', constant('_CHEMIN_FICHIER_ZIP'));
$this->deprecate_config_string('_CHEMIN_FICHIER_ZIP', 'archive.zip.path');
}
if (defined('_NOM_PAQUET_ZIP')) {
$this->config->set('archive.zip.name', _CHEMIN_FICHIER_ZIP);
$this->deprecate_config_string('_CHEMIN_FICHIER_ZIP', 'archive.zip.name');
$this->config->set('archive.zip.name', constant('_NOM_PAQUET_ZIP'));
$this->deprecate_config_string('_NOM_PAQUET_ZIP', 'archive.zip.name');
}
# adresse du depot
@ -161,7 +161,7 @@ class Deprecations {
foreach ($configs as $key => $value) {
$config .= "\t'$key' => $value,\n";
}
$config .= "];";
$config .= '];';
return $config;
}
}

2
loader/src/Filesystem.php

@ -3,7 +3,6 @@
namespace Spip\Loader;
class Filesystem {
/**
* Invalidates a PHP file from any active opcode caches.
*
@ -65,5 +64,4 @@ class Filesystem {
closedir($dh);
}
}
}

2
loader/src/I18n/Languages.php

@ -76,7 +76,7 @@ class Languages {
* @return string
*/
public static function getLangDir($lang) {
return in_array($lang, self::LANG_RTL) ? 'rtl': 'ltr';
return in_array($lang, self::LANG_RTL) ? 'rtl' : 'ltr';
}
/**

15
loader/src/I18n/Translator.php

@ -14,7 +14,7 @@ class Translator {
/** @var string */
private $lang_directory;
/** @param string */
/** @param string $root_directory */
public function __construct($root_directory, $lang = '', $fallback_lang = '') {
$this->lang_directory = $root_directory . '/lang';
if ($lang) {
@ -52,7 +52,6 @@ class Translator {
* Then returns '' if not found
*
* @param string $key Translation key
* @param array $context
* @param string $lang Destination language (default to object language)
* @return string
*/
@ -74,7 +73,7 @@ class Translator {
* Paramétrise un texte de traduction
*
* @param string $message
* @param array $args
* @param array $context
* @return string
*/
public function contextualize($message, array $context = []) {
@ -83,11 +82,11 @@ class Translator {
}
$keys = [];
foreach (array_keys($context) as $key) {
$keys[] = '@' . $key . '@';
}
foreach (array_keys($context) as $key) {
$keys[] = '@' . $key . '@';
}
return str_replace($keys, $context, $message);
return str_replace($keys, $context, $message);
}
/**
@ -125,6 +124,4 @@ class Translator {
unset($GLOBALS[$GLOBALS['idx_lang']], $GLOBALS['idx_lang']);
return true;
}
}

8
loader/src/Remote.php

@ -3,7 +3,6 @@
namespace Spip\Loader;
class Remote {
/**
* Recupere une page sur le net
* et au besoin l'encode dans le charset local
@ -95,7 +94,7 @@ class Remote {
public static function init_http($get, $url, $refuse_gz = false) {
$fopen = false;
// FIXME: need config access
$http_proxy = !preg_match(',^http://,i', _URL_LOADER_PROXY) ? '' : _URL_LOADER_PROXY;
$http_proxy = !preg_match(',^http://,i', constant('_URL_LOADER_PROXY')) ? '' : constant('_URL_LOADER_PROXY');
$t = @parse_url($url);
$host = $t['host'];
@ -114,6 +113,7 @@ class Remote {
$path = '/';
}
$proxy_pass = '';
if ($http_proxy) {
$t2 = @parse_url($http_proxy);
$proxy_host = $t2['host'];
@ -162,8 +162,8 @@ class Remote {
/**
* Trouver le numéro de version du dernier spip_loader
* @param string $url_spip_loader_version
* @return string
* @param string $url_spip_loader_version
* @return string
*/
public static function recuperer_version_spip_loader($url_spip_loader_version) {
static $version = null;

10
loader/src/Route/ArchiveDecompress.php

@ -13,11 +13,9 @@ use Spip\Loader\Template\Page;
use Spip\Loader\Template\Utils;
class ArchiveDecompress implements RouteInterface {
public static function match(array $REQUEST) {
# and file_exists($fichier)
return isset($REQUEST['etape']) && $REQUEST['etape'] === 'fichier';
}
/**
@ -47,7 +45,7 @@ class ArchiveDecompress implements RouteInterface {
'<div class="error">'
. $page->getTranslator()->translate('tradloader:echec_chargement')
. "<br><em>$paquet, $fichier, $range</em>"
. "</div>"
. '</div>'
);
echo $page->toHTML();
return false;
@ -85,7 +83,7 @@ class ArchiveDecompress implements RouteInterface {
$page->setInner(
'<div class="error">'
. $page->getTranslator()->translate('tradloader:donnees_incorrectes', ['erreur' => $zip->errorInfo()])
. "</div>"
. '</div>'
);
echo $page->toHTML();
return false;
@ -100,7 +98,8 @@ class ArchiveDecompress implements RouteInterface {
return true;
} elseif (!$ended) {
// on a fait le dernier tour, afficher 100% et relancer pour les déplacements et nettoyages
$page->redirige_boucle($url . '&ended=1', 1);;
$page->redirige_boucle($url . '&ended=1', 1);
;
return true;
}
@ -146,5 +145,4 @@ class ArchiveDecompress implements RouteInterface {
}
return 'cretion des repertoires tentee';
}
}

3
loader/src/Route/ArchiveDownload.php

@ -11,7 +11,6 @@ use Spip\Loader\Template\Page;
use Spip\Loader\Template\Utils;
class ArchiveDownload implements RouteInterface {
public static function match(array $REQUEST) {
return isset($REQUEST['etape']) && $REQUEST['etape'] === 'charger';
}
@ -43,7 +42,7 @@ class ArchiveDownload implements RouteInterface {
'<div class="error">'
. $page->getTranslator()->translate('tradloader:echec_chargement')
. "<br><em>$paquet, $fichier, $range</em>"
. "</div>"
. '</div>'
);
echo $page->toHTML();
return false;

29
loader/src/Route/ArchiveSelection.php

@ -10,8 +10,7 @@ use Spip\Loader\Spip;
use Spip\Loader\Template\Page;
use Spip\Loader\Template\Utils;
Class ArchiveSelection implements RouteInterface {
class ArchiveSelection implements RouteInterface {
public static function match(array $REQUEST) {
return true;
}
@ -33,7 +32,6 @@ Class ArchiveSelection implements RouteInterface {
$dest = $contexte['dest'];
$paquet = $contexte['paquet'];
$etape = $contexte['etape'];
$range = $contexte['range'];
$version_spip_loader = $contexte['version'];
@ -136,7 +134,6 @@ Class ArchiveSelection implements RouteInterface {
// Construction du corps
if ($versions_spip) {
$corps =
$translator->translate('tradloader:texte_intro', [
'paquet' => strtoupper($config->get('archive.zip.name')),
@ -162,10 +159,12 @@ Class ArchiveSelection implements RouteInterface {
}
$remote_version = Remote::recuperer_version_spip_loader($config->get('url.spip_loader.version'));
if ($this->necessite_maj_spip_loader(
$version_spip_loader,
$remote_version
)) {
if (
$this->necessite_maj_spip_loader(
$version_spip_loader,
$remote_version
)
) {
$corps .=
"<div class='info'><a href='" . $config->get('url.spip_loader.php') . "'>"
. $translator->translate('tradloader:spip_loader_maj', ['version' => $remote_version])
@ -198,7 +197,11 @@ Class ArchiveSelection implements RouteInterface {
/**
* // TODO: RequestInterface parameter
* @param string $version_installee */
* @param Config $config
* @param Versions $versions
* @param Spip $spip
* @return string
*/
public static function selectArchive(
Config $config,
Versions $versions,
@ -220,14 +223,14 @@ Class ArchiveSelection implements RouteInterface {
}
if (
$paquet and ((strpos($paquet, '../') !== false)
or ($paquet and substr($paquet, -4, 4) !== '.zip'))
$paquet and (
(strpos($paquet, '../') !== false) or
(substr($paquet, -4, 4) !== '.zip')
)
) {
throw new \Exception("chemin incorrect $paquet");
}
return $paquet;
}
}

3
loader/src/Route/RouteInterface.php

@ -8,13 +8,12 @@ use Spip\Loader\Spip;
use Spip\Loader\Template\Page;
interface RouteInterface {
/**
* TODO: RequestInterface parameter
* @param array $Request ($_REQUEST)
* @return bool
* */
public static function match(array $REQUEST);
public static function match(array $Request);
/**
* TODO: RequestInterface parameter

4
loader/src/Route/SelfUpdate.php

@ -10,8 +10,7 @@ use Spip\Loader\Spip;
use Spip\Loader\Template\Page;
use Spip\Loader\Filesystem;
Class SelfUpdate implements RouteInterface {
class SelfUpdate implements RouteInterface {
public static function match(array $REQUEST) {
return !empty($REQUEST['spip_loader_update']);
}
@ -32,5 +31,4 @@ Class SelfUpdate implements RouteInterface {
$page->redirige_boucle('../' . basename($url_spip_loader));
return true;
}
}

7
loader/src/Spip.php

@ -3,7 +3,6 @@
namespace Spip\Loader;
class Spip {
const BOOTSTRAP_SPIP_FILE = 'ecrire/inc_version.php';
private $loaded = false;
@ -41,11 +40,13 @@ class Spip {
public function isUsable() {
$this->start();
if (
(defined('_FILE_CONNECT') and _FILE_CONNECT and strpos(_FILE_CONNECT, '.php'))
or defined('_SITES_ADMIN_MUTUALISATION')
defined('_FILE_CONNECT') and constant('_FILE_CONNECT') and strpos(constant('_FILE_CONNECT'), '.php')
) {
return true;
}
if (defined('_SITES_ADMIN_MUTUALISATION')) {
return true;
}
return false;
}

2
loader/src/Template/Error.php

@ -3,8 +3,6 @@
namespace Spip\Loader\Template;
class Error extends Template {
const PAGE_TYPE = 'error';
private $message;

3
loader/src/Template/Page.php

@ -6,7 +6,6 @@ use Spip\Loader\Config\Config;
use Spip\Loader\I18n\Translator;
class Page extends Template {
const PAGE_TYPE = 'admin';
private $translator;
@ -32,6 +31,4 @@ class Page extends Template {
public function getTranslator() {
return $this->translator;
}
}

23
loader/src/Template/Template.php

@ -5,7 +5,6 @@ namespace Spip\Loader\Template;
use Spip\Loader\I18n\Languages;
class Template {
const APP_NAME = 'Spip Loader';
const PAGE_TYPE = 'public';
@ -38,7 +37,6 @@ class Template {
/**
* @param string $title
* @param string $message
*/
public function __construct($title) {
$this->title = $title;
@ -242,7 +240,7 @@ HTML;
* @param string $progres
* @return void
*/
function redirige_boucle($url, $progres = '') {
public function redirige_boucle($url, $progres = '') {
//@apache_setenv('no-gzip', 1); // provoque page blanche chez certains hebergeurs donc ne pas utiliser
@ini_set('zlib.output_compression', '0'); // pour permettre l'affichage au fur et a mesure
@ini_set('output_buffering', 'off');
@ -250,7 +248,7 @@ HTML;
@ob_implicit_flush(1);
$corps = '<meta http-equiv="refresh" content="0;' . $url . '">';
if ($progres) {
$progres = round($progres * 100);
$progres = round((int)$progres * 100);
$corps .= <<<HTML
<div class='progression'>
<label for='progression_bar'>{$progres}%</label>
@ -268,13 +266,12 @@ HTML;
* @param string $templateContent
* @return string
*/
protected function render($templateContent, array $context = [])
{
$keys = [];
foreach (array_keys($context) as $key) {
$keys[] = '{' . $key . '}';
}
return str_replace($keys, $context, $templateContent);
}
protected function render($templateContent, array $context = []) {
$keys = [];
foreach (array_keys($context) as $key) {
$keys[] = '{' . $key . '}';
}
return str_replace($keys, $context, $templateContent);
}
}

9
loader/src/Template/Utils.php

@ -2,19 +2,20 @@
namespace Spip\Loader\Template;
use function headers_sent, header;
use function headers_sent,
header;
use Spip\Loader\I18n\Languages;
use Spip\Loader\Config\Config;
use Spip\Loader\Api\Versions;
class Utils {
public static function http_no_cache() {
if (headers_sent()) {
return;
}
header("Content-Type: text/html; charset=utf-8");
header('Content-Type: text/html; charset=utf-8');
header('Expires: 0');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-cache, must-revalidate');
@ -92,6 +93,4 @@ HTML;
}
return $select;
}
}

4
phpcs.xml.dist

@ -3,7 +3,9 @@
<file>./loader/index.php</file>
<file>./loader/src</file>
<rule ref="SCS1"/>
<rule ref="SCS1" >
<exclude name="PSR12.Properties.ConstantVisibility" />
</rule>
<rule ref="PHPCompatibility"/>
<config name="testVersion" value="5.6-8.2"/>

7
phpstan-baseline.neon

@ -1,7 +1,2 @@
parameters:
ignoreErrors:
-
message: "#^Undefined variable\\: \\$proxy_pass$#"
count: 1
path: index.php
ignoreErrors: []

2
phpstan.neon.dist

@ -11,4 +11,4 @@ parameters:
excludePaths:
analyse:
- loader/pclzip.php
level: 3
level: 4

Loading…
Cancel
Save