Author: Kamil Tekiela (kamil-tekiela)
Committer: GitHub (web-flow)
Pusher: kamil-tekiela
Date: 2022-09-16T12:47:04+01:00

Commit: 
https://github.com/php/web-php/commit/9752e1b90d45ffac26e0c222ec8fcaf4f767e8ba
Raw diff: 
https://github.com/php/web-php/commit/9752e1b90d45ffac26e0c222ec8fcaf4f767e8ba.diff

Introduce UserNote DTO and refactor Sorter (#594)

Changed paths:
  A  src/UserNotes/UserNote.php
  A  tests/UserNotes/Sorter/sort-notes-full.phpt
  M  include/shared-manual.inc
  M  manual/add-note.php
  M  manual/vote-note.php
  M  src/UserNotes/Sorter.php
  M  tests/UserNotes/Sorter/sort-no-notes.phpt
  M  tests/UserNotes/Sorter/sort-single-note-with-no-votes.phpt
  M  tests/UserNotes/Sorter/sort-some-notes.phpt


Diff:

diff --git a/include/shared-manual.inc b/include/shared-manual.inc
index 1461866265..8e4c1c033b 100644
--- a/include/shared-manual.inc
+++ b/include/shared-manual.inc
@@ -23,10 +23,16 @@ $PGI = []; $SIDEBAR_DATA = '';
 // 
=============================================================================
 
 require_once __DIR__ . '/../autoload.php';
-use phpweb\UserNotes\Sorter;
 
-// Print out all user notes for this manual page
-function manual_notes($notes) {
+use phpweb\UserNotes\Sorter;
+use phpweb\UserNotes\UserNote;
+
+/**
+ * Print out all user notes for this manual page
+ *
+ * @param array<string, UserNote> $notes
+ */
+function manual_notes($notes):void {
     // Get needed values
     list($filename) = $GLOBALS['PGI']['this'];
 
@@ -68,20 +74,21 @@ END_USERNOTE_HEADER;
         // If we have notes, print them out
         echo '<div id="allnotes">';
         foreach ($notes as $note) {
-            manual_note_display(
-                $note['xwhen'], $note['user'], $note['note'], $note['id'], 
$note['votes']
-            );
+            manual_note_display($note);
         }
         echo "</div>\n";
         echo "<div class=\"foot\">$addnotesnippet</div>\n";
     }
     echo "</section>";
 }
-// Get user notes from the appropriate text dump
-function manual_notes_load($id)
+
+/**
+ * Get user notes from the appropriate text dump
+ *
+ * @return array<string, UserNote>
+ */
+function manual_notes_load(string $id): array
 {
-    // Initialize values
-    $notes = [];
     $hash = substr(md5($id), 0, 16);
     $notes_file = $_SERVER['DOCUMENT_ROOT'] . "/backend/notes/" .
                   substr($hash, 0, 2) . "/$hash";
@@ -89,22 +96,15 @@ function manual_notes_load($id)
     // Open the note file for reading and get the data (12KB)
     // ..if it exists
     if (!file_exists($notes_file)) {
-        return $notes;
+        return [];
     }
+    $notes = [];
     if ($fp = @fopen($notes_file, "r")) {
         while (!feof($fp)) {
             $line = chop(fgets($fp, 12288));
             if ($line == "") { continue; }
             @list($id, $sect, $rate, $ts, $user, $note, $up, $down) = 
explode("|", $line);
-            $notes[$id] = [
-                "id" => $id,
-                "sect" => $sect,
-                "rate" => $rate,
-                "xwhen" => $ts,
-                "user" => $user,
-                "note" => base64_decode($note, true),
-                "votes" => ["up" => (int)$up, "down" => (int)$down]
-            ];
+            $notes[$id] = new UserNote($id, $sect, $rate, $ts, $user, 
base64_decode($note, true), (int) $up, (int) $down);
         }
         fclose($fp);
     }
@@ -112,24 +112,25 @@ function manual_notes_load($id)
 }
 
 // Print out one user note entry
-function manual_note_display($date, $name, $text, $id, $votes = ['up' => 0, 
'down' => 0], $voteOption = true)
+function manual_note_display(UserNote $note, $voteOption = true)
 {
-    if ($name) {
-        $name = "\n  <strong class=\"user\"><em>" . htmlspecialchars($name) . 
"</em></strong>";
+    if ($note->user) {
+        $name = "\n  <strong class=\"user\"><em>" . 
htmlspecialchars($note->user) . "</em></strong>";
     } else {
         $name = "<strong class=\"user\"><em>Anonymous</em></strong>";
     }
-    $name = ($id ? "\n  <a href=\"#$id\" class=\"name\">$name</a><a 
class=\"genanchor\" href=\"#$id\"> &para;</a>" : "\n  $name");
+    $name = ($note->id ? "\n  <a href=\"#{$note->id}\" 
class=\"name\">$name</a><a class=\"genanchor\" href=\"#{$note->id}\"> 
&para;</a>" : "\n  $name");
 
     // New date style will be relative time
-    $datestr = relTime(new DateTime("@{$date}"));
-    $fdatestr = date("Y-m-d h:i", $date);
-    $text = clean_note($text);
+    $date = new DateTime("@{$note->ts}");
+    $datestr = relTime($date);
+    $fdatestr = $date->format("Y-m-d h:i");
+    $text = clean_note($note->text);
 
     // Calculate note rating by up/down votes
-    $vote = $votes['up'] - $votes['down'];
-    $p = floor(($votes['up'] / (($votes['up'] + $votes['down']) ?: 1)) * 100);
-    $rate = !$p && !($votes['up'] + $votes['down']) ? "no votes..." : "$p% 
like this...";
+    $vote = $note->upvotes - $note->downvotes;
+    $p = floor(($note->upvotes / (($note->upvotes + $note->downvotes) ?: 1)) * 
100);
+    $rate = !$p && !($note->upvotes + $note->downvotes) ? "no votes..." : "$p% 
like this...";
 
     // Vote User Notes Div
     if ($voteOption) {
@@ -140,13 +141,13 @@ function manual_note_display($date, $name, $text, $id, 
$votes = ['up' => 0, 'dow
         $rredir_filename = urlencode($redir_filename);
         $votediv = <<<VOTEDIV
   <div class="votes">
-    <div id="Vu{$id}">
-    <a 
href="/manual/vote-note.php?id={$id}&amp;page={$rredir_filename}&amp;vote=up" 
title="Vote up!" class="usernotes-voteu">up</a>
+    <div id="Vu{$note->id}">
+    <a 
href="/manual/vote-note.php?id={$note->id}&amp;page={$rredir_filename}&amp;vote=up"
 title="Vote up!" class="usernotes-voteu">up</a>
     </div>
-    <div id="Vd{$id}">
-    <a 
href="/manual/vote-note.php?id={$id}&amp;page={$rredir_filename}&amp;vote=down" 
title="Vote down!" class="usernotes-voted">down</a>
+    <div id="Vd{$note->id}">
+    <a 
href="/manual/vote-note.php?id={$note->id}&amp;page={$rredir_filename}&amp;vote=down"
 title="Vote down!" class="usernotes-voted">down</a>
     </div>
-    <div class="tally" id="V{$id}" title="{$rate}">
+    <div class="tally" id="V{$note->id}" title="{$rate}">
     {$vote}
     </div>
   </div>
@@ -156,26 +157,26 @@ VOTEDIV;
     }
 
     // If the viewer is logged in, show admin options
-    if (isset($_COOKIE['IS_DEV']) && $id) {
+    if (isset($_COOKIE['IS_DEV']) && $note->id) {
 
         $admin = "\n  <span class=\"admin\">\n   " .
 
         make_popup_link(
-            'https://main.php.net/manage/user-notes.php?action=edit+' . $id,
+            'https://main.php.net/manage/user-notes.php?action=edit+' . 
$note->id,
             '<img src="/images/notes-e...@2x.png" height="12" width="12" 
alt="edit note">',
             'admin',
             'scrollbars=yes,width=650,height=400'
         ) . "\n   " .
 
         make_popup_link(
-            'https://main.php.net/manage/user-notes.php?action=reject+' . $id,
+            'https://main.php.net/manage/user-notes.php?action=reject+' . 
$note->id,
             '<img src="/images/notes-rej...@2x.png" height="12" width="12" 
alt="reject note">',
             'admin',
             'scrollbars=no,width=300,height=200'
         ) . "\n   " .
 
         make_popup_link(
-            'https://main.php.net/manage/user-notes.php?action=delete+' . $id,
+            'https://main.php.net/manage/user-notes.php?action=delete+' . 
$note->id,
             '<img src="/images/notes-del...@2x.png" height="12" width="12" 
alt="delete note">',
             'admin',
             'scrollbars=no,width=300,height=200'
@@ -187,8 +188,8 @@ VOTEDIV;
 
     echo <<<USER_NOTE_TEXT
 
-  <div class="note" id="$id">{$votediv}{$name}{$admin}<div class="date" 
title="$fdatestr"><strong>{$datestr}</strong></div>
-  <div class="text" id="Hcom{$id}">
+  <div class="note" id="{$note->id}">{$votediv}{$name}{$admin}<div 
class="date" title="$fdatestr"><strong>{$datestr}</strong></div>
+  <div class="text" id="Hcom{$note->id}">
 {$text}
   </div>
  </div>
@@ -295,7 +296,7 @@ function manual_setup($setup) {
     $USERNOTES = manual_notes_load($filename);
     if ($USERNOTES) {
         $note = current($USERNOTES);
-        $timestamps[] = $note["xwhen"];
+        $timestamps[] = $note->ts;
     }
 
     $lastmod = max($timestamps);
diff --git a/manual/add-note.php b/manual/add-note.php
index b965ab969f..a084bc4116 100644
--- a/manual/add-note.php
+++ b/manual/add-note.php
@@ -7,6 +7,8 @@
 include_once __DIR__ . '/../include/shared-manual.inc';
 include __DIR__ . '/spam_challenge.php';
 
+use phpweb\UserNotes\UserNote;
+
 site_header("Add Manual Note", ['css' => 'add-note.css']);
 
 // Copy over "sect" and "redirect" from GET to POST
@@ -140,7 +142,7 @@
     // Print out preview of note
     echo '<p>This is what your entry will look like, roughly:</p>';
     echo '<div id="usernotes">';
-    manual_note_display(time(), $user, $note, false);
+    manual_note_display(new UserNote('', '', '', time(), $user, $note));
     echo '</div><br><br>';
 }
 
diff --git a/manual/vote-note.php b/manual/vote-note.php
index c4031464e2..dae5b8b559 100644
--- a/manual/vote-note.php
+++ b/manual/vote-note.php
@@ -17,10 +17,8 @@
 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
   if (isset($_SERVER['HTTP_X_JSON']) && $_SERVER['HTTP_X_JSON'] == 'On' && 
!empty($_REQUEST['id']) && !empty($_REQUEST['page']) && ($N = 
manual_notes_load($_REQUEST['page'])) && array_key_exists($_REQUEST['id'], $N) 
&& !empty($_REQUEST['vote']) && ($_REQUEST['vote'] === 'up' || 
$_REQUEST['vote'] === 'down')) {
     $response = [];
-    $update = $N[$_REQUEST['id']]['votes']['up'] - 
$N[$_REQUEST['id']]['votes']['down'];
     $hash = substr(md5($_REQUEST['page']), 0, 16);
-    $notes_file = $_SERVER['DOCUMENT_ROOT'] . "/backend/notes/" .
-        substr($hash, 0, 2) . "/$hash";
+    $notes_file = $_SERVER['DOCUMENT_ROOT'] . "/backend/notes/" . 
substr($hash, 0, 2) . "/$hash";
     if (!file_exists($notes_file)) {
       $response["success"] = false;
       $response["msg"] = "Invalid request.";
@@ -60,15 +58,13 @@
       }
       else {
         if ($_REQUEST['vote'] === 'up') {
-          $N[$_REQUEST['id']]['votes']['up']++;
+          $N[$_REQUEST['id']]->upvotes++;
         }
         elseif ($_REQUEST['vote'] === 'down') {
-          $N[$_REQUEST['id']]['votes']['down']++;
+          $N[$_REQUEST['id']]->downvotes++;
         }
-        $update = $N[$_REQUEST['id']]['votes']['up'] - 
$N[$_REQUEST['id']]['votes']['down'];
         $hash = substr(md5($_REQUEST['page']), 0, 16);
-        $notes_file = $_SERVER['DOCUMENT_ROOT'] . "/backend/notes/" .
-            substr($hash, 0, 2) . "/$hash";
+        $notes_file = $_SERVER['DOCUMENT_ROOT'] . "/backend/notes/" . 
substr($hash, 0, 2) . "/$hash";
         if (file_exists($notes_file)) {
           $data = [
               "noteid" => $_REQUEST['id'],
@@ -128,10 +124,7 @@
 <?php
   $backID = htmlspecialchars($_REQUEST['id']);
   $backPAGE = htmlspecialchars($_REQUEST['page']);
-  manual_note_display(
-      $N[$_REQUEST['id']]['xwhen'], $N[$_REQUEST['id']]['user'], 
$N[$_REQUEST['id']]['note'], $N[$_REQUEST['id']]['id'],
-      $N[$_REQUEST['id']]['votes'], false
-  );
+  manual_note_display($N[$_REQUEST['id']], false);
 ?>
  </div>
  <div style="width: 90%; margin: auto;"><p><a href="<?php echo 
"/{$backPAGE}#{$backID}"; ?>">&lt;&lt; Back to user notes page</a></p></div>
@@ -184,10 +177,7 @@
 <?php
   $backID = htmlspecialchars($_REQUEST['id']);
   $backPAGE = htmlspecialchars($_REQUEST['page']);
-  manual_note_display(
-      $N[$_REQUEST['id']]['xwhen'], $N[$_REQUEST['id']]['user'], 
$N[$_REQUEST['id']]['note'], $N[$_REQUEST['id']]['id'],
-      $N[$_REQUEST['id']]['votes'], false
-  );
+  manual_note_display($N[$_REQUEST['id']], false);
 ?>
  </div>
  <div style="width: 90%; margin: auto;"><p><a href="<?php echo 
"/{$backPAGE}#{$backID}"; ?>">&lt;&lt; Back to user notes page</a></p></div>
diff --git a/src/UserNotes/Sorter.php b/src/UserNotes/Sorter.php
index df12b9062d..e19a3d462a 100644
--- a/src/UserNotes/Sorter.php
+++ b/src/UserNotes/Sorter.php
@@ -21,7 +21,10 @@ class Sorter {
 
     private $ageWeight = 2;
 
-    public function sort(array &$notes) {
+    /**
+     * @param array<string, UserNote> $notes
+     */
+    public function sort(array &$notes):void {
         // First we make a pass over the data to get the min and max values
         // for data normalization.
         $this->findMinMaxValues($notes);
@@ -36,80 +39,58 @@ public function sort(array &$notes) {
         $this->ageFactor *= $this->ageWeight;
 
         // Second we loop through to calculate sort priority using the above 
numbers
-        $this->calcSortPriority($notes);
+        $prio = $this->calcSortPriority($notes);
 
         // Third we sort the data.
-        uasort($notes, [$this, 'factorSort']);
+        uasort($notes, function ($a, $b) use ($prio) {
+            return $prio[$b->id] <=> $prio[$a->id];
+        });
     }
 
-    private function calcVotePriority(array $note) {
-        return ($note['score'] - $this->minVote) * $this->voteFactor + .3;
+    private function calcVotePriority(UserNote $note):float {
+        return ($note->upvotes - $note->downvotes - $this->minVote) * 
$this->voteFactor + .3;
     }
 
-    private function calcRatingPriority(array $note) {
-        if ($note['total'] <= 2) {
-            return 0.5;
-        }
-
-        return $note['rating'];
+    private function calcRatingPriority(UserNote $note):float {
+        return $note->upvotes + $note->downvotes <= 2 ? 0.5 : 
$this->calcRating($note);
     }
 
-    private function calcSortPriority(array &$notes) {
-        foreach ($notes as &$note) {
-            $prio = [
-                'vote' => $this->calcVotePriority($note) * $this->voteWeight,
-                'rating' => $this->calcRatingPriority($note) * 
$this->ratingWeight,
-                'age' => ($note['xwhen'] - $this->minAge) * $this->ageFactor
-            ];
-            $note['sort'] = $prio['value'] = array_sum($prio);
-        }
+    private function calcRating(UserNote $note):float {
+        $totalVotes = $note->upvotes + $note->downvotes;
+        return $totalVotes > 0 ? $note->upvotes / $totalVotes : .5;
     }
 
-    /*
-     * Not sure why, but using `$b['sort'] - $a['sort']` does not seem to
-     * work properly.
+    /**
+     * @param array<string, UserNote> $notes
      */
-    private function factorSort($a, $b) {
-        if ($a['sort'] < $b['sort']) {
-            return 1;
-        }
-
-        if ($a['sort'] == $b['sort']) {
-            return 0;
+    private function calcSortPriority(array $notes): array {
+        $prio = [];
+        foreach ($notes as $note) {
+            $prio[$note->id] = ($this->calcVotePriority($note) * 
$this->voteWeight)
+                + ($this->calcRatingPriority($note) * $this->ratingWeight)
+                + (($note->ts - $this->minAge) * $this->ageFactor);
         }
-
-        return -1;
+        return $prio;
     }
 
-    private function findMinMaxValues(array &$notes) {
-        $count = count($notes);
-        if ($count <= 0) {
+    /**
+     * @param array<string, UserNote> $notes
+     */
+    private function findMinMaxValues(array $notes):void {
+        if ($notes === []) {
             return;
         }
-        $note = array_shift($notes);
-        $note['score'] = $net = ($note['votes']['up'] - 
$note['votes']['down']);
-        $note['total'] = $totalVotes = ($note['votes']['up'] + 
$note['votes']['down']);
-        $note['rating'] = $totalVotes > 0
-            ? $note['votes']['up'] / $totalVotes
-            : .5;
 
-        $this->minVote = $this->maxVote = $net;
-        $this->minAge = $this->maxAge = $age = $note['xwhen'];
-
-        $first = $note;
-
-        foreach ($notes as &$note) {
-            $note['score'] = $net = ($note['votes']['up'] - 
$note['votes']['down']);
-            $note['total'] = $totalVotes = ($note['votes']['up'] + 
$note['votes']['down']);
-            $note['rating'] = $totalVotes > 0
-                ? $note['votes']['up'] / $totalVotes
-                : .5;
-            $age = $note['xwhen'];
-            $this->maxVote = max($this->maxVote, $net);
-            $this->minVote = min($this->minVote, $net);
-            $this->maxAge = max($this->maxAge, $age);
-            $this->minAge = min($this->minAge, $age);
+        $first = array_shift($notes);
+
+        $this->minVote = $this->maxVote = ($first->upvotes - 
$first->downvotes);
+        $this->minAge = $this->maxAge = $first->ts;
+
+        foreach ($notes as $note) {
+            $this->maxVote = max($this->maxVote, ($note->upvotes - 
$note->downvotes));
+            $this->minVote = min($this->minVote, ($note->upvotes - 
$note->downvotes));
+            $this->maxAge = max($this->maxAge, $note->ts);
+            $this->minAge = min($this->minAge, $note->ts);
         }
-        array_unshift($notes, $first);
     }
 }
diff --git a/src/UserNotes/UserNote.php b/src/UserNotes/UserNote.php
new file mode 100644
index 0000000000..54987e0d0f
--- /dev/null
+++ b/src/UserNotes/UserNote.php
@@ -0,0 +1,54 @@
+<?php
+
+namespace phpweb\UserNotes;
+
+/**
+ * @readonly
+ * @psalm-immutable
+ */
+final class UserNote
+{
+    /** @var string $id */
+    public $id;
+
+    /** @var string $sect */
+    public $sect;
+
+    /** @var string $rate */
+    public $rate;
+
+    /** @var string $ts */
+    public $ts;
+
+    /** @var string $user */
+    public $user;
+
+    /** @var string $text */
+    public $text;
+
+    /** @var int $upvotes */
+    public $upvotes;
+
+    /** @var int $downvotes */
+    public $downvotes;
+
+    public function __construct(
+        string $id,
+        string $sect,
+        string $rate,
+        string $ts,
+        string $user,
+        string $text,
+        int $upvotes = 0,
+        int $downvotes = 0
+    ) {
+        $this->id = $id;
+        $this->sect = $sect;
+        $this->rate = $rate;
+        $this->ts = $ts;
+        $this->user = $user;
+        $this->text = $text;
+        $this->upvotes = $upvotes;
+        $this->downvotes = $downvotes;
+    }
+}
diff --git a/tests/UserNotes/Sorter/sort-no-notes.phpt 
b/tests/UserNotes/Sorter/sort-no-notes.phpt
index de76c1c4a7..3162da17dd 100644
--- a/tests/UserNotes/Sorter/sort-no-notes.phpt
+++ b/tests/UserNotes/Sorter/sort-no-notes.phpt
@@ -4,21 +4,22 @@ sort no notes
 precision=-1
 --FILE--
 <?php
+
+use phpweb\UserNotes\Sorter;
+use phpweb\UserNotes\UserNote;
+
 require_once __DIR__ . "/../../../autoload.php";
 
 $notes = [];
 
-$sorter = new phpweb\UserNotes\Sorter();
+$sorter = new Sorter();
 $sorter->sort($notes);
-var_dump(array_map(function (array $note): array {
+var_dump(array_map(function (UserNote $note): array {
     return [
-        "ts" => $note["xwhen"],
-        "upvotes" => $note["votes"]["up"],
-        "downvotes" => $note["votes"]["down"],
-        "score" => $note["score"],
-        "total" => $note["total"],
-        "rating" => $note["rating"],
-        "sort" => $note["sort"],
+        "id" => $note->id,
+        "ts" => $note->ts,
+        "upvotes" => $note->upvotes,
+        "downvotes" => $note->downvotes,
     ];
 }, $notes));
 ?>
diff --git a/tests/UserNotes/Sorter/sort-notes-full.phpt 
b/tests/UserNotes/Sorter/sort-notes-full.phpt
new file mode 100644
index 0000000000..31328a6cdf
--- /dev/null
+++ b/tests/UserNotes/Sorter/sort-notes-full.phpt
@@ -0,0 +1,540 @@
+--TEST--
+sort sample notes from strpos notes
+--INI--
+precision=-1
+--FILE--
+<?php
+
+use phpweb\UserNotes\Sorter;
+use phpweb\UserNotes\UserNote;
+
+require_once __DIR__ . "/../../../autoload.php";
+
+$file = file(__DIR__ . "/../../../backend/notes/d7/d7742c269d23ea86");
+$notes = [];
+foreach ($file as $line) {
+    @list($id, $sect, $rate, $ts, $user, $note, $up, $down) = explode("|", 
$line);
+    $notes[$id] = new UserNote($id, $sect, $rate, $ts, $user, 
base64_decode($note, true), (int) $up, (int) $down);
+}
+
+$sorter = new Sorter();
+$sorter->sort($notes);
+var_dump(array_map(function (UserNote $note): array {
+    return [
+        "id" => $note->id,
+        "ts" => $note->ts,
+        "upvotes" => $note->upvotes,
+        "downvotes" => $note->downvotes,
+    ];
+}, $notes));
+?>
+--EXPECT--
+array(46) {
+  [110464]=>
+  array(4) {
+    ["id"]=>
+    string(6) "110464"
+    ["ts"]=>
+    string(10) "1351105628"
+    ["upvotes"]=>
+    int(10)
+    ["downvotes"]=>
+    int(2)
+  }
+  [93816]=>
+  array(4) {
+    ["id"]=>
+    string(5) "93816"
+    ["ts"]=>
+    string(10) "1254343074"
+    ["upvotes"]=>
+    int(4)
+    ["downvotes"]=>
+    int(1)
+  }
+  [92849]=>
+  array(4) {
+    ["id"]=>
+    string(5) "92849"
+    ["ts"]=>
+    string(10) "1249997359"
+    ["upvotes"]=>
+    int(4)
+    ["downvotes"]=>
+    int(1)
+  }
+  [70394]=>
+  array(4) {
+    ["id"]=>
+    string(5) "70394"
+    ["ts"]=>
+    string(10) "1160823504"
+    ["upvotes"]=>
+    int(7)
+    ["downvotes"]=>
+    int(3)
+  }
+  [106407]=>
+  array(4) {
+    ["id"]=>
+    string(6) "106407"
+    ["ts"]=>
+    string(10) "1320695958"
+    ["upvotes"]=>
+    int(5)
+    ["downvotes"]=>
+    int(2)
+  }
+  [87868]=>
+  array(4) {
+    ["id"]=>
+    string(5) "87868"
+    ["ts"]=>
+    string(10) "1230396484"
+    ["upvotes"]=>
+    int(5)
+    ["downvotes"]=>
+    int(2)
+  }
+  [82229]=>
+  array(4) {
+    ["id"]=>
+    string(5) "82229"
+    ["ts"]=>
+    string(10) "1207066654"
+    ["upvotes"]=>
+    int(3)
+    ["downvotes"]=>
+    int(1)
+  }
+  [80363]=>
+  array(4) {
+    ["id"]=>
+    string(5) "80363"
+    ["ts"]=>
+    string(10) "1200066332"
+    ["upvotes"]=>
+    int(3)
+    ["downvotes"]=>
+    int(1)
+  }
+  [75146]=>
+  array(4) {
+    ["id"]=>
+    string(5) "75146"
+    ["ts"]=>
+    string(10) "1179195708"
+    ["upvotes"]=>
+    int(3)
+    ["downvotes"]=>
+    int(1)
+  }
+  [102773]=>
+  array(4) {
+    ["id"]=>
+    string(6) "102773"
+    ["ts"]=>
+    string(10) "1299300266"
+    ["upvotes"]=>
+    int(6)
+    ["downvotes"]=>
+    int(3)
+  }
+  [111422]=>
+  array(4) {
+    ["id"]=>
+    string(6) "111422"
+    ["ts"]=>
+    string(10) "1361224553"
+    ["upvotes"]=>
+    int(4)
+    ["downvotes"]=>
+    int(2)
+  }
+  [94469]=>
+  array(4) {
+    ["id"]=>
+    string(5) "94469"
+    ["ts"]=>
+    string(10) "1257516214"
+    ["upvotes"]=>
+    int(4)
+    ["downvotes"]=>
+    int(2)
+  }
+  [99476]=>
+  array(4) {
+    ["id"]=>
+    string(5) "99476"
+    ["ts"]=>
+    string(10) "1282186230"
+    ["upvotes"]=>
+    int(2)
+    ["downvotes"]=>
+    int(1)
+  }
+  [99332]=>
+  array(4) {
+    ["id"]=>
+    string(5) "99332"
+    ["ts"]=>
+    string(10) "1281503061"
+    ["upvotes"]=>
+    int(2)
+    ["downvotes"]=>
+    int(1)
+  }
+  [96926]=>
+  array(4) {
+    ["id"]=>
+    string(5) "96926"
+    ["ts"]=>
+    string(10) "1269330508"
+    ["upvotes"]=>
+    int(2)
+    ["downvotes"]=>
+    int(1)
+  }
+  [93887]=>
+  array(4) {
+    ["id"]=>
+    string(5) "93887"
+    ["ts"]=>
+    string(10) "1254733546"
+    ["upvotes"]=>
+    int(2)
+    ["downvotes"]=>
+    int(1)
+  }
+  [87061]=>
+  array(4) {
+    ["id"]=>
+    string(5) "87061"
+    ["ts"]=>
+    string(10) "1226944352"
+    ["upvotes"]=>
+    int(2)
+    ["downvotes"]=>
+    int(1)
+  }
+  [85835]=>
+  array(4) {
+    ["id"]=>
+    string(5) "85835"
+    ["ts"]=>
+    string(10) "1221823065"
+    ["upvotes"]=>
+    int(2)
+    ["downvotes"]=>
+    int(1)
+  }
+  [72466]=>
+  array(4) {
+    ["id"]=>
+    string(5) "72466"
+    ["ts"]=>
+    string(10) "1169208947"
+    ["upvotes"]=>
+    int(2)
+    ["downvotes"]=>
+    int(1)
+  }
+  [69927]=>
+  array(4) {
+    ["id"]=>
+    string(5) "69927"
+    ["ts"]=>
+    string(10) "1159299208"
+    ["upvotes"]=>
+    int(2)
+    ["downvotes"]=>
+    int(1)
+  }
+  [41762]=>
+  array(4) {
+    ["id"]=>
+    string(5) "41762"
+    ["ts"]=>
+    string(10) "1082561916"
+    ["upvotes"]=>
+    int(2)
+    ["downvotes"]=>
+    int(1)
+  }
+  [107678]=>
+  array(4) {
+    ["id"]=>
+    string(6) "107678"
+    ["ts"]=>
+    string(10) "1330185500"
+    ["upvotes"]=>
+    int(3)
+    ["downvotes"]=>
+    int(2)
+  }
+  [89788]=>
+  array(4) {
+    ["id"]=>
+    string(5) "89788"
+    ["ts"]=>
+    string(10) "1237801686"
+    ["upvotes"]=>
+    int(3)
+    ["downvotes"]=>
+    int(2)
+  }
+  [74286]=>
+  array(4) {
+    ["id"]=>
+    string(5) "74286"
+    ["ts"]=>
+    string(10) "1175594279"
+    ["upvotes"]=>
+    int(3)
+    ["downvotes"]=>
+    int(2)
+  }
+  [58688]=>
+  array(4) {
+    ["id"]=>
+    string(5) "58688"
+    ["ts"]=>
+    string(10) "1131719326"
+    ["upvotes"]=>
+    int(3)
+    ["downvotes"]=>
+    int(2)
+  }
+  [45088]=>
+  array(4) {
+    ["id"]=>
+    string(5) "45088"
+    ["ts"]=>
+    string(10) "1093449145"
+    ["upvotes"]=>
+    int(3)
+    ["downvotes"]=>
+    int(2)
+  }
+  [49739]=>
+  array(4) {
+    ["id"]=>
+    string(5) "49739"
+    ["ts"]=>
+    string(10) "1107758025"
+    ["upvotes"]=>
+    int(2)
+    ["downvotes"]=>
+    int(0)
+  }
+  [108426]=>
+  array(4) {
+    ["id"]=>
+    string(6) "108426"
+    ["ts"]=>
+    string(10) "1335372412"
+    ["upvotes"]=>
+    int(2)
+    ["downvotes"]=>
+    int(2)
+  }
+  [107240]=>
+  array(4) {
+    ["id"]=>
+    string(6) "107240"
+    ["ts"]=>
+    string(10) "1327390683"
+    ["upvotes"]=>
+    int(2)
+    ["downvotes"]=>
+    int(2)
+  }
+  [105984]=>
+  array(4) {
+    ["id"]=>
+    string(6) "105984"
+    ["ts"]=>
+    string(10) "1317340435"
+    ["upvotes"]=>
+    int(2)
+    ["downvotes"]=>
+    int(2)
+  }
+  [99440]=>
+  array(4) {
+    ["id"]=>
+    string(5) "99440"
+    ["ts"]=>
+    string(10) "1282058725"
+    ["upvotes"]=>
+    int(4)
+    ["downvotes"]=>
+    int(4)
+  }
+  [93566]=>
+  array(4) {
+    ["id"]=>
+    string(5) "93566"
+    ["ts"]=>
+    string(10) "1253094436"
+    ["upvotes"]=>
+    int(2)
+    ["downvotes"]=>
+    int(2)
+  }
+  [88798]=>
+  array(4) {
+    ["id"]=>
+    string(5) "88798"
+    ["ts"]=>
+    string(10) "1234090865"
+    ["upvotes"]=>
+    int(1)
+    ["downvotes"]=>
+    int(1)
+  }
+  [84910]=>
+  array(4) {
+    ["id"]=>
+    string(5) "84910"
+    ["ts"]=>
+    string(10) "1217938595"
+    ["upvotes"]=>
+    int(2)
+    ["downvotes"]=>
+    int(2)
+  }
+  [83914]=>
+  array(4) {
+    ["id"]=>
+    string(5) "83914"
+    ["ts"]=>
+    string(10) "1213760931"
+    ["upvotes"]=>
+    int(1)
+    ["downvotes"]=>
+    int(1)
+  }
+  [78483]=>
+  array(4) {
+    ["id"]=>
+    string(5) "78483"
+    ["ts"]=>
+    string(10) "1192337362"
+    ["upvotes"]=>
+    int(1)
+    ["downvotes"]=>
+    int(1)
+  }
+  [74763]=>
+  array(4) {
+    ["id"]=>
+    string(5) "74763"
+    ["ts"]=>
+    string(10) "1177577911"
+    ["upvotes"]=>
+    int(1)
+    ["downvotes"]=>
+    int(1)
+  }
+  [74432]=>
+  array(4) {
+    ["id"]=>
+    string(5) "74432"
+    ["ts"]=>
+    string(10) "1176269720"
+    ["upvotes"]=>
+    int(1)
+    ["downvotes"]=>
+    int(1)
+  }
+  [47879]=>
+  array(4) {
+    ["id"]=>
+    string(5) "47879"
+    ["ts"]=>
+    string(10) "1102066114"
+    ["upvotes"]=>
+    int(1)
+    ["downvotes"]=>
+    int(1)
+  }
+  [40617]=>
+  array(4) {
+    ["id"]=>
+    string(5) "40617"
+    ["ts"]=>
+    string(10) "1078853206"
+    ["upvotes"]=>
+    int(0)
+    ["downvotes"]=>
+    int(0)
+  }
+  [38375]=>
+  array(4) {
+    ["id"]=>
+    string(5) "38375"
+    ["ts"]=>
+    string(10) "1071743640"
+    ["upvotes"]=>
+    int(1)
+    ["downvotes"]=>
+    int(1)
+  }
+  [106295]=>
+  array(4) {
+    ["id"]=>
+    string(6) "106295"
+    ["ts"]=>
+    string(10) "1319574977"
+    ["upvotes"]=>
+    int(2)
+    ["downvotes"]=>
+    int(3)
+  }
+  [95875]=>
+  array(4) {
+    ["id"]=>
+    string(5) "95875"
+    ["ts"]=>
+    string(10) "1264517173"
+    ["upvotes"]=>
+    int(2)
+    ["downvotes"]=>
+    int(3)
+  }
+  [102336]=>
+  array(4) {
+    ["id"]=>
+    string(6) "102336"
+    ["ts"]=>
+    string(10) "1297217360"
+    ["upvotes"]=>
+    int(1)
+    ["downvotes"]=>
+    int(2)
+  }
+  [93781]=>
+  array(4) {
+    ["id"]=>
+    string(5) "93781"
+    ["ts"]=>
+    string(10) "1254189367"
+    ["upvotes"]=>
+    int(1)
+    ["downvotes"]=>
+    int(2)
+  }
+  [90065]=>
+  array(4) {
+    ["id"]=>
+    string(5) "90065"
+    ["ts"]=>
+    string(10) "1238827503"
+    ["upvotes"]=>
+    int(1)
+    ["downvotes"]=>
+    int(2)
+  }
+}
+
diff --git a/tests/UserNotes/Sorter/sort-single-note-with-no-votes.phpt 
b/tests/UserNotes/Sorter/sort-single-note-with-no-votes.phpt
index 5a1c125948..5e13ef5691 100644
--- a/tests/UserNotes/Sorter/sort-single-note-with-no-votes.phpt
+++ b/tests/UserNotes/Sorter/sort-single-note-with-no-votes.phpt
@@ -4,46 +4,38 @@ sort a single note with no votes
 precision=-1
 --FILE--
 <?php
+
+use phpweb\UserNotes\Sorter;
+use phpweb\UserNotes\UserNote;
+
 require_once __DIR__ . "/../../../autoload.php";
 
 $notes = [
-    [
-        "votes" => ["up" => 0, "down" => 0],
-        "xwhen" => 1613487094,
-    ],
+    new UserNote('1', '', '', '1613487094', '', '', 0, 0),
 ];
 
-$sorter = new phpweb\UserNotes\Sorter();
+$sorter = new Sorter();
 $sorter->sort($notes);
-var_dump(array_map(function (array $note): array {
-    return [
-        "ts" => $note["xwhen"],
-        "upvotes" => $note["votes"]["up"],
-        "downvotes" => $note["votes"]["down"],
-        "score" => $note["score"],
-        "total" => $note["total"],
-        "rating" => $note["rating"],
-        "sort" => $note["sort"],
-    ];
+var_dump(array_map(function (UserNote $note): array {
+  return [
+      "id" => $note->id,
+      "ts" => $note->ts,
+      "upvotes" => $note->upvotes,
+      "downvotes" => $note->downvotes,
+  ];
 }, $notes));
 ?>
 --EXPECT--
 array(1) {
   [0]=>
-  array(7) {
+  array(4) {
+    ["id"]=>
+    string(1) "1"
     ["ts"]=>
-    int(1613487094)
+    string(10) "1613487094"
     ["upvotes"]=>
     int(0)
     ["downvotes"]=>
     int(0)
-    ["score"]=>
-    int(0)
-    ["total"]=>
-    int(0)
-    ["rating"]=>
-    float(0.5)
-    ["sort"]=>
-    float(41.4)
   }
 }
diff --git a/tests/UserNotes/Sorter/sort-some-notes.phpt 
b/tests/UserNotes/Sorter/sort-some-notes.phpt
index 5afa348d9a..108a8fb90a 100644
--- a/tests/UserNotes/Sorter/sort-some-notes.phpt
+++ b/tests/UserNotes/Sorter/sort-some-notes.phpt
@@ -4,106 +4,74 @@ sort some notes
 precision=-1
 --FILE--
 <?php
+
+use phpweb\UserNotes\Sorter;
+use phpweb\UserNotes\UserNote;
+
 require_once __DIR__ . "/../../../autoload.php";
 
 $notes = [
-    [
-        "votes" => ["up" => 0, "down" => 2],
-        "xwhen" => 1613487094,
-    ], [
-        "votes" => ["up" => 0, "down" => 0],
-        "xwhen" => 1508180150,
-    ], [
-        "votes" => ["up" => 14, "down" => 3],
-        "xwhen" => 1508179844,
-    ], [
-        "votes" => ["up" => 14, "down" => 3],
-        "xwhen" => 1508179844,
-    ],
+    new UserNote('1', '', '', '1613487094', '', '', 0, 2),
+    new UserNote('2', '', '', '1508180150', '', '', 0, 0),
+    new UserNote('3', '', '', '1508179844', '', '', 14, 3),
+    new UserNote('4', '', '', '1508179844', '', '', 14, 3),
 ];
 
-$sorter = new phpweb\UserNotes\Sorter();
+$sorter = new Sorter();
 $sorter->sort($notes);
-var_dump(array_map(function (array $note): array {
+var_dump(array_map(function (UserNote $note): array {
     return [
-        "ts" => $note["xwhen"],
-        "upvotes" => $note["votes"]["up"],
-        "downvotes" => $note["votes"]["down"],
-        "score" => $note["score"],
-        "total" => $note["total"],
-        "rating" => $note["rating"],
-        "sort" => $note["sort"],
+        "id" => $note->id,
+        "ts" => $note->ts,
+        "upvotes" => $note->upvotes,
+        "downvotes" => $note->downvotes,
     ];
 }, $notes));
 ?>
 --EXPECT--
 array(4) {
   [2]=>
-  array(7) {
+  array(4) {
+    ["id"]=>
+    string(1) "3"
     ["ts"]=>
-    int(1508179844)
+    string(10) "1508179844"
     ["upvotes"]=>
     int(14)
     ["downvotes"]=>
     int(3)
-    ["score"]=>
-    int(11)
-    ["total"]=>
-    int(17)
-    ["rating"]=>
-    float(0.8235294117647058)
-    ["sort"]=>
-    float(87.41176470588235)
   }
   [3]=>
-  array(7) {
+  array(4) {
+    ["id"]=>
+    string(1) "4"
     ["ts"]=>
-    int(1508179844)
+    string(10) "1508179844"
     ["upvotes"]=>
     int(14)
     ["downvotes"]=>
     int(3)
-    ["score"]=>
-    int(11)
-    ["total"]=>
-    int(17)
-    ["rating"]=>
-    float(0.8235294117647058)
-    ["sort"]=>
-    float(87.41176470588235)
   }
   [1]=>
-  array(7) {
+  array(4) {
+    ["id"]=>
+    string(1) "2"
     ["ts"]=>
-    int(1508180150)
+    string(10) "1508180150"
     ["upvotes"]=>
     int(0)
     ["downvotes"]=>
     int(0)
-    ["score"]=>
-    int(0)
-    ["total"]=>
-    int(0)
-    ["rating"]=>
-    float(0.5)
-    ["sort"]=>
-    float(45.49231350387337)
   }
   [0]=>
-  array(7) {
+  array(4) {
+    ["id"]=>
+    string(1) "1"
     ["ts"]=>
-    int(1613487094)
+    string(10) "1613487094"
     ["upvotes"]=>
     int(0)
     ["downvotes"]=>
     int(2)
-    ["score"]=>
-    int(-2)
-    ["total"]=>
-    int(2)
-    ["rating"]=>
-    int(0)
-    ["sort"]=>
-    float(43.4)
   }
 }

-- 
PHP Webmaster List Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to