Skip to content

Commit aaceba3

Browse files
authored
Changes for Components helper (#60)
* initial setup
1 parent 57719e1 commit aaceba3

12 files changed

+153
-148
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ The semantic versioning started from version 0.2.1.
1010

1111
_No documentation available about unreleased changes yet._
1212

13+
## [3.0.0](https://github.com/infinum/eightshift-coding-standards/compare/2.0.0...3.0.0)
14+
15+
### Changed
16+
- Components helpers in the new eightshift-libs@8.0.0 is deprecated and removed. Instead `Helpers` is used. The `Eightshift.Security.ComponentsEscape` sniff is updated and renamed to `Eightshift.Security.HelpersEscape` to reflect this change.
17+
1318
## [2.0.0](https://github.com/infinum/eightshift-coding-standards/compare/1.6.0...2.0.0) - 2023-09-XX
1419

1520
### Added

Eightshift/Docs/Security/ComponentsEscapeStandard.xml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,45 @@
11
<?xml version="1.0"?>
22
<documentation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
33
xsi:noNamespaceSchemaLocation="https://phpcsstandards.github.io/PHPCSDevTools/phpcsdocs.xsd"
4-
title="Components escape">
4+
title="Helpers escape">
55
<standard>
66
<![CDATA[
77
Modification of the EscapeOutput sniff where we want to silence escape errors on library specific safe static methods.
88
]]>
99
</standard>
1010
<code_comparison>
11-
<code title="Valid: Calling Components::render if the Components class comes from EightshiftLibs">
11+
<code title="Valid: Calling Helpers::render if the Helpers class comes from EightshiftLibs">
1212
<![CDATA[
13-
use ProjectVendor\EightshiftLibs\Helpers\Components;
13+
use ProjectVendor\EightshiftLibs\Helpers\Helpers;
1414
15-
echo Components::render(
15+
echo Helpers::render(
1616
'accordion',
17-
Components::props('accordion', $attributes, [
17+
Helpers::props('accordion', $attributes, [
1818
'accordionContent' => $innerBlockContent
1919
])
2020
);
2121
]]>
2222
</code>
2323
<code title="Invalid: Using globally namespaced methods">
2424
<![CDATA[
25-
echo \Components::outputCssVariables($attributes, $manifest, $unique, $globalManifest);
25+
echo \Helpers::outputCssVariables($attributes, $manifest, $unique, $globalManifest);
2626
]]>
2727
</code>
2828
</code_comparison>
2929
<code_comparison>
3030
<code title="Valid: Referencing fully qualified class name">
3131
<![CDATA[
32-
echo ProjectVendor\EightshiftLibs\Helpers\Components::outputCssVariables(
32+
echo ProjectVendor\EightshiftLibs\Helpers\Helpers::outputCssVariables(
3333
'accordion',
34-
Components::props('accordion', $attributes, [
34+
Helpers::props('accordion', $attributes, [
3535
'accordionContent' => $innerBlockContent
3636
])
3737
);
3838
]]>
3939
</code>
4040
<code title="Invalid: Calling a non safe method">
4141
<![CDATA[
42-
echo Components::props('accordion', $attributes, [
42+
echo Helpers::props('accordion', $attributes, [
4343
'accordionContent' => $innerBlockContent
4444
]);
4545
]]>

Eightshift/Sniffs/Security/ComponentsEscapeSniff.php renamed to Eightshift/Sniffs/Security/HelpersEscapeSniff.php

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,22 @@
2121
* Override the WordPress.Security.EscapeOutput sniff
2222
*
2323
* This sniff will ignore escaping errors whenever it finds the
24-
* EightshiftLibs\Helpers\Components::$allowedMethods, where the $allowedMethods are by default `render` and `outputCssVariables`, but can be extended in the ruleset.
24+
* EightshiftLibs\Helpers\Helpers::$allowedMethods, where the $allowedMethods are by default `render` and `outputCssVariables`, but can be extended in the ruleset.
2525
*
2626
* $allowedMethods are considered safe.
2727
*
2828
* @since 2.0.0 Add list of allowed static methods that shouldn't trigger the sniff error.
2929
* @since 1.4.0
3030
*/
31-
class ComponentsEscapeSniff extends EscapeOutputSniff
31+
class HelpersEscapeSniff extends EscapeOutputSniff
3232
{
3333
/**
34-
* A fully qualified class name for Components class override.
34+
* A fully qualified class name for Helpers class override.
3535
*
3636
* You should put the fully qualified class name of the class you used
3737
* to override the EightshiftLibs\Helpers\Component class.
3838
*
39-
* For Example: namespace\\SomeSubNamespace\\MyComponents.
39+
* For Example: namespace\\SomeSubNamespace\\MyHelpers.
4040
*
4141
* @since 1.4.0
4242
*
@@ -98,7 +98,7 @@ public function process_token($stackPtr)
9898
// Check the next token after echo.
9999
$elementPtr = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
100100

101-
// If it's not the string token, move on, we're only interested in Components string.
101+
// If it's not the string token, move on, we're only interested in Helpers string.
102102
if ($tokens[$elementPtr]['code'] !== \T_STRING) {
103103
return parent::process_token($stackPtr);
104104
}
@@ -111,23 +111,23 @@ public function process_token($stackPtr)
111111
}
112112
}
113113

114-
// Check for Components string token.
115-
$componentsClassNamePtr = $phpcsFile->findNext(\T_STRING, ($stackPtr + 1), null, false, 'Components');
114+
// Check for Helpers string token.
115+
$helpersClassNamePtr = $phpcsFile->findNext(\T_STRING, ($stackPtr + 1), null, false, 'Helpers');
116116

117-
if (!$componentsClassNamePtr) {
118-
// If there is no Components down the line, just run the regular sniff.
117+
if (!$helpersClassNamePtr) {
118+
// If there is no Helpers down the line, just run the regular sniff.
119119
return parent::process_token($stackPtr);
120120
}
121121

122122
// Check if the next token is double colon. We are interested in static methods.
123-
if ($tokens[$componentsClassNamePtr + 1]['code'] !== \T_DOUBLE_COLON) {
124-
$echoPtr = $phpcsFile->findPrevious(\T_ECHO, ($componentsClassNamePtr - 1), null, false, null, true);
123+
if ($tokens[$helpersClassNamePtr + 1]['code'] !== \T_DOUBLE_COLON) {
124+
$echoPtr = $phpcsFile->findPrevious(\T_ECHO, ($helpersClassNamePtr - 1), null, false, null, true);
125125

126126
return parent::process_token($echoPtr);
127127
}
128128

129129
// If it is, check, if the class is imported or fully qualified.
130-
$nameEnd = $phpcsFile->findPrevious(\T_STRING, ($componentsClassNamePtr + 1));
130+
$nameEnd = $phpcsFile->findPrevious(\T_STRING, ($helpersClassNamePtr + 1));
131131
$nameStart = ($phpcsFile->findPrevious(
132132
[\T_STRING, \T_NS_SEPARATOR, \T_NAMESPACE],
133133
($nameEnd - 1),
@@ -140,18 +140,18 @@ public function process_token($stackPtr)
140140
$className = GetTokensAsString::normal($phpcsFile, $nameStart, $nameEnd);
141141

142142
if ($importExists) {
143-
// Fully qualified import, i.e. EightshiftLibs\Helpers\Components.
143+
// Fully qualified import, i.e. EightshiftLibs\Helpers\Helpers.
144144
if ($importData['fullImportExists']) {
145-
// Components name is ok, \Components is not ok, \Anything\Components is not ok FQCN is ok.
145+
// Helpers name is ok, \Helpers is not ok, \Anything\Helpers is not ok FQCN is ok.
146146
if (
147-
$className === 'Components'
148-
|| \strpos($className, 'EightshiftLibs\\Helpers\\Components') !== false
147+
$className === 'Helpers'
148+
|| \strpos($className, 'EightshiftLibs\\Helpers\\Helpers') !== false
149149
|| (! empty($this->overriddenClass) && \strpos($className, $this->overriddenClass) !== false)
150150
) {
151151
// Check the static method name.
152152
$methodNamePtr = $phpcsFile->findNext(
153153
\T_STRING,
154-
($componentsClassNamePtr + 1),
154+
($helpersClassNamePtr + 1),
155155
null,
156156
false,
157157
null,
@@ -162,13 +162,13 @@ public function process_token($stackPtr)
162162
return; // Skip sniffing allowed methods.
163163
} else {
164164
// Not allowed method, continue as usual.
165-
$echoPtr = $this->getEchoToken($componentsClassNamePtr);
165+
$echoPtr = $this->getEchoToken($helpersClassNamePtr);
166166

167167
return parent::process_token($echoPtr);
168168
}
169169
} else {
170170
// Some other class we don't care about.
171-
$echoPtr = $this->getEchoToken($componentsClassNamePtr);
171+
$echoPtr = $this->getEchoToken($helpersClassNamePtr);
172172

173173
return parent::process_token($echoPtr);
174174
}
@@ -184,7 +184,7 @@ public function process_token($stackPtr)
184184
// Correctly used class name.
185185
$methodNamePtr = $phpcsFile->findNext(
186186
\T_STRING,
187-
($componentsClassNamePtr + 1),
187+
($helpersClassNamePtr + 1),
188188
null,
189189
false,
190190
null,
@@ -195,26 +195,26 @@ public function process_token($stackPtr)
195195
return; // Skip sniffing allowed methods.
196196
} else {
197197
// Not allowed method, continue as usual.
198-
$echoPtr = $this->getEchoToken($componentsClassNamePtr);
198+
$echoPtr = $this->getEchoToken($helpersClassNamePtr);
199199

200200
return parent::process_token($echoPtr);
201201
}
202202
} else {
203203
// Wrongly imported, or class that is not related to the libs.
204-
$echoPtr = $this->getEchoToken($componentsClassNamePtr);
204+
$echoPtr = $this->getEchoToken($helpersClassNamePtr);
205205

206206
return parent::process_token($echoPtr);
207207
}
208208
}
209209
} else {
210210
// Check if the class name is fully qualified and contains the helper part.
211211
if (
212-
\strpos($className, 'EightshiftLibs\\Helpers\\Components') !== false
212+
\strpos($className, 'EightshiftLibs\\Helpers\\Helpers') !== false
213213
|| (! empty($this->overriddenClass) && \strpos($className, $this->overriddenClass) !== false)
214214
) {
215215
$methodNamePtr = $phpcsFile->findNext(
216216
\T_STRING,
217-
($componentsClassNamePtr + 1),
217+
($helpersClassNamePtr + 1),
218218
null,
219219
false,
220220
null,
@@ -225,12 +225,12 @@ public function process_token($stackPtr)
225225
return; // Skip sniffing allowed methods.
226226
} else {
227227
// Not allowed method, continue as usual.
228-
$echoPtr = $this->getEchoToken($componentsClassNamePtr);
228+
$echoPtr = $this->getEchoToken($helpersClassNamePtr);
229229

230230
return parent::process_token($echoPtr);
231231
}
232232
} else {
233-
$echoPtr = $this->getEchoToken($componentsClassNamePtr);
233+
$echoPtr = $this->getEchoToken($helpersClassNamePtr);
234234

235235
return parent::process_token($echoPtr);
236236
}
@@ -271,7 +271,7 @@ private function checkIfImportInformation(int $stackPtr, array $importData): arr
271271

272272
// Check for fully qualified import.
273273
if (
274-
\strpos($fullyQualifiedClassNameImport, 'EightshiftLibs\\Helpers\\Components') !== false
274+
\strpos($fullyQualifiedClassNameImport, 'EightshiftLibs\\Helpers\\Helpers') !== false
275275
|| (! empty($overriddenClass) && \strpos($fullyQualifiedClassNameImport, $overriddenClass) !== false) // phpcs:ignore Generic.Files.LineLength.TooLong
276276
) {
277277
$importData['fullImportExists'] = true;
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
<?php
22

3-
use ProjectVendor\EightshiftLibs\Helpers\Components;
3+
use ProjectVendor\EightshiftLibs\Helpers\Helpers;
44

5-
echo Components::render( // OK (imported method)
5+
echo Helpers::render( // OK (imported method)
66
'accordion',
7-
Components::props('accordion', $attributes, [
7+
Helpers::props('accordion', $attributes, [
88
'accordionContent' => $innerBlockContent
99
])
1010
);
1111

12-
echo ProjectVendor\EightshiftLibs\Helpers\Components::render( // OK (FQCN)
12+
echo ProjectVendor\EightshiftLibs\Helpers\Helpers::render( // OK (FQCN)
1313
'accordion',
14-
Components::props('accordion', $attributes, [
14+
Helpers::props('accordion', $attributes, [
1515
'accordionContent' => $innerBlockContent
1616
])
1717
);
1818

19-
echo Components::outputCssVariables($attributes, $manifest, $unique, $globalManifest); // OK (import)
19+
echo Helpers::outputCssVariables($attributes, $manifest, $unique, $globalManifest); // OK (import)
2020

21-
echo \Components::outputCssVariables($attributes, $manifest, $unique, $globalManifest); // Bad (global namespaced class)
21+
echo \Helpers::outputCssVariables($attributes, $manifest, $unique, $globalManifest); // Bad (global namespaced class)
2222

23-
echo Components::getManifest($attributes, $manifest, $unique, $globalManifest); // Bad (Not on the safe method list)
23+
echo Helpers::getManifest($attributes, $manifest, $unique, $globalManifest); // Bad (Not on the safe method list)
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
<?php
22

3-
echo Components::render( // Bad. Not the lib's helper, no import.
3+
echo Helpers::render( // Bad. Not the lib's helper, no import.
44
'accordion',
5-
Components::props('accordion', $attributes, [
5+
Helpers::props('accordion', $attributes, [
66
'accordionContent' => $innerBlockContent
77
])
88
);
99

10-
echo MyClass\Components::render( // Bad. Not the lib's helper.
10+
echo MyClass\Helpers::render( // Bad. Not the lib's helper.
1111
'accordion',
12-
Components::props('accordion', $attributes, [
12+
Helpers::props('accordion', $attributes, [
1313
'accordionContent' => $innerBlockContent
1414
])
1515
);
1616

17-
echo Components::props('accordion', $attributes, [ // Bad. Not safe method.
17+
echo Helpers::props('accordion', $attributes, [ // Bad. Not safe method.
1818
'accordionContent' => $innerBlockContent
1919
]);
2020

21-
echo ProjectVendor\EightshiftLibs\Helpers\Components::render( // OK (FQCN)
21+
echo ProjectVendor\EightshiftLibs\Helpers\Helpers::render( // OK (FQCN)
2222
'accordion',
23-
Components::props('accordion', $attributes, [
23+
Helpers::props('accordion', $attributes, [
2424
'accordionContent' => $innerBlockContent
2525
])
2626
);

Eightshift/Tests/Security/ComponentsEscapeUnitTest.3.inc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22

33
use ProjectVendor\EightshiftLibs\Helpers;
44

5-
echo Helpers\Components::render( // OK (imported method, even though not correctly, it's still valid)
5+
echo Helpers\Helpers::render( // OK (imported method, even though not correctly, it's still valid)
66
'accordion',
7-
Components::props('accordion', $attributes, [
7+
Helpers::props('accordion', $attributes, [
88
'accordionContent' => $innerBlockContent
99
])
1010
);
1111

12-
echo Components::render( // Bad (Not correctly imported)
12+
echo Helpers::render( // Bad (Not correctly imported)
1313
'accordion',
14-
Components::props('accordion', $attributes, [
14+
Helpers::props('accordion', $attributes, [
1515
'accordionContent' => $innerBlockContent
1616
])
1717
);
1818

19-
echo (new Components())->render( // Bad
19+
echo (new Helpers())->render( // Bad
2020
'accordion',
2121
[]
2222
);

0 commit comments

Comments
 (0)