These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
0 ignored issues
–
show
|
|||
2 | /** |
||
3 | * An extension building on the Bootstrap Extension that provides certain components |
||
4 | * inside mediawiki markup as parser functions or parser tags. |
||
5 | * |
||
6 | * @see https://www.mediawiki.org/wiki/Extension:BootstrapComponents |
||
7 | * @see https://www.mediawiki.org/wiki/Extension:Bootstrap |
||
8 | * |
||
9 | * @author Tobias Oetterer |
||
10 | * |
||
11 | * @defgroup BootstrapComponents BootstrapComponents |
||
12 | */ |
||
13 | |||
14 | /** |
||
15 | * The main file of the BootstrapComponents extension, when loaded via Composer. |
||
16 | * |
||
17 | * @copyright (C) 2018, Tobias Oetterer, Paderborn University |
||
18 | * @license https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License, version 3 (or later) |
||
19 | * |
||
20 | * This file is part of the MediaWiki extension BootstrapComponents. |
||
21 | * The BootstrapComponents extension is free software: you can redistribute it |
||
22 | * and/or modify it under the terms of the GNU General Public License as published |
||
23 | * by the Free Software Foundation, either version 3 of the License, or |
||
24 | * (at your option) any later version. |
||
25 | * |
||
26 | * The BootstrapComponents extension is distributed in the hope that it will be useful, |
||
27 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
28 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
29 | * GNU General Public License for more details. |
||
30 | * |
||
31 | * You should have received a copy of the GNU General Public License |
||
32 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
||
33 | * |
||
34 | * My thanks go to Stephan Gambke for creating the Bootstrap extension and to |
||
35 | * mwjames and JeroenDeDauw who both where kind enough to help me getting better |
||
36 | * in coding for mediawiki. |
||
37 | * |
||
38 | * @file |
||
39 | * @ingroup BootstrapComponents |
||
40 | */ |
||
41 | |||
42 | namespace BootstrapComponents; |
||
43 | |||
44 | use \MWException; |
||
45 | |||
46 | /** |
||
47 | * Provides methods to register, when installed by composer |
||
48 | * |
||
49 | * @since 1.0 |
||
50 | * |
||
51 | * @codeCoverageIgnore |
||
52 | */ |
||
53 | class BootstrapComponents { |
||
54 | |||
55 | /** |
||
56 | * @throws \MWException |
||
57 | */ |
||
58 | public static function load() { |
||
59 | |||
60 | if ( self::doCheckRequirements() ) { |
||
61 | wfLoadExtension( 'BootstrapComponents' ); |
||
62 | } |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * @throws \MWException |
||
67 | * |
||
68 | * @return bool |
||
69 | */ |
||
70 | public static function doCheckRequirements() { |
||
0 ignored issues
–
show
doCheckRequirements uses the super-global variable $GLOBALS which is generally not recommended.
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: // Bad
class Router
{
public function generate($path)
{
return $_SERVER['HOST'].$path;
}
}
// Better
class Router
{
private $host;
public function __construct($host)
{
$this->host = $host;
}
public function generate($path)
{
return $this->host.$path;
}
}
class Controller
{
public function myAction(Request $request)
{
// Instead of
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
// Better (assuming you use the Symfony2 request)
$page = $request->query->get('page', 1);
}
}
![]() |
|||
71 | |||
72 | if ( !defined( 'MEDIAWIKI' ) ) { |
||
73 | echo 'This file is part of the Mediawiki extension BootstrapComponents, it is not a valid entry point.' . PHP_EOL; |
||
74 | throw new MWException( 'This file is part of a Mediawiki Extension, it is not a valid entry point.' ); |
||
75 | } |
||
76 | |||
77 | if ( version_compare( $GLOBALS[ 'wgVersion' ], '1.27', 'lt' ) ) { |
||
78 | echo '<b>Error:</b> <a href="https://github.com/oetterer/BootstrapComponents/">Bootstrap Components</a> ' |
||
79 | . 'is only compatible with MediaWiki 1.27 or above. You need to upgrade MediaWiki first.' . PHP_EOL; |
||
80 | throw new MWException( 'BootstrapComponents detected an incompatible MediaWiki version. Exiting.' ); |
||
81 | } |
||
82 | |||
83 | if ( defined( 'BOOTSTRAP_COMPONENTS_VERSION' ) ) { |
||
84 | // Do not initialize more than once. |
||
85 | return false; |
||
86 | } |
||
87 | return true; |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * Returns version number of Extension BootstrapComponents |
||
92 | * @return float |
||
93 | */ |
||
94 | public static function getVersion() { |
||
95 | return BOOTSTRAP_COMPONENTS_VERSION; |
||
96 | } |
||
97 | } |
||
98 | |||
99 | |||
100 | /** |
||
101 | * This file is loaded, when using composer. Defers initialization to wfLoadExtension as if called by LocalSettings. |
||
102 | * |
||
103 | * @see https://github.com/oetterer/BootstrapComponents/ |
||
104 | */ |
||
105 | BootstrapComponents::load(); |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.