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 | use SCI\HookRegistry; |
||
4 | use SCI\Options; |
||
5 | use SMW\ApplicationFactory; |
||
6 | |||
7 | /** |
||
8 | * @see https://github.com/SemanticMediaWiki/SemanticCite/ |
||
9 | * |
||
10 | * @defgroup SCI Semantic Citation |
||
11 | */ |
||
12 | SemanticCite::load(); |
||
13 | |||
14 | /** |
||
15 | * @codeCoverageIgnore |
||
16 | */ |
||
17 | class SemanticCite { |
||
0 ignored issues
–
show
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.
You can fix this by adding a namespace to your class: namespace YourVendor;
class YourClass { }
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries. ![]() |
|||
18 | |||
19 | /** |
||
20 | * @since 1.3 |
||
21 | * |
||
22 | * @note It is expected that this function is loaded before LocalSettings.php |
||
23 | * to ensure that settings and global functions are available by the time |
||
24 | * the extension is activated. |
||
25 | */ |
||
26 | public static function load() { |
||
27 | |||
28 | if ( is_readable( __DIR__ . '/vendor/autoload.php' ) ) { |
||
29 | include_once __DIR__ . '/vendor/autoload.php'; |
||
30 | } |
||
31 | |||
32 | // Load DefaultSettings |
||
33 | require_once __DIR__ . '/DefaultSettings.php'; |
||
34 | } |
||
35 | |||
36 | /** |
||
37 | * @since 1.1 |
||
38 | */ |
||
39 | public static function initExtension( $credits = [] ) { |
||
0 ignored issues
–
show
initExtension 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);
}
}
![]() |
|||
40 | |||
41 | // See https://phabricator.wikimedia.org/T151136 |
||
42 | define( 'SCI_VERSION', isset( $credits['version'] ) ? $credits['version'] : 'UNKNOWN' ); |
||
43 | |||
44 | // Extend the upgrade key provided by SMW to ensure that an DB |
||
45 | // schema is updated accordingly before using the extension |
||
46 | if ( isset( $GLOBALS['smwgUpgradeKey'] ) ) { |
||
0 ignored issues
–
show
This
if statement is empty and can be removed.
This check looks for the bodies of These if (rand(1, 6) > 3) {
//print "Check failed";
} else {
print "Check succeeded";
}
could be turned into if (rand(1, 6) <= 3) {
print "Check succeeded";
}
This is much more concise to read. ![]() |
|||
47 | // $GLOBALS['smwgUpgradeKey'] .= ':scite:2018-09'; |
||
0 ignored issues
–
show
Unused Code
Comprehensibility
introduced
by
55% of this comment could be valid code. Did you maybe forget this after debugging?
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it. The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production. This check looks for comments that seem to be mostly valid code and reports them. ![]() |
|||
48 | } |
||
49 | |||
50 | // Register message files |
||
51 | $GLOBALS['wgMessagesDirs']['SemanticCite'] = __DIR__ . '/i18n'; |
||
52 | $GLOBALS['wgExtensionMessagesFiles']['SemanticCiteMagic'] = __DIR__ . '/i18n/SemanticCite.magic.php'; |
||
53 | $GLOBALS['wgExtensionMessagesFiles']['SemanticCiteAlias'] = __DIR__ . '/i18n/SemanticCite.alias.php'; |
||
54 | |||
55 | $GLOBALS['wgSpecialPages']['FindCitableMetadata'] = '\SCI\Specials\SpecialFindCitableMetadata'; |
||
56 | |||
57 | // Restrict access to the meta search for registered users only |
||
58 | $GLOBALS['wgAvailableRights'][] = 'sci-metadatasearch'; |
||
59 | $GLOBALS['wgGroupPermissions']['user']['sci-metadatasearch'] = true; |
||
60 | |||
61 | // Register resource files |
||
62 | $GLOBALS['wgResourceModules']['ext.scite.styles'] = [ |
||
63 | 'styles' => 'res/scite.styles.css', |
||
64 | 'localBasePath' => __DIR__ , |
||
65 | 'remoteExtPath' => 'SemanticCite', |
||
66 | 'position' => 'top', |
||
67 | 'group' => 'ext.smw', |
||
68 | 'targets' => [ |
||
69 | 'mobile', |
||
70 | 'desktop' |
||
71 | ] |
||
72 | ]; |
||
73 | |||
74 | $GLOBALS['wgResourceModules']['ext.scite.metadata'] = [ |
||
75 | 'scripts' => [ |
||
76 | 'res/scite.text.selector.js', |
||
77 | 'res/scite.page.creator.js' |
||
78 | ], |
||
79 | 'localBasePath' => __DIR__ , |
||
80 | 'remoteExtPath' => 'SemanticCite', |
||
81 | 'position' => 'top', |
||
82 | 'group' => 'ext.smw', |
||
83 | 'dependencies' => [ |
||
84 | 'ext.scite.styles', |
||
85 | 'mediawiki.api' |
||
86 | ], |
||
87 | 'targets' => [ |
||
88 | 'mobile', |
||
89 | 'desktop' |
||
90 | ] |
||
91 | ]; |
||
92 | |||
93 | $GLOBALS['wgResourceModules']['ext.scite.tooltip'] = [ |
||
94 | 'scripts' => [ |
||
95 | 'res/scite.tooltip.js' |
||
96 | ], |
||
97 | 'localBasePath' => __DIR__ , |
||
98 | 'remoteExtPath' => 'SemanticCite', |
||
99 | 'dependencies' => [ |
||
100 | 'onoi.qtip', |
||
101 | 'onoi.blobstore', |
||
102 | 'onoi.util', |
||
103 | 'ext.scite.styles', |
||
104 | 'mediawiki.api.parse' |
||
105 | ], |
||
106 | 'messages' => [ |
||
107 | 'sci-tooltip-citation-lookup-failure', |
||
108 | 'sci-tooltip-citation-lookup-failure-multiple' |
||
109 | ], |
||
110 | 'targets' => [ |
||
111 | 'mobile', |
||
112 | 'desktop' |
||
113 | ] |
||
114 | ]; |
||
115 | |||
116 | // Register hooks that require to be listed as soon as possible and preferable |
||
117 | // before the execution of onExtensionFunction |
||
118 | HookRegistry::initExtension( $GLOBALS ); |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * @since 1.1 |
||
123 | */ |
||
124 | public static function onExtensionFunction() { |
||
0 ignored issues
–
show
onExtensionFunction 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);
}
}
![]() |
|||
125 | |||
126 | if ( !defined( 'SMW_VERSION' ) ) { |
||
127 | if ( PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg' ) { |
||
128 | die( "\nThe 'Semantic Cite' extension requires 'Semantic MediaWiki' to be installed and enabled.\n" ); |
||
0 ignored issues
–
show
The method
onExtensionFunction() contains an exit expression.
An exit expression should only be used in rare cases. For example, if you write a short command line script. In most cases however, using an ![]() |
|||
129 | } else { |
||
130 | die( '<b>Error:</b> The <a href="https://github.com/SemanticMediaWiki/SemanticCite/">Semantic Cite</a> extension requires <a href="https://www.semantic-mediawiki.org/wiki/Semantic_MediaWiki">Semantic MediaWiki</a> to be installed and enabled.<br />' ); |
||
0 ignored issues
–
show
The method
onExtensionFunction() contains an exit expression.
An exit expression should only be used in rare cases. For example, if you write a short command line script. In most cases however, using an ![]() |
|||
131 | } |
||
132 | } |
||
133 | |||
134 | // Require a global because MW's Special page is missing an interface |
||
135 | // to inject dependencies |
||
136 | $GLOBALS['scigCachePrefix'] = $GLOBALS['wgCachePrefix'] === false ? wfWikiID() : $GLOBALS['wgCachePrefix']; |
||
137 | |||
138 | $configuration = [ |
||
139 | 'numberOfReferenceListColumns' => $GLOBALS['scigNumberOfReferenceListColumns'], |
||
140 | 'browseLinkToCitationResource' => $GLOBALS['scigBrowseLinkToCitationResource'], |
||
141 | 'showTooltipForCitationReference' => $GLOBALS['scigShowTooltipForCitationReference'], |
||
142 | 'tooltipRequestCacheTTL' => $GLOBALS['scigTooltipRequestCacheTTLInSeconds'], |
||
143 | 'citationReferenceCaptionFormat' => $GLOBALS['scigCitationReferenceCaptionFormat'], |
||
144 | 'referenceListType' => $GLOBALS['scigReferenceListType'], |
||
145 | 'enabledStrictParserValidation' => $GLOBALS['scigEnabledStrictParserValidation'], |
||
146 | 'cachePrefix' => $GLOBALS['scigCachePrefix'], |
||
147 | 'enabledCitationTextChangeUpdateJob' => $GLOBALS['scigEnabledCitationTextChangeUpdateJob'], |
||
148 | 'responsiveMonoColumnCharacterBoundLength' => $GLOBALS['scigResponsiveMonoColumnCharacterBoundLength'] |
||
149 | ]; |
||
150 | |||
151 | $applicationFactory = ApplicationFactory::getInstance(); |
||
152 | |||
153 | $hookRegistry = new HookRegistry( |
||
154 | $applicationFactory->getStore(), |
||
155 | $applicationFactory->newCacheFactory()->newMediaWikiCompositeCache( $GLOBALS['scigReferenceListCacheType'] ), |
||
156 | new Options( $configuration ) |
||
157 | ); |
||
158 | |||
159 | $hookRegistry->register(); |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * @since 1.1 |
||
164 | * |
||
165 | * @return string|null |
||
166 | */ |
||
167 | public static function getVersion() { |
||
168 | |||
169 | if ( !defined( 'SCI_VERSION' ) ) { |
||
170 | return null; |
||
171 | } |
||
172 | |||
173 | return SCI_VERSION; |
||
174 | } |
||
175 | |||
176 | } |
||
177 |
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.