Skip to content

Commit

Permalink
Merge pull request wp-cli#161 from wp-cli/phpcs-scope-variable-whitel…
Browse files Browse the repository at this point in the history
…ist-more-finely

Fine-tune property and variable name whitelisting
  • Loading branch information
schlessera authored Apr 24, 2019
2 parents e4edd2f + f63b1e7 commit e7a3650
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 14 deletions.
10 changes: 6 additions & 4 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,13 @@
<exclude-pattern>*/src/IterableCodeExtractor\.php$</exclude-pattern>
</rule>

<!-- Whitelist variable names for classes that extend a PSR-2 package. -->
<!-- Whitelist property name for a a JSON-decoded object. -->
<rule ref="WordPress.NamingConventions.ValidVariableName">
<exclude-pattern>*/src/JsFunctionsScanner\.php$</exclude-pattern>
<exclude-pattern>*/src/PhpFunctionsScanner\.php$</exclude-pattern>
<exclude-pattern>*/src/MapCodeExtractor\.php$</exclude-pattern>
<properties>
<property name="customPropertiesWhitelist" type="array">
<element value="sourcesContent"/>
</property>
</properties>
</rule>

<!-- Allow for JavaScript format examples to be provided in comments.
Expand Down
10 changes: 5 additions & 5 deletions src/JsFunctionsScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,22 @@ final class JsFunctionsScanner extends GettextJsFunctionsScanner {
*
* @var string|false|array
*/
private $extractComments = false;
private $extract_comments = false;

/**
* Enable extracting comments that start with a tag (if $tag is empty all the comments will be extracted).
*
* @param mixed $tag
*/
public function enableCommentsExtraction( $tag = '' ) {
$this->extractComments = $tag;
$this->extract_comments = $tag;
}

/**
* Disable comments extraction.
*/
public function disableCommentsExtraction() {
$this->extractComments = false;
$this->extract_comments = false;
}

/**
Expand All @@ -43,7 +43,7 @@ public function saveGettextFunctions( Translations $translations, array $options

$peast_options = [
'sourceType' => Peast::SOURCE_TYPE_MODULE,
'comments' => false !== $this->extractComments,
'comments' => false !== $this->extract_comments,
'jsx' => true,
];
$ast = Peast::latest( $this->code, $peast_options )->parse();
Expand Down Expand Up @@ -140,7 +140,7 @@ function ( $node ) use ( &$translations, $options, &$all_comments ) {
}

$parsed_comment = ParsedComment::create( $comment->getRawText(), $comment->getLocation()->getStart()->getLine() );
$prefixes = array_filter( (array) $this->extractComments );
$prefixes = array_filter( (array) $this->extract_comments );

if ( $parsed_comment->checkPrefixes( $prefixes ) ) {
$translation->addExtractedComment( $parsed_comment->getComment() );
Expand Down
6 changes: 3 additions & 3 deletions src/MapCodeExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ public static function fromString( $string, Translations $translations, array $o
try {
$options += static::$options;

$mapObject = json_decode( $string );
$map_object = json_decode( $string );

if ( ! isset( $mapObject->sourcesContent ) || ! is_array( $mapObject->sourcesContent ) ) {
if ( ! isset( $map_object->sourcesContent ) || ! is_array( $map_object->sourcesContent ) ) {
return;
}

$string = implode( "\n", $mapObject->sourcesContent );
$string = implode( "\n", $map_object->sourcesContent );

$functions = new JsFunctionsScanner( $string );

Expand Down
4 changes: 2 additions & 2 deletions src/PhpFunctionsScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public function saveGettextFunctions( Translations $translations, array $options
$translation = $translation->addReference( $file, $line );

if ( isset( $function[3] ) ) {
foreach ( $function[3] as $extractedComment ) {
$translation = $translation->addExtractedComment( $extractedComment );
foreach ( $function[3] as $extracted_comment ) {
$translation = $translation->addExtractedComment( $extracted_comment );
}
}
}
Expand Down

0 comments on commit e7a3650

Please sign in to comment.