Skip to content

Commit

Permalink
Merge PSR12 standard on all Helpers. 1f72824
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-unwin committed Oct 1, 2024
1 parent f5a2d1f commit 11f6985
Show file tree
Hide file tree
Showing 79 changed files with 600 additions and 473 deletions.
1 change: 1 addition & 0 deletions app/Helpers/auth_helper.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

# Copyright © 2023 FirstWave. All Rights Reserved.
# SPDX-License-Identifier: AGPL-3.0-or-later

Expand Down
5 changes: 3 additions & 2 deletions app/Helpers/components_helper.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

# Copyright © 2023 FirstWave. All Rights Reserved.
# SPDX-License-Identifier: AGPL-3.0-or-later

Expand Down Expand Up @@ -158,7 +159,7 @@ function version_padded($version)
if (strlen($p) > 10) {
$version_padded .= $p;
} else {
$version_padded .= mb_substr("00000000000000000000".$p, -10);
$version_padded .= mb_substr("00000000000000000000" . $p, -10);
}
}
}
Expand Down Expand Up @@ -823,7 +824,7 @@ function weight($set_by = 'user')
case 'nmap':
$weight = 6000;
break;

default:
$weight = 10000;
break;
Expand Down
88 changes: 55 additions & 33 deletions app/Helpers/device_helper.php

Large diffs are not rendered by default.

225 changes: 102 additions & 123 deletions app/Helpers/diff_helper.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/*
class.Diff.php
Expand All @@ -16,7 +17,6 @@ class.Diff.php
// A class containing functions for computing diffs and formatting the output.
class Diff
{

// define the constants
const UNMODIFIED = 0;
const DELETED = 1;
Expand Down Expand Up @@ -53,13 +53,13 @@ public static function compare($string1, $string2, $compareCharacters = false)

// skip any common prefix
while ($start <= $end1 && $start <= $end2 and $sequence1[$start] == $sequence2[$start]) {
$start ++;
$start++;
}

// skip any common suffix
while ($end1 >= $start and $end2 >= $start and $sequence1[$end1] == $sequence2[$end2]) {
$end1 --;
$end2 --;
$end1--;
$end2--;
}

// compute the table of longest common subsequence lengths
Expand All @@ -70,11 +70,11 @@ public static function compare($string1, $string2, $compareCharacters = false)

// generate the full diff
$diff = array();
for ($index = 0; $index < $start; $index ++) {
for ($index = 0; $index < $start; $index++) {
$diff[] = array($sequence1[$index], self::UNMODIFIED);
}
while (count($partialDiff) > 0) $diff[] = array_pop($partialDiff);
for ($index = $end1 + 1; $index < ($compareCharacters ? strlen($sequence1) : count($sequence1)); $index ++) {
for ($index = $end1 + 1; $index < ($compareCharacters ? strlen($sequence1) : count($sequence1)); $index++) {
$diff[] = array($sequence1[$index], self::UNMODIFIED);
}

Expand Down Expand Up @@ -115,11 +115,11 @@ private static function computeTable($sequence1, $sequence2, $start, $end1, $end
$table = array(array_fill(0, $length2 + 1, 0));

// loop over the rows
for ($index1 = 1; $index1 <= $length1; $index1 ++) {
for ($index1 = 1; $index1 <= $length1; $index1++) {
// create the new row
$table[$index1] = array(0);
// loop over the columns
for ($index2 = 1; $index2 <= $length2; $index2 ++) {
for ($index2 = 1; $index2 <= $length2; $index2++) {
// store the longest common subsequence length
if ($sequence1[$index1 + $start - 1] == $sequence2[$index2 + $start - 1]) {
$table[$index1][$index2] = $table[$index1 - 1][$index2 - 1] + 1;
Expand Down Expand Up @@ -153,16 +153,16 @@ private static function generatePartialDiff($table, $sequence1, $sequence2, $sta
if ($index1 > 0 && $index2 > 0 and $sequence1[$index1 + $start - 1] == $sequence2[$index2 + $start - 1]) {
// update the diff and the indices
$diff[] = array($sequence1[$index1 + $start - 1], self::UNMODIFIED);
$index1 --;
$index2 --;
$index1--;
$index2--;
} elseif ($index2 > 0 and $table[$index1][$index2] == $table[$index1][$index2 - 1]) {
// update the diff and the indices
$diff[] = array($sequence2[$index2 + $start - 1], self::INSERTED);
$index2 --;
$index2--;
} else {
// update the diff and the indices
$diff[] = array($sequence1[$index1 + $start - 1], self::DELETED);
$index1 --;
$index1--;
}
}
// return the diff
Expand Down Expand Up @@ -200,7 +200,7 @@ public static function toString($diff, $separator = "\n")
$string .= $separator;
// return the string
return $string;
}
}

/* Returns a diff as an HTML string, where unmodified lines are contained
* within 'span' elements, deletions are contained within 'del' elements, and
Expand All @@ -210,35 +210,34 @@ public static function toString($diff, $separator = "\n")
* $separator - the separator between lines; this optional parameter defaults
* to '<br>'
*/
public static function toHTML($diff, $separator = '<br>'){

// initialise the HTML
$html = '';

// loop over the lines in the diff
foreach ($diff as $line){

// extend the HTML with the line
switch ($line[1]){
case self::UNMODIFIED : $element = 'span'; break;
case self::DELETED : $element = 'del'; break;
case self::INSERTED : $element = 'ins'; break;
}
$html .=
'<' . $element . '>'
. htmlspecialchars($line[0])
. '</' . $element . '>';
public static function toHTML($diff, $separator = '<br>')
{

// extend the HTML with the separator
$html .= $separator;
// initialise the HTML
$html = '';

// loop over the lines in the diff
foreach ($diff as $line) {
// extend the HTML with the line
switch ($line[1]) {
case self::UNMODIFIED:
$element = 'span';
break;
case self::DELETED:
$element = 'del';
break;
case self::INSERTED:
$element = 'ins';
break;
}
$html .= '<' . $element . '>' . htmlspecialchars($line[0]) . '</' . $element . '>';
// extend the HTML with the separator
$html .= $separator;
}
// return the HTML
return $html;
}

// return the HTML
return $html;

}

/* Returns a diff as an HTML table. The parameters are:
*
* $diff - the diff array
Expand All @@ -247,76 +246,67 @@ public static function toHTML($diff, $separator = '<br>'){
* $separator - the separator between lines; this optional parameter
* defaults to '<br>'
*/
public static function toTable($diff, $indentation = '', $separator = '<br>'){

// initialise the HTML
$html = $indentation . "<table class=\"diff\">\n";

// loop over the lines in the diff
$index = 0;
while ($index < count($diff)){

// determine the line type
switch ($diff[$index][1]){

// display the content on the left and right
case self::UNMODIFIED:
$leftCell =
self::getCellContent(
$diff, $indentation, $separator, $index, self::UNMODIFIED);
$rightCell = $leftCell;
break;
public static function toTable($diff, $indentation = '', $separator = '<br>')
{

// display the deleted on the left and inserted content on the right
case self::DELETED:
$leftCell =
self::getCellContent(
$diff, $indentation, $separator, $index, self::DELETED);
$rightCell =
self::getCellContent(
$diff, $indentation, $separator, $index, self::INSERTED);
break;
// initialise the HTML
$html = $indentation . "<table class=\"diff\">\n";

// display the inserted content on the right
case self::INSERTED:
$leftCell = '';
$rightCell =
self::getCellContent(
$diff, $indentation, $separator, $index, self::INSERTED);
break;
// loop over the lines in the diff
$index = 0;
while ($index < count($diff)) {
// determine the line type
switch ($diff[$index][1]) {
// display the content on the left and right
case self::UNMODIFIED:
$leftCell =
self::getCellContent($diff, $indentation, $separator, $index, self::UNMODIFIED);
$rightCell = $leftCell;
break;

}
// display the deleted on the left and inserted content on the right
case self::DELETED:
$leftCell =
self::getCellContent($diff, $indentation, $separator, $index, self::DELETED);
$rightCell =
self::getCellContent($diff, $indentation, $separator, $index, self::INSERTED);
break;

// extend the HTML with the new row
$html .=
$indentation
. " <tr>\n"
. $indentation
. ' <td class="diff'
. ($leftCell == $rightCell
? 'Unmodified'
: ($leftCell == '' ? 'Blank' : 'Deleted'))
. '">'
. $leftCell
. "</td>\n"
. $indentation
. ' <td class="diff'
. ($leftCell == $rightCell
? 'Unmodified'
: ($rightCell == '' ? 'Blank' : 'Inserted'))
. '">'
. $rightCell
. "</td>\n"
. $indentation
. " </tr>\n";
// display the inserted content on the right
case self::INSERTED:
$leftCell = '';
$rightCell =
self::getCellContent($diff, $indentation, $separator, $index, self::INSERTED);
break;
}

// extend the HTML with the new row
$html .=
$indentation
. " <tr>\n"
. $indentation
. ' <td class="diff'
. ($leftCell == $rightCell
? 'Unmodified'
: ($leftCell == '' ? 'Blank' : 'Deleted'))
. '">'
. $leftCell
. "</td>\n"
. $indentation
. ' <td class="diff'
. ($leftCell == $rightCell
? 'Unmodified'
: ($rightCell == '' ? 'Blank' : 'Inserted'))
. '">'
. $rightCell
. "</td>\n"
. $indentation
. " </tr>\n";
}
// return the HTML
return $html . $indentation . "</table>\n";
}

// return the HTML
return $html . $indentation . "</table>\n";

}

/* Returns the content of the cell, for use in the toTable function. The
* parameters are:
*
Expand All @@ -326,27 +316,16 @@ public static function toTable($diff, $indentation = '', $separator = '<br>'){
* $index - the current index, passes by reference
* $type - the type of line
*/
private static function getCellContent(
$diff, $indentation, $separator, &$index, $type){

// initialise the HTML
$html = '';

// loop over the matching lines, adding them to the HTML
while ($index < count($diff) && $diff[$index][1] == $type){
$html .=
'<span>'
. htmlspecialchars($diff[$index][0])
. '</span>'
. $separator;
$index ++;
private static function getCellContent($diff, $indentation, $separator, &$index, $type)
{
// initialise the HTML
$html = '';
// loop over the matching lines, adding them to the HTML
while ($index < count($diff) && $diff[$index][1] == $type) {
$html .= '<span>' . htmlspecialchars($diff[$index][0]) . '</span>' . $separator;
$index++;
}
// return the HTML
return $html;
}

// return the HTML
return $html;

}

}

?>
Loading

0 comments on commit 11f6985

Please sign in to comment.