Skip to content

Commit

Permalink
fix linter errors
Browse files Browse the repository at this point in the history
  • Loading branch information
NovemLinguae committed May 9, 2024
1 parent 3fbd38f commit 5175ceb
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 139 deletions.
50 changes: 25 additions & 25 deletions Task7RFACount/src/EchoHelper.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
<?php

class EchoHelper {
public function __construct(Helper $h) {
public function __construct( Helper $h ) {
$this->h = $h;
}

/** Echo to the browser instantly, without a delay. Also, convert to HTML so we don't need to use Content-Type:text/plain, which displays HTML error messages incorrectly. Also, put a border around each message. */
public function echoAndFlush(string $str, string $type): void {
public function echoAndFlush( string $str, string $type ): void {
global $SHORT_WIKICODE_IN_CONSOLE, $CHARACTERS_TO_ECHO, $SHOW_API_READS;

if ( $type == 'api_read' && ! $SHOW_API_READS ) {
if ( $type == 'api_read' && !$SHOW_API_READS ) {
return;
}

switch ( $type ) {
case 'api_read':
$color = 'lightgray';
$description = 'API read';
$str = htmlentities($str);
$str = nl2br($str);
$str = $this->h->nbsp($str);
$str = htmlentities( $str );
$str = nl2br( $str );
$str = $this->h->nbsp( $str );
break;
case 'api_write':
$color = '#FFCC66';
Expand All @@ -28,23 +28,23 @@ public function echoAndFlush(string $str, string $type): void {
case 'variable':
$color = 'lawngreen';
$description = 'Variable';
$str = htmlentities($str);
$str = nl2br($str);
$str = $this->h->nbsp($str);
$str = htmlentities( $str );
$str = nl2br( $str );
$str = $this->h->nbsp( $str );
break;
case 'error':
$color = 'salmon';
$description = 'Error';
$str = htmlentities($str);
$str = nl2br($str);
$str = $this->h->nbsp($str);
$str = htmlentities( $str );
$str = nl2br( $str );
$str = $this->h->nbsp( $str );
break;
case 'newtopic':
$color = 'yellow';
$description = 'Starting new topic';
$str = htmlentities($str);
$str = nl2br($str);
$str = $this->h->nbsp($str);
$str = htmlentities( $str );
$str = nl2br( $str );
$str = $this->h->nbsp( $str );
break;
case 'message':
$color = 'lightblue';
Expand All @@ -53,36 +53,36 @@ public function echoAndFlush(string $str, string $type): void {
case 'complete':
$color = 'yellow';
$description = 'Run complete';
$str = htmlentities($str);
$str = nl2br($str);
$str = $this->h->nbsp($str);
$str = htmlentities( $str );
$str = nl2br( $str );
$str = $this->h->nbsp( $str );
break;
}

if ( $SHORT_WIKICODE_IN_CONSOLE ) {
$str2 = substr($str, 0, $CHARACTERS_TO_ECHO);
$str2 = substr( $str, 0, $CHARACTERS_TO_ECHO );
if ( $str2 != $str ) {
$str2 .= "\n\n[...]";
}
$str = $str2;
}

$str = '<div style="border: 2px solid black; margin-bottom: 1em; background-color: '.$color.';"><b><u>' . $description . '</u></b>:<br />' . $str . '</div>';
$str = '<div style="border: 2px solid black; margin-bottom: 1em; background-color: ' . $color . ';"><b><u>' . $description . '</u></b>:<br />' . $str . '</div>';

echo $str;
flush();
if (ob_get_level() > 0) {
if ( ob_get_level() > 0 ) {
ob_flush();
}
}

public function html_var_export($arr, $type) {
return $this->echoAndFlush(var_export($arr, true), $type);
public function html_var_export( $arr, $type ) {
return $this->echoAndFlush( var_export( $arr, true ), $type );
}

/** Don't forget to use continue; after this is called, to continue execution of the outer loop */
public function logError($error_message) {
$this->echoAndFlush('ERROR, SKIPPING THE REST OF THIS TOPIC: ' . $error_message, 'error');
public function logError( $error_message ) {
$this->echoAndFlush( 'ERROR, SKIPPING THE REST OF THIS TOPIC: ' . $error_message, 'error' );

// TODO: replace {{User:NovemBot/Promote}} with "Unable to promote due to $error_message. ~~~~"
}
Expand Down
66 changes: 34 additions & 32 deletions Task7RFACount/src/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@

class Helper {
/** Grabs template Wikicode of first instance encountered of that template. Case insensitive. Returns null if no template found. */
public function sliceFirstTemplateFound(string $wikicode, string $templateName) {
$starting_position = strpos(strtolower($wikicode), "{{" . strtolower($templateName));
public function sliceFirstTemplateFound( string $wikicode, string $templateName ) {
$starting_position = strpos( strtolower( $wikicode ), "{{" . strtolower( $templateName ) );
if ( $starting_position === false ) {
return null;
}
$counter = 0;
$length = strlen($wikicode);
$length = strlen( $wikicode );
for ( $i = $starting_position + 2; $i < $length; $i++ ) {
$next_two = substr($wikicode, $i, 2);
$next_two = substr( $wikicode, $i, 2 );
if ( $next_two == "{{" ) {
$counter++;
continue;
} elseif ( $next_two == "}}" ) {
if ( $counter == 0 ) {
return substr($wikicode, $starting_position, $i - $starting_position + 2);
return substr( $wikicode, $starting_position, $i - $starting_position + 2 );
} else {
$counter--;
continue;
Expand All @@ -27,13 +27,13 @@ public function sliceFirstTemplateFound(string $wikicode, string $templateName)
}

/** Used by echoAndFlush() */
public function nbsp($string) {
$string = preg_replace('/\t/', '&nbsp;&nbsp;&nbsp;&nbsp;', $string);
public function nbsp( $string ) {
$string = preg_replace( '/\t/', '&nbsp;&nbsp;&nbsp;&nbsp;', $string );

// replace more than 1 space in a row with &nbsp;
$string = preg_replace('/ /m', '&nbsp;&nbsp;', $string);
$string = preg_replace('/ &nbsp;/m', '&nbsp;&nbsp;', $string);
$string = preg_replace('/&nbsp; /m', '&nbsp;&nbsp;', $string);
$string = preg_replace( '/ /m', '&nbsp;&nbsp;', $string );
$string = preg_replace( '/ &nbsp;/m', '&nbsp;&nbsp;', $string );
$string = preg_replace( '/&nbsp; /m', '&nbsp;&nbsp;', $string );

if ( $string == ' ' ) {
$string = '&nbsp;';
Expand All @@ -43,69 +43,71 @@ public function nbsp($string) {
}

/** Similar to preg_match, except always returns the contents of the first match. No need to deal with a $matches[1] variable. */
public function preg_first_match($regex, $haystack, $throwErrorIfNoMatch = false) {
preg_match($regex, $haystack, $matches);
if ( isset($matches[1]) ) {
public function preg_first_match( $regex, $haystack, $throwErrorIfNoMatch = false ) {
preg_match( $regex, $haystack, $matches );
if ( isset( $matches[1] ) ) {
return $matches[1];
}
if ( $throwErrorIfNoMatch ) {
throw new Exception("RegEx match not found in the following RegEx: $regex");
throw new Exception( "RegEx match not found in the following RegEx: $regex" );
} else {
return '';
}
}

public function deleteLastLineOfString($string) {
return substr($string, 0, strrpos($string, "\n"));
public function deleteLastLineOfString( $string ) {
return substr( $string, 0, strrpos( $string, "\n" ) );
}

public function insertStringIntoStringAtPosition($oldstr, $str_to_insert, $pos) {
return substr_replace($oldstr, $str_to_insert, $pos, 0);
public function insertStringIntoStringAtPosition( $oldstr, $str_to_insert, $pos ) {
return substr_replace( $oldstr, $str_to_insert, $pos, 0 );
}

public function insertCodeAtEndOfFirstTemplate($wikicode, $templateNameRegExNoDelimiters, $codeToInsert) {
public function insertCodeAtEndOfFirstTemplate( $wikicode, $templateNameRegExNoDelimiters, $codeToInsert ) {
// This uses RegEx recursion (?2) to handle nested braces {{ }}.
// https://regex101.com/r/GmiY1z/1
return preg_replace('/({{' . $templateNameRegExNoDelimiters . '\s*\|?((?:(?!{{|}}).|{{(?2)}})*))(}})/is', "$1\n$codeToInsert\n$3", $wikicode, 1);
return preg_replace( '/({{' . $templateNameRegExNoDelimiters . '\s*\|?((?:(?!{{|}}).|{{(?2)}})*))(}})/is', "$1\n$codeToInsert\n$3", $wikicode, 1 );
}

public function preg_position($regex, $haystack) {
preg_match($regex, $haystack, $matches, PREG_OFFSET_CAPTURE);
public function preg_position( $regex, $haystack ) {
preg_match( $regex, $haystack, $matches, PREG_OFFSET_CAPTURE );
return $matches[0][1] ?? false;
}

public function deleteArrayValue(array $array, $valueToDelete) {
$array = array_diff($array, [$valueToDelete]); // delete value
$array = array_values($array); // reindex (fix keys), 0 to whatever
public function deleteArrayValue( array $array, $valueToDelete ) {
// delete value
$array = array_diff( $array, [ $valueToDelete ] );
// reindex (fix keys), 0 to whatever
$array = array_values( $array );
return $array;
}

public function deleteMiddleOfString($string, $deleteStartPosition, $deleteEndPosition) {
public function deleteMiddleOfString( $string, $deleteStartPosition, $deleteEndPosition ) {
$pos = $deleteStartPosition;
$len = $deleteEndPosition - $deleteStartPosition;

$part1 = substr($string, 0, $deleteStartPosition);
$part2 = substr($string, $deleteEndPosition);
$part1 = substr( $string, 0, $deleteStartPosition );
$part2 = substr( $string, $deleteEndPosition );

$final_str = $part1 . $part2;
return $final_str;
}

public function deleteArrayValuesBeginningWith(array $array, string $prefix) {
public function deleteArrayValuesBeginningWith( array $array, string $prefix ) {
if ( $prefix === '' ) {
throw new InvalidArgumentException();
}

$array2 = [];
foreach ( $array as $key => $value ) {
if ( ! $this->str_starts_with($value, $prefix) ) {
if ( !$this->str_starts_with( $value, $prefix ) ) {
$array2[$key] = $value;
}
}
return $array2;
}

public function str_starts_with($haystack, $needle) {
return strpos($haystack, $needle) === 0;
public function str_starts_with( $haystack, $needle ) {
return strpos( $haystack, $needle ) === 0;
}
}
12 changes: 6 additions & 6 deletions Task7RFACount/src/RFACount.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

function countRFAs($wikicode) {
preg_match_all('/\{\{Wikipedia:Requests for adminship\/[^\}]+\}\}/i', $wikicode, $matches);
function countRFAs( $wikicode ) {
preg_match_all( '/\{\{Wikipedia:Requests for adminship\/[^\}]+\}\}/i', $wikicode, $matches );

// don't count {{Wikipedia:Requests for adminship/Header}} and {{Wikipedia:Requests for adminship/bureaucratship}}
$count = count($matches[0]) - 2;
$count = count( $matches[0] ) - 2;

// if we get an impossible count, just return zero
if ( $count < 0 ) {
Expand All @@ -14,10 +14,10 @@ function countRFAs($wikicode) {
return $count;
}

function countRFBs($wikicode) {
preg_match_all('/\{\{Wikipedia:Requests for bureaucratship\/[^\}]+\}\}/i', $wikicode, $matches);
function countRFBs( $wikicode ) {
preg_match_all( '/\{\{Wikipedia:Requests for bureaucratship\/[^\}]+\}\}/i', $wikicode, $matches );

$count = count($matches[0]);
$count = count( $matches[0] );

return $count;
}
Loading

0 comments on commit 5175ceb

Please sign in to comment.