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.
Completed
Push — master ( d83874...24d968 )
by Der Mundschenk
12s
created

Dewidow_Fix::apply()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 5
nc 3
nop 3
1
<?php
2
/**
3
 *  This file is part of PHP-Typography.
4
 *
5
 *  Copyright 2014-2017 Peter Putzer.
6
 *  Copyright 2009-2011 KINGdesk, LLC.
7
 *
8
 *  This program is free software; you can redistribute it and/or
9
 *  modify it under the terms of the GNU General Public License
10
 *  as published by the Free Software Foundation; either version 2
11
 *  of the License, or (at your option) any later version.
12
 *
13
 *  This program is distributed in the hope that it will be useful,
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 *  GNU General Public License for more details.
17
 *
18
 *  You should have received a copy of the GNU General Public License
19
 *  along with this program; if not, write to the Free Software
20
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21
 *
22
 *  ***
23
 *
24
 *  @package mundschenk-at/php-typography
25
 *  @license http://www.gnu.org/licenses/gpl-2.0.html
26
 */
27
28
namespace PHP_Typography\Fixes\Node_Fixes;
29
30
use \PHP_Typography\DOM;
31
use \PHP_Typography\RE;
32
use \PHP_Typography\Settings;
33
use \PHP_Typography\Strings;
34
use \PHP_Typography\U;
35
36
/**
37
 * Prevents widows (if enabled).
38
 *
39
 * @author Peter Putzer <[email protected]>
40
 *
41
 * @since 5.0.0
42
 */
43
class Dewidow_Fix extends Abstract_Node_Fix {
44
	const SPACE_BETWEEN = '[\s]+'; // \s includes all special spaces (but not ZWSP) with the u flag.
45
	const WIDOW = '[\w\p{M}\-' . U::ZERO_WIDTH_SPACE . U::SOFT_HYPHEN . ']+?'; // \w includes all alphanumeric Unicode characters but not composed characters.
46
47
	const REGEX_START = '/
48
		(?:
49
			\A
50
			|
51
			(?:
52
				(?<space_before>            # subpattern 1: space before (note: ZWSP is not a space)
53
					[\s' . U::ZERO_WIDTH_SPACE . U::SOFT_HYPHEN . ']+
54
				)
55
				(?<neighbor>                # subpattern 2: neighbors widow (short as possible)
56
					[^\s' . U::ZERO_WIDTH_SPACE . U::SOFT_HYPHEN . ']+?
57
				)
58
			)
59
		)
60
		(?<space_between>                   # subpattern 3: space between
61
			' . self::SPACE_BETWEEN . '
62
		)
63
		(?<widow>                           # subpattern 4: widow
64
			' . self::WIDOW . '
65
			(?:
66
				' . self::SPACE_BETWEEN . self::WIDOW . '
67
			){0,'; // The maximum number of repetitions is missing.
68
69
	const REGEX_END =
70
		'})
71
		(?<trailing>                       # subpattern 5: any trailing punctuation or spaces
72
			[^\w\p{M}]*
73
		)
74
		\Z
75
	/Sxu';
76
77
	/**
78
	 * Apply the fix to a given textnode.
79
	 *
80
	 * @param \DOMText $textnode Required.
81
	 * @param Settings $settings Required.
82
	 * @param bool     $is_title Optional. Default false.
83
	 */
84
	public function apply( \DOMText $textnode, Settings $settings, $is_title = false ) {
85
		// Intervening inline tags may interfere with widow identification, but that is a sacrifice of using the parser.
86
		// Intervening tags will only interfere if they separate the widow from previous or preceding whitespace.
87
		if ( empty( $settings['dewidow'] ) || empty( $settings['dewidowMaxPull'] ) || empty( $settings['dewidowMaxLength'] ) ) {
88
			return;
89
		}
90
91
		if ( '' === DOM::get_next_chr( $textnode ) ) {
92
			// We have the last type "text" child of a block level element.
93
			$textnode->data = $this->dewidow( $textnode->data, Strings::functions( $textnode->data ), $settings['dewidowMaxPull'], $settings['dewidowMaxLength'], $settings['dewidowWordNumber'] );
94
		}
95
	}
96
97
	/**
98
	 * Dewidow a given text fragment.
99
	 *
100
	 * @param  string $text        The text fragment to dewidow.
101
	 * @param  array  $func        An array of string functions.
102
	 * @param  int    $max_pull    Maximum number of characters pulled from previous line.
103
	 * @param  int    $max_length  Maximum widow length.
104
	 * @param  int    $word_number Maximum number of words allowed in widow.
105
	 *
106
	 * @return string
107
	 */
108
	protected function dewidow( $text, array $func, $max_pull, $max_length, $word_number ) {
109
		if ( $word_number < 1 ) {
110
			return $text; // We are done.
111
		}
112
113
		// Do what we have to do.
114
		return preg_replace_callback( self::REGEX_START . ( $word_number - 1 ) . self::REGEX_END, function( array $widow ) use ( $func, $max_pull, $max_length, $word_number ) {
115
116
			// If we are here, we know that widows are being protected in some fashion
117
			// with that, we will assert that widows should never be hyphenated or wrapped
118
			// as such, we will strip soft hyphens and zero-width-spaces.
119
			$widow['widow']    = self::strip_breaking_characters( $widow['widow'] );
120
			$widow['trailing'] = self::strip_breaking_characters( self::make_space_nonbreaking( $widow['trailing'], $func['u'] ) );
121
122
			if (
123
				// Eject if widows neighbor is proceeded by a no break space (the pulled text would be too long).
124
				'' === $widow['space_before'] || strstr( U::NO_BREAK_SPACE, $widow['space_before'] ) ||
125
126
				// Eject if widows neighbor length exceeds the max allowed or widow length exceeds max allowed.
127
				$func['strlen']( $widow['neighbor'] ) > $max_pull || $func['strlen']( $widow['widow'] ) > $max_length ||
128
129
				// Never replace thin and hair spaces with &nbsp;.
130
				U::THIN_SPACE === $widow['space_between'] || U::HAIR_SPACE === $widow['space_between']
131
			) {
132
				return $widow['space_before'] . $widow['neighbor'] . $this->dewidow( $widow['space_between'] . $widow['widow'] . $widow['trailing'], $func, $max_pull, $max_length, $word_number - 1 );
133
			}
134
135
			// Let's protect some widows!
136
			return $widow['space_before'] . $widow['neighbor'] . U::NO_BREAK_SPACE . self::make_space_nonbreaking( $widow['widow'], $func['u'] ) . $widow['trailing'];
137
		}, $text );
138
	}
139
140
141
	/**
142
	 * Strip zero-width space and soft hyphens from the given string.
143
	 *
144
	 * @param  string $string Required.
145
	 *
146
	 * @return string
147
	 */
148
	protected static function strip_breaking_characters( $string ) {
149
		return str_replace( [ U::ZERO_WIDTH_SPACE, U::SOFT_HYPHEN ], '', $string );
150
	}
151
152
	/**
153
	 * Strip zero-width space and soft hyphens from the given string.
154
	 *
155
	 * @param  string $string  Required.
156
	 * @param  string $unicode The "u" flag for Unicode regular expressions or an empty string.
157
	 *
158
	 * @return string
159
	 */
160
	protected static function make_space_nonbreaking( $string, $unicode ) {
161
		return preg_replace( "/\s+/{$unicode}", U::NO_BREAK_SPACE, $string );
162
	}
163
}
164