Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Pull Request — master (#44)
by Der Mundschenk
02:23
created

Registry   C

Complexity

Total Complexity 13

Size/Duplication

Total Lines 175
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 27

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 27
dl 0
loc 175
rs 5
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A get_node_fixes() 0 3 1
A register_node_fix() 0 7 2
A register_token_fix() 0 3 1
A update_hyphenator_cache() 0 3 1
B apply_fixes() 0 9 5
B create() 0 46 2
1
<?php
2
/**
3
 *  This file is part of PHP-Typography.
4
 *
5
 *  Copyright 2017 Peter Putzer.
6
 *
7
 *  This program is free software; you can redistribute it and/or modify modify
8
 *  it under the terms of the GNU General Public License as published by
9
 *  the Free Software Foundation; either version 2 of the License, or
10
 *  (at your option) any later version.
11
 *
12
 *  This program is distributed in the hope that it will be useful,
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *  GNU General Public License for more details.
16
 *
17
 *  You should have received a copy of the GNU General Public License along
18
 *  with this program; if not, write to the Free Software Foundation, Inc.,
19
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20
 *
21
 *  ***
22
 *
23
 *  @package mundschenk-at/php-typography
24
 *  @license http://www.gnu.org/licenses/gpl-2.0.html
25
 */
26
27
namespace PHP_Typography\Fixes;
28
29
use PHP_Typography\Settings;
30
31
use PHP_Typography\Hyphenator\Cache;
32
33
use PHP_Typography\Fixes\Node_Fix;
34
use PHP_Typography\Fixes\Token_Fix;
35
use PHP_Typography\Fixes\Token_Fixes\Hyphenate_Fix;
36
37
/**
38
 * Manages the fixes used by PHP_Typography.
39
 *
40
 * @author Peter Putzer <[email protected]>
41
 *
42
 * @since 6.0.0
43
 */
44
class Registry {
45
46
	const CHARACTERS         = 10;
47
	const SPACING_PRE_WORDS  = 20;
48
	const PROCESS_WORDS      = 30;
49
	const SPACING_POST_WORDS = 40;
50
	const HTML_INSERTION     = 50;
51
52
	const GROUPS = [ self::CHARACTERS, self::SPACING_PRE_WORDS, self::PROCESS_WORDS, self::SPACING_POST_WORDS, self::HTML_INSERTION ];
53
54
	/**
55
	 * An array of CSS classes that are added for ampersands, numbers etc.
56
	 */
57
	const DEFAULT_CSS_CLASSES = [
58
		'caps'        => 'caps',
59
		'numbers'     => 'numbers',
60
		'amp'         => 'amp',
61
		'quo'         => 'quo',
62
		'dquo'        => 'dquo',
63
		'pull-single' => 'pull-single',
64
		'pull-double' => 'pull-double',
65
		'push-single' => 'push-single',
66
		'push-double' => 'push-double',
67
		'numerator'   => 'numerator',
68
		'denominator' => 'denominator',
69
		'ordinal'     => 'ordinal',
70
	];
71
72
	/**
73
	 * An array of Node_Fix implementations.
74
	 *
75
	 * @var array
76
	 */
77
	private $node_fixes = [
78
		self::CHARACTERS         => [],
79
		self::SPACING_PRE_WORDS  => [],
80
		self::PROCESS_WORDS      => [],
81
		self::SPACING_POST_WORDS => [],
82
		self::HTML_INSERTION     => [],
83
	];
84
85
	/**
86
	 * The token fix registry.
87
	 *
88
	 * @var Node_Fixes\Process_Words_Fix
89
	 */
90
	private $process_words_fix;
91
92
	/**
93
	 * Creates new registry instance.
94
	 */
95
	public function __construct() {
96
		// Parse and process individual words.
97
		$this->process_words_fix = new Node_Fixes\Process_Words_Fix();
98
		$this->register_node_fix( $this->process_words_fix, self::PROCESS_WORDS );
99
	}
100
101
	/**
102
	 * Retrieves the registered node fixes.
103
	 *
104
	 * @return Node_Fix[][]
105
	 */
106
	public function get_node_fixes() {
107
		return $this->node_fixes;
108
	}
109
110
	/**
111
	 * Registers a node fix.
112
	 *
113
	 * @since 5.0.0
114
	 *
115
	 * @param Node_Fix $fix   Required.
116
	 * @param int      $group Required. Only the constants CHARACTERS, SPACING_PRE_WORDS, SPACING_POST_WORDS, HTML_INSERTION are valid.
117
	 *
118
	 * @throws \InvalidArgumentException Group is invalid.
119
	 */
120
	public function register_node_fix( Node_Fix $fix, $group ) {
121
		if ( isset( $this->node_fixes[ $group ] ) ) {
122
			$this->node_fixes[ $group ][] = $fix;
123
		} else {
124
			throw new \InvalidArgumentException( "Invalid fixer group $group." );
125
		}
126
	}
127
128
	/**
129
	 * Registers a token fix.
130
	 *
131
	 * @param Token_Fix $fix Required.
132
	 */
133
	public function register_token_fix( Token_Fix $fix ) {
134
		$this->process_words_fix->register_token_fix( $fix );
135
	}
136
137
	/**
138
	 * Sets the hyphenator cache for all registered token fixes (that require one).
139
	 *
140
	 * @param Cache $cache A hyphenator cache instance.
141
	 */
142
	public function update_hyphenator_cache( Cache $cache ) {
143
		$this->process_words_fix->update_hyphenator_cache( $cache );
144
	}
145
146
	/**
147
	 * Applies typography fixes to a textnode.
148
	 *
149
	 * @param \DOMText $textnode The node to process.
150
	 * @param Settings $settings The settings to apply.
151
	 * @param bool     $is_title Treat as title/heading tag if true.
152
	 * @param bool     $is_feed  Check for feed compatibility if true.
153
	 */
154
	public function apply_fixes( \DOMText $textnode, Settings $settings, $is_title, $is_feed ) {
155
		foreach ( $this->node_fixes as $group => $fixes ) {
156
			foreach ( $fixes as $fix ) {
157
				if ( ! $is_feed || $fix->feed_compatible() ) {
158
					$fix->apply( $textnode, $settings, $is_title );
159
				}
160
			}
161
		}
162
	}
163
164
	/**
165
	 * Creates a new default registry instance.
166
	 *
167
	 * @param Cache|null    $cache       Optional. A hyphenatation cache instance to use. Default null.
168
	 * @param string[]|null $css_classes Optional. An array of CSS classes to use. Defaults to null (i.e. use the predefined classes).
169
	 *
170
	 * @return Registry
171
	 */
172
	public static function create( Cache $cache = null, array $css_classes = null ) {
173
		$registry = new Registry();
174
175
		if ( null === $css_classes ) {
176
			$css_classes = self::DEFAULT_CSS_CLASSES;
177
		}
178
179
		// Nodify anything that requires adjacent text awareness here.
180
		$registry->register_node_fix( new Node_Fixes\Smart_Maths_Fix(),          self::CHARACTERS );
181
		$registry->register_node_fix( new Node_Fixes\Smart_Diacritics_Fix(),     self::CHARACTERS );
182
		$registry->register_node_fix( new Node_Fixes\Smart_Quotes_Fix( true ),   self::CHARACTERS );
183
		$registry->register_node_fix( new Node_Fixes\Smart_Dashes_Fix( true ),   self::CHARACTERS );
184
		$registry->register_node_fix( new Node_Fixes\Smart_Ellipses_Fix( true ), self::CHARACTERS );
185
		$registry->register_node_fix( new Node_Fixes\Smart_Marks_Fix( true ),    self::CHARACTERS );
186
187
		// Keep spacing after smart character replacement.
188
		$registry->register_node_fix( new Node_Fixes\Single_Character_Word_Spacing_Fix(), self::SPACING_PRE_WORDS );
189
		$registry->register_node_fix( new Node_Fixes\Dash_Spacing_Fix(),                  self::SPACING_PRE_WORDS );
190
		$registry->register_node_fix( new Node_Fixes\Unit_Spacing_Fix(),                  self::SPACING_PRE_WORDS );
191
		$registry->register_node_fix( new Node_Fixes\Numbered_Abbreviation_Spacing_Fix(), self::SPACING_PRE_WORDS );
192
		$registry->register_node_fix( new Node_Fixes\French_Punctuation_Spacing_Fix(),    self::SPACING_PRE_WORDS );
193
194
		// Some final space manipulation.
195
		$registry->register_node_fix( new Node_Fixes\Dewidow_Fix(),        self::SPACING_POST_WORDS );
196
		$registry->register_node_fix( new Node_Fixes\Space_Collapse_Fix(), self::SPACING_POST_WORDS );
197
198
		// Everything that requires HTML injection occurs here (functions above assume tag-free content)
199
		// pay careful attention to functions below for tolerance of injected tags.
200
		$registry->register_node_fix( new Node_Fixes\Smart_Ordinal_Suffix_Fix( $css_classes['ordinal'] ),                           self::HTML_INSERTION ); // call before "style_numbers" and "smart_fractions".
201
		$registry->register_node_fix( new Node_Fixes\Smart_Exponents_Fix(),                                                         self::HTML_INSERTION ); // call before "style_numbers".
202
		$registry->register_node_fix( new Node_Fixes\Smart_Fractions_Fix( $css_classes['numerator'], $css_classes['denominator'] ), self::HTML_INSERTION ); // call before "style_numbers" and after "smart_ordinal_suffix".
203
		$registry->register_node_fix( new Node_Fixes\Style_Caps_Fix( $css_classes['caps'] ),                                        self::HTML_INSERTION ); // Call before "style_numbers".
204
		$registry->register_node_fix( new Node_Fixes\Style_Numbers_Fix( $css_classes['numbers'] ),                                  self::HTML_INSERTION ); // Call after "smart_ordinal_suffix", "smart_exponents", "smart_fractions", and "style_caps".
205
		$registry->register_node_fix( new Node_Fixes\Style_Ampersands_Fix( $css_classes['amp'] ),                                   self::HTML_INSERTION );
206
		$registry->register_node_fix( new Node_Fixes\Style_Initial_Quotes_Fix( $css_classes['quo'], $css_classes['dquo'] ),         self::HTML_INSERTION );
207
		$registry->register_node_fix( new Node_Fixes\Style_Hanging_Punctuation_Fix( $css_classes['push-single'], $css_classes['push-double'], $css_classes['pull-single'], $css_classes['pull-double'] ), self::HTML_INSERTION );
208
209
		// Register token fixes.
210
		$registry->register_token_fix( new Token_Fixes\Wrap_Hard_Hyphens_Fix() );
211
		$registry->register_token_fix( new Token_Fixes\Hyphenate_Compounds_Fix( $cache ) );
212
		$registry->register_token_fix( new Token_Fixes\Hyphenate_Fix( $cache ) );
213
		$registry->register_token_fix( new Token_Fixes\Wrap_URLs_Fix( $cache ) );
214
		$registry->register_token_fix( new Token_Fixes\Wrap_Emails_Fix() );
215
216
		return $registry;
217
	}
218
}
219