HookRegistry::addCallbackHandlers()   B
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 135

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 68
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 135
rs 8
c 0
b 0
f 0
ccs 68
cts 68
cp 1
cc 3
nc 1
nop 2
crap 3

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace SBL;
4
5
use SMW\Store;
6
use SMW\ApplicationFactory;
7
use DummyLinker;
8
use Hooks;
9
10
/**
11
 * @license GNU GPL v2+
12
 * @since 1.0
13
 *
14
 * @author mwjames
15
 */
16
class HookRegistry {
17
18
	/**
19
	 * @var array
20
	 */
21
	private $handlers = [];
22
23
	/**
24
	 * @since 1.0
25
	 *
26
	 * @param Store $store
27
	 * @param Options $options
28
	 */
29 2
	public function __construct( Store $store, Options $options ) {
30 2
		$this->addCallbackHandlers( $store, $options );
31 2
	}
32
33
	/**
34
	 * @since  1.1
35
	 *
36
	 * @param string $name
37
	 *
38
	 * @return boolean
39
	 */
40 1
	public function isRegistered( $name ) {
41 1
		return Hooks::isRegistered( $name );
42
	}
43
44
	/**
45
	 * @since  1.1
46
	 *
47
	 * @param string $name
48
	 *
49
	 * @return Callable|false
50
	 */
51 1
	public function getHandlerFor( $name ) {
52 1
		return isset( $this->handlers[$name] ) ? $this->handlers[$name] : false;
53
	}
54
55
	/**
56
	 * @since  1.0
57
	 */
58 1
	public function register() {
59 1
		foreach ( $this->handlers as $name => $callback ) {
60 1
			Hooks::register( $name, $callback );
61 1
		}
62 1
	}
63
64 2
	private function addCallbackHandlers( $store, $options ) {
65
66
		/**
67
		 * @see https://github.com/SemanticMediaWiki/SemanticMediaWiki/blob/master/docs/technical/hooks.md
68
		 */
69 2
		$this->handlers['SMW::Property::initProperties'] = function( $baseRegistry ) {
70
71 1
			$propertyRegistry = new PropertyRegistry();
72
73 1
			$propertyRegistry->register(
74
				$baseRegistry
75 1
			);
76
77 1
			return true;
78
		};
79
80
		/**
81
		 * @see https://www.semantic-mediawiki.org/wiki/Hooks/SMW::Parser::BeforeMagicWordsFinder
82
		 */
83 2
		$this->handlers['SMW::Parser::BeforeMagicWordsFinder'] = function( array &$magicWords ) {
84 1
			$magicWords = array_merge( $magicWords, [ 'SBL_NOBREADCRUMBLINKS' ] );
85 1
			return true;
86
		};
87
88
		/**
89
		 * @note This is bit of a hack but there is no other way to get access to
90
		 * the ParserOutput
91
		 *
92
		 * @see https://www.mediawiki.org/wiki/Manual:Hooks/OutputPageParserOutput
93
		 */
94 2
		$this->handlers['OutputPageParserOutput'] = function( &$outputPage, $parserOutput ) {
95 1
			$outputPage->smwmagicwords = $parserOutput->getExtensionData( 'smwmagicwords' );
96 1
			return true;
97
		};
98
99
		/**
100
		 * @see https://www.mediawiki.org/wiki/Manual:Hooks/SkinTemplateOutputPageBeforeExec
101
		 */
102
		$this->handlers['SkinTemplateOutputPageBeforeExec'] = function ( &$skin, &$template ) use( $store, $options ) {
103
104 1
			$bySubpageLinksFinder = new BySubpageLinksFinder();
105 1
			$bySubpageLinksFinder->setSubpageDiscoveryFallback(
106 1
				$options->get( 'useSubpageFinderFallback' )
107 1
			);
108
109 1
			$byPropertyHierarchicalLinksFinder = new ByPropertyHierarchicalLinksFinder( $store );
110 1
			$byPropertyHierarchicalLinksFinder->setFindClosestDescendantState(
111 1
				$options->get( 'tryToFindClosestDescendant' )
112 1
			);
113
114 1
			$byPropertyHierarchicalLinksFinder->setPropertySearchPatternByNamespace(
115 1
				$options->get( 'propertySearchPatternByNamespace' )
116 1
			);
117
118 1
			$htmlBreadcrumbLinksBuilder = new HtmlBreadcrumbLinksBuilder(
119 1
				$byPropertyHierarchicalLinksFinder,
120
				$bySubpageLinksFinder
121 1
			);
122
123 1
			$htmlBreadcrumbLinksBuilder->setLinker( new DummyLinker() );
124 1
			$htmlBreadcrumbLinksBuilder->setBreadcrumbTrailStyleClass(
125 1
				$options->get( 'breadcrumbTrailStyleClass' )
126 1
			);
127
128 1
			$htmlBreadcrumbLinksBuilder->setBreadcrumbDividerStyleClass(
129 1
				$options->get( 'breadcrumbDividerStyleClass' )
130 1
			);
131
132 1
			$htmlBreadcrumbLinksBuilder->hideSubpageParent(
133 1
				$options->get( 'hideSubpageParent' )
134 1
			);
135
136 1
			$skinTemplateOutputModifier = new SkinTemplateOutputModifier(
137 1
				$htmlBreadcrumbLinksBuilder,
138 1
				ApplicationFactory::getInstance()->getNamespaceExaminer()
139 1
			);
140
141 1
			$skinTemplateOutputModifier->modify( $skin->getOutput(), $template );
142
143 1
			return true;
144
		};
145
146
		/**
147
		 * @see https://www.mediawiki.org/wiki/Manual:Hooks/BeforePageDisplay
148
		 */
149
		$this->handlers['BeforePageDisplay'] = function ( &$output, &$skin ) use ( $options ) {
0 ignored issues
show
Unused Code introduced by
The parameter $skin is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
150
151 1
			$pageDisplayOutputModifier = new PageDisplayOutputModifier();
152
153 1
			$pageDisplayOutputModifier->hideSubpageParent(
154 1
				$options->get( 'hideSubpageParent' )
155 1
			);
156
157 1
			$pageDisplayOutputModifier->setSubpageByNamespace(
158 1
				$options->get( 'wgNamespacesWithSubpages' )
159 1
			);
160
161 1
			$pageDisplayOutputModifier->modifyOutput( $output );
162
163 1
			return true;
164
		};
165
166
		/**
167
		 * @see https://www.mediawiki.org/wiki/Manual:Hooks/ParserAfterTidy
168
		 */
169
		$this->handlers['ParserAfterTidy'] = function ( &$parser, &$text ) use ( $options ) {
0 ignored issues
show
Unused Code introduced by
The parameter $text is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
170
171
			// ParserOptions::getInterfaceMessage is being used to identify whether a
172
			// parse was initiated by `Message::parse`
173 1
			if ( $parser->getTitle()->isSpecialPage() || $parser->getOptions()->getInterfaceMessage() ) {
174 1
				return true;
175
			}
176
177 1
			$parserData = ApplicationFactory::getInstance()->newParserData(
178 1
				$parser->getTitle(),
179 1
				$parser->getOutput()
180 1
			);
181
182 1
			$subpageParentAnnotator = new SubpageParentAnnotator(
183
				$parserData
184 1
			);
185
186 1
			$subpageParentAnnotator->enableSubpageParentAnnotation(
187 1
				$options->get( 'enabledSubpageParentAnnotation' )
188 1
			);
189
190 1
			$subpageParentAnnotator->disableTranslationSubpageAnnotation(
191 1
				$options->get( 'disableTranslationSubpageAnnotation' )
192 1
			);
193
194 1
			$subpageParentAnnotator->addAnnotation();
195
196 1
			return true;
197
		};
198 2
	}
199
200
}
201