Skip to content

Commit 13636d1

Browse files
committed
update and formatting
1 parent 2472880 commit 13636d1

File tree

3 files changed

+138
-209
lines changed

3 files changed

+138
-209
lines changed

src/TitleFormatter.php

+31-89
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,28 @@ class TitleFormatter
66
{
77
/**
88
* The title that we need to format and return.
9-
*
10-
* @var string|null
119
*/
12-
protected $title;
10+
protected ?string $title = null;
1311

1412
/**
1513
* The separator character for in between words.
16-
*
17-
* @var string
1814
*/
19-
protected $separator = ' ';
15+
protected string $separator = ' ';
2016

2117
/**
2218
* Encoding used for mb_ functions.
23-
*
24-
* @var string
2519
*/
26-
protected $encoding = 'UTF-8';
20+
protected string $encoding = 'UTF-8';
2721

2822
/**
2923
* Collection of words generated from the original title.
30-
*
31-
* @var array
3224
*/
33-
protected $indexedWords = [];
25+
protected array $indexedWords = [];
3426

3527
/**
3628
* Words that should be ignored from capitalization.
37-
*
38-
* @var array
3929
*/
40-
protected $ignoredWords = [
30+
protected array $ignoredWords = [
4131
'a',
4232
'an',
4333
'and',
@@ -59,22 +49,16 @@ class TitleFormatter
5949
'via',
6050
];
6151

62-
/**
63-
* @param string $title
64-
* @param string $separator
65-
*/
66-
private function __construct($title, $separator = ' ')
52+
private function __construct(?string $title = null, string $separator = ' ')
6753
{
6854
$this->setTitle($title);
6955
$this->separator = $separator;
7056
}
7157

7258
/**
7359
* Converts the initial title to a correctly formatted one.
74-
*
75-
* @return string
7660
*/
77-
public function convertTitle()
61+
public function convertTitle(): string
7862
{
7963
if ($this->title === null) {
8064
return '';
@@ -86,46 +70,39 @@ public function convertTitle()
8670
}
8771
}
8872

89-
return $this->title;
73+
return $this->title ?? '';
9074
}
9175

9276
/**
9377
* Returns the newly formatted title.
94-
*
95-
* @param string $title
96-
* @param string $separator
97-
* @return string
9878
*/
99-
public static function titleCase($title, $separator = ' ')
79+
public static function titleCase(?string $title = null, string $separator = ' '): string
10080
{
10181
return (new self($title, $separator))->convertTitle();
10282
}
10383

10484
/**
10585
* Sets the title after cleaning up extra spaces.
106-
*
107-
* @param string $title
10886
*/
109-
protected function setTitle($title)
87+
protected function setTitle(?string $title = null): void
11088
{
11189
$title = trim(preg_replace('/\s\s+/', ' ', str_replace("\n", ' ', $title)));
11290

113-
if ($title != '') {
91+
if ($title !== '') {
11492
$this->title = $title;
11593
}
11694
}
11795

11896
/**
11997
* Creates an array of words from the title to be formatted.
12098
*/
121-
protected function splitWords()
99+
protected function splitWords(): array
122100
{
123101
$indexedWords = [];
124102
$offset = 0;
125103

126-
$words = explode($this->separator, $this->title);
127-
foreach ($words as $word) {
128-
if (mb_strlen($word, $this->encoding) == 0) {
104+
foreach (explode($this->separator, $this->title) as $word) {
105+
if (mb_strlen($word, $this->encoding) === 0) {
129106
continue;
130107
}
131108

@@ -145,12 +122,8 @@ protected function splitWords()
145122

146123
/**
147124
* Finds the correct index of the word within the title.
148-
*
149-
* @param $word
150-
* @param $offset
151-
* @return int
152125
*/
153-
protected function getWordIndex($word, $offset)
126+
protected function getWordIndex(string $word, int $offset): int
154127
{
155128
$index = mb_strpos($this->title, $word, $offset, $this->encoding);
156129

@@ -159,22 +132,16 @@ protected function getWordIndex($word, $offset)
159132

160133
/**
161134
* Corrects the potential offset issue with some UTF-8 characters.
162-
*
163-
* @param $index
164-
* @return int
165135
*/
166-
protected function correctIndexOffset($index)
136+
protected function correctIndexOffset(?int $index): int
167137
{
168138
return mb_strlen(mb_substr($this->title, 0, $index, $this->encoding), $this->encoding);
169139
}
170140

171141
/**
172142
* Replaces a formatted word within the current title.
173-
*
174-
* @param int $index
175-
* @param string $word
176143
*/
177-
protected function rebuildTitle($index, $word)
144+
protected function rebuildTitle(int $index, string $word): void
178145
{
179146
$this->title =
180147
mb_substr($this->title, 0, $index, $this->encoding) .
@@ -189,11 +156,8 @@ protected function rebuildTitle($index, $word)
189156

190157
/**
191158
* Performs the uppercase action on the given word.
192-
*
193-
* @param $word
194-
* @return string
195159
*/
196-
protected function uppercaseWord($word)
160+
protected function uppercaseWord(string $word): string
197161
{
198162
// see if first characters are special
199163
$prefix = '';
@@ -213,31 +177,24 @@ protected function uppercaseWord($word)
213177

214178
/**
215179
* Condition to see if the given word should be uppercase.
216-
*
217-
* @param $index
218-
* @param $word
219-
* @return bool
220180
*/
221-
protected function wordShouldBeUppercase($index, $word)
181+
protected function wordShouldBeUppercase(int $index, string $word): bool
222182
{
223183
return
224184
(
225185
$this->isFirstWordOfSentence($index) ||
226186
$this->isLastWord($word) ||
227-
!$this->isIgnoredWord($word)
187+
! $this->isIgnoredWord($word)
228188
) &&
229189
(
230-
!$this->hasUppercaseLetter($word)
190+
! $this->hasUppercaseLetter($word)
231191
);
232192
}
233193

234194
/**
235195
* Checks to see if the word is the last word in the title.
236-
*
237-
* @param $word
238-
* @return bool
239196
*/
240-
protected function isLastWord($word)
197+
protected function isLastWord(string $word): bool
241198
{
242199
if ($word === end($this->indexedWords)) {
243200
return true;
@@ -247,14 +204,11 @@ protected function isLastWord($word)
247204
}
248205

249206
/**
250-
* Checks to see if the word the start of a new sentence.
251-
*
252-
* @param $index
253-
* @return bool
207+
* Checks to see if the word is the start of a new sentence.
254208
*/
255-
protected function isFirstWordOfSentence($index)
209+
protected function isFirstWordOfSentence(int $index): bool
256210
{
257-
if ($index == 0) {
211+
if ($index === 0) {
258212
return true;
259213
}
260214

@@ -269,47 +223,35 @@ protected function isFirstWordOfSentence($index)
269223

270224
/**
271225
* Checks to see if the given string is a punctuation character.
272-
*
273-
* @param $string
274-
* @return int
275226
*/
276-
protected function isPunctuation($string)
227+
protected function isPunctuation(string $string): int
277228
{
278229
return preg_match("/[\p{P}\p{S}]/u", $string);
279230
}
280231

281232
/**
282233
* Checks if the given word should be ignored.
283-
*
284-
* @param $word
285-
* @return bool
286234
*/
287-
protected function isIgnoredWord($word)
235+
protected function isIgnoredWord(string $word): bool
288236
{
289-
return in_array($word, $this->ignoredWords);
237+
return in_array($word, $this->ignoredWords, true);
290238
}
291239

292240
/**
293241
* Checks to see if a word has an uppercase letter.
294-
*
295-
* @param $word
296-
* @return int
297242
*/
298-
protected function hasUppercaseLetter($word)
243+
protected function hasUppercaseLetter(string $word): int
299244
{
300245
return preg_match('/[A-Z]/', $word);
301246
}
302247

303248
/**
304249
* Checks to see if the word has a dash.
305-
*
306-
* @param $word
307-
* @return int
308250
*/
309-
protected function hasDash($word)
251+
protected function hasDash(string $word): bool
310252
{
311253
$wordWithoutDashes = str_replace('-', '', $word);
312254

313-
return preg_match("/\-/", $word) && mb_strlen($wordWithoutDashes, $this->encoding) > 1;
255+
return (bool) preg_match("/\-/", $word) && mb_strlen($wordWithoutDashes, $this->encoding) > 1;
314256
}
315257
}

tests/TestCase.php

-9
This file was deleted.

0 commit comments

Comments
 (0)