Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #393: NPE prevents reviewing further findings #402

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 95 additions & 35 deletions mubench.reviewsite/src/Controllers/ReviewsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use MuBench\ReviewSite\Models\Misuse;
use MuBench\ReviewSite\Models\Review;
use MuBench\ReviewSite\Models\Reviewer;
use MuBench\ReviewSite\Models\ReviewState;
use MuBench\ReviewSite\Models\Run;
use MuBench\ReviewSite\Models\Tag;
use MuBench\ReviewSite\Models\Violation;
Expand Down Expand Up @@ -39,12 +38,10 @@ public function getReview(Request $request, Response $response, array $args)
$resolution_reviewer = Reviewer::where(['name' => 'resolution'])->first();
$is_reviewer = ($this->user && $reviewer && $this->user->id == $reviewer->id) || ($reviewer && $reviewer->id == $resolution_reviewer->id);

$runs = RunsController::getRuns($detector, $experiment, $ex2_review_size);

$all_misuses = $this->collectAllMisuses($runs);
$runs = Run::of($detector)->in($experiment)->orderBy('project_muid')->orderBy('version_muid')->get();

list($previous_misuse, $next_misuse, $next_reviewable_misuse, $misuse) =
$this->determineNavigationTargets($all_misuses, $project_muid, $version_muid, $misuse_muid, $reviewer);
$this->determineNavigationTargets($runs, $experiment, $project_muid, $version_muid, $misuse_muid, $reviewer, $ex2_review_size);

$all_violations = Violation::all();
$all_tags = Tag::all()->sortBy('name');
Expand Down Expand Up @@ -152,52 +149,115 @@ public function updateOrCreateReview($misuse_id, $reviewer_id, $comment, $findin
}
}

private function collectAllMisuses($runs)
{
$all_misuses = new Collection;
foreach ($runs as $run) {
$all_misuses = $all_misuses->merge($run->misuses);
}
return $all_misuses;
}

function determineNavigationTargets(Collection $all_misuses, $project_muid, $version_muid, $misuse_muid, $reviewer)
function determineNavigationTargets(Collection $runs, $experiment, $project_muid, $version_muid, $misuse_muid, $reviewer, $ex2_review_size)
{
$previous_misuse = $all_misuses->last();
$previous_misuse = NULL;
$misuse = NULL;
$next_misuse = NULL;
$next_reviewable_misuse = NULL;
$last_run = $runs->last();

foreach ($all_misuses as $current_misuse) { /** @var Misuse $current_misuse */
if (!$misuse && $current_misuse->getProject() === $project_muid
&& $current_misuse->getVersion() === $version_muid
&& $current_misuse->misuse_muid === $misuse_muid) {
$misuse = $current_misuse;
} else {
if ($misuse && !$next_misuse) {
$next_misuse = $current_misuse;
foreach ($runs as $run) {
if ($misuse && !$next_misuse) {
$next_misuse = $run->getMisuses($experiment, $ex2_review_size)->first();
}
if (!$misuse && $run->project_muid == $project_muid && $run->version_muid == $version_muid) {
$this->findMisuse($run->getMisuses($experiment, $ex2_review_size), $misuse_muid, $reviewer, $previous_misuse, $misuse, $next_misuse, $next_reviewable_misuse);
if($misuse && !$previous_misuse){
$previous_misuse = $last_run->getMisuses($experiment, $ex2_review_size)->last();
}
$is_current_misuse_reviewable = $current_misuse->getReviewState() === ReviewState::NEEDS_REVIEW
&& !$current_misuse->hasReviewed($reviewer);
$first_reviewable_misuse = !$next_reviewable_misuse && $is_current_misuse_reviewable;
$first_reviewable_misuse_after = $misuse && $is_current_misuse_reviewable;
if ($first_reviewable_misuse || $first_reviewable_misuse_after) {
$next_reviewable_misuse = $current_misuse;
if ($misuse) {
break;
if(!$misuse){
// search for misuse out of review scope
foreach($run->misuses as $current_misuse){
if($current_misuse->misuse_muid == $misuse_muid){
$misuse = $current_misuse;
break;
}
$previous_misuse = $current_misuse;
}
}
} elseif ($misuse && !$next_reviewable_misuse) {
$next_reviewable_misuse = $this->findNextReviewableMisuse($run->getMisuses($experiment, $ex2_review_size), $reviewer);
}

if ($next_misuse && $next_reviewable_misuse) {
break;
}
$last_run = $run;
}
if ($misuse && !$next_misuse) {
$next_misuse = $runs->first()->getMisuses($experiment, $ex2_review_size)->first();
}
if(!$next_reviewable_misuse){
$next_reviewable_misuse = $this->findNextReviewableBeforeMisuse($runs, $experiment, $project_muid, $version_muid, $misuse, $reviewer, $ex2_review_size);
}
if(!$previous_misuse){
$previous_misuse = $this->findPreviousMisuse($runs, $misuse);
}
return array($previous_misuse, $next_misuse, $next_reviewable_misuse, $misuse);
}

private function findMisuse(Collection $misuses, $misuse_muid, $reviewer, &$previous_misuse, &$misuse, &$next_misuse, &$next_reviewable_misuse)
{
foreach ($misuses as $current_misuse) {
if ($misuse && !$next_misuse) {
$next_misuse = $current_misuse;
}
if ($misuse && !$current_misuse->hasReviewed($reviewer) && !$current_misuse->hasSufficientReviews()) {
$next_reviewable_misuse = $current_misuse;
break;
}
if (!$misuse && $current_misuse->misuse_muid == $misuse_muid) {
$misuse = $current_misuse;

}
if (!$misuse) {
$previous_misuse = $current_misuse;
}
}
}

if (!$next_misuse) {
$next_misuse = $all_misuses->first();
private function findPreviousMisuse(Collection $runs, $misuse)
{
$previous_misuse = NULL;
$idx = sizeof($runs) - 1;
while(!$previous_misuse && $idx >= 0){
$run = $runs->get($idx);
$previous_misuse = $run->misuses->last();
if($previous_misuse && ($misuse->id == $previous_misuse->id)){
$previous_misuse = NULL;
}
$idx--;
}
return $previous_misuse;
}

return array($previous_misuse, $next_misuse, $next_reviewable_misuse, $misuse);
private function findNextReviewableMisuse(Collection $misuses, $reviewer)
{
$next_reviewable_misuse = NULL;
foreach ($misuses as $current_misuse) {
if (!$current_misuse->hasReviewed($reviewer) && !$current_misuse->hasSufficientReviews()) {
$next_reviewable_misuse = $current_misuse;
break;
}
}
return $next_reviewable_misuse;
}

private function findNextReviewableBeforeMisuse(Collection $runs, $experiment, $project_muid, $version_muid, $misuse, $reviewer, $ex2_review_size)
{
$next_reviewable_misuse = NULL;
foreach($runs as $run){
$next_reviewable_misuse = $this->findNextReviewableMisuse($run->getMisuses($experiment, $ex2_review_size), $reviewer);

if($run->project_muid == $project_muid && $run->version_muid == $version_muid){
break;
}
}
if($next_reviewable_misuse && $misuse->id == $next_reviewable_misuse->id) {
return NULL;
}
return $next_reviewable_misuse;
}

}
28 changes: 1 addition & 27 deletions mubench.reviewsite/src/Controllers/RunsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,33 +229,7 @@ static function getRuns($detector, $experiment, $max_reviews)
$runs = Run::of($detector)->in($experiment)->orderBy('project_muid')->orderBy('version_muid')->get();

foreach($runs as $run){
$conclusive_reviews = 0;
$filtered_misuses = new Collection;
$misuses = $run->misuses->sortBy('misuse_muid', SORT_NATURAL);
if($experiment->id === 1) {
foreach($misuses as $misuse){
if($misuse->metadata && !$misuse->metadata->correct_usages->isEmpty()){
$filtered_misuses->add($misuse);
}
}
} else if($experiment->id === 2) {
foreach ($misuses as $misuse) {
if ($conclusive_reviews >= $max_reviews) {
break;
}
$filtered_misuses->add($misuse);
if ($misuse->hasConclusiveReviewState() || (!$misuse->hasSufficientReviews() && !$misuse->hasInconclusiveReview())) {
$conclusive_reviews++;
}
}
} else {
foreach($misuses as $misuse){
if($misuse->metadata){
$filtered_misuses->add($misuse);
}
}
}
$run->misuses = $filtered_misuses;
$run->misuses = $run->getMisuses($experiment, $max_reviews);
}

return $runs;
Expand Down
32 changes: 32 additions & 0 deletions mubench.reviewsite/src/Models/Run.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace MuBench\ReviewSite\Models;

use Illuminate\Database\Eloquent\Collection;

class Run extends DetectorDependent
{
Expand All @@ -14,4 +15,35 @@ public function misuses()
{
return $this->hasMany(Misuse::class, 'run_id', 'id')->where('detector_id', $this->detector->id);
}

public function getMisuses($experiment, $max_review_size)
{
$filtered_misuses = new Collection;
$misuses = $this->misuses->sortBy('misuse_muid', SORT_NATURAL);
if($experiment->id === 1) {
foreach($misuses as $misuse){
if($misuse->metadata && !$misuse->metadata->correct_usages->isEmpty()){
$filtered_misuses->add($misuse);
}
}
} else if($experiment->id === 2) {
$conclusive_reviews = 0;
foreach ($misuses as $misuse) {
if ($conclusive_reviews >= $max_review_size) {
break;
}
$filtered_misuses->add($misuse);
if ($misuse->hasConclusiveReviewState() || (!$misuse->hasSufficientReviews() && !$misuse->hasInconclusiveReview())) {
$conclusive_reviews++;
}
}
} else {
foreach($misuses as $misuse){
if($misuse->metadata){
$filtered_misuses->add($misuse);
}
}
}
return $filtered_misuses;
}
}
2 changes: 1 addition & 1 deletion mubench.reviewsite/templates/detector.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ $columns_to_ignore = array(
$button_classes = "button" . (!$misuse->hasPotentialHits() || ($user && $misuse->hasSufficientReviews() || $misuse->hasReviewed($user)) ? " no-hit" : "")
?>
<?php if (!$user): ?>
<a href="<?= $pathFor('view', array('experiment_id' => $experiment->id, 'detector_muid' => $detectorPathId($detector), 'project_muid' => $project, 'version_muid' => $version, 'misuse_muid' => $misuse->misuse_muid)); ?>" class="<?= $button_classes ?>">view</a>
<a href="<?= $pathFor('view', array('experiment_id' => $experiment->id, 'detector_muid' => $detectorPathId($detector), 'project_muid' => $project, 'version_muid' => $version, 'misuse_muid' => $misuse->misuse_muid)); ?><?= $experiment->id === 2 ? "?ex2_review_size=$ex2_review_size" : "" ?>" class="<?= $button_classes ?>">view</a>
<?php elseif ($misuse->hasReviewed($user)): ?>
<a href="<?= $pathFor('review', array('experiment_id' => $experiment->id, 'detector_muid' => $detectorPathId($detector), 'project_muid' => $project, 'version_muid' => $version, 'misuse_muid' => $misuse->misuse_muid, 'reviewer_name' => $reviewerPathId($user))) ?><?= $origin_param ?><?= $experiment->id === 2 ? "&ex2_review_size=$ex2_review_size" : "" ?>" class="<?= $button_classes ?>">edit
<?php $your_decision = $misuse->getReview($user)->getDecision() ?>
Expand Down
81 changes: 51 additions & 30 deletions mubench.reviewsite/tests/ReviewControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

use MuBench\ReviewSite\Controllers\RunsController;
use MuBench\ReviewSite\Models\Decision;
use MuBench\ReviewSite\Models\Detector;
use MuBench\ReviewSite\Models\Experiment;
use MuBench\ReviewSite\Models\Misuse;
use MuBench\ReviewSite\Models\Reviewer;
use MuBench\ReviewSite\Models\Run;
use MuBench\ReviewSite\Models\Violation;

class ReviewControllerTest extends SlimTestCase
Expand Down Expand Up @@ -70,13 +73,15 @@ function test_stores_violations()

function test_determine_next_misuse_current_and_next_are_reviewed()
{
list($misuse1, $misuse2, $misuse3) = $this->createRunWithThreeMisuses();
$this->createRunWithMisuses("-p-", "-v-", 3);
list($misuse1, $misuse2, $misuse3) = Misuse::all()->all();
$runs = Run::of(Detector::find('test-detector'))->in(Experiment::find(2))->get();

$this->createConclusiveReviewState($misuse1);
$this->createConclusiveReviewState($misuse2);

// current misuse = 1, reviewer = 1
list($previous_misuse, $next_misuse, $next_reviewable_misuse, $misuse) = $this->reviewControllerHelper->reviewController->determineNavigationTargets(Misuse::all(), '-p-', '-v-', $misuse1->misuse_muid, $this->reviewer1);
list($previous_misuse, $next_misuse, $next_reviewable_misuse, $misuse) = $this->reviewControllerHelper->reviewController->determineNavigationTargets($runs, Experiment::find(2), '-p-', '-v-', $misuse1->misuse_muid, $this->reviewer1, 30);

self::assertEquals($misuse3->misuse_muid, $previous_misuse->misuse_muid);
self::assertEquals($misuse2->misuse_muid, $next_misuse->misuse_muid);
Expand All @@ -86,13 +91,15 @@ function test_determine_next_misuse_current_and_next_are_reviewed()

function test_determine_next_misuse_current_is_only_reviewable()
{
list($misuse1, $misuse2, $misuse3) = $this->createRunWithThreeMisuses();
$this->createRunWithMisuses("-p-", "-v-", 3);
list($misuse1, $misuse2, $misuse3) = Misuse::all()->all();
$runs = Run::of(Detector::find('test-detector'))->in(Experiment::find(2))->get();

$this->createConclusiveReviewState($misuse1);
$this->createConclusiveReviewState($misuse2);

// current misuse = 3, reviewer = 1
list($previous_misuse, $next_misuse, $next_reviewable_misuse, $misuse) = $this->reviewControllerHelper->reviewController->determineNavigationTargets(Misuse::all(), '-p-', '-v-', $misuse3->misuse_muid, $this->reviewer1);
list($previous_misuse, $next_misuse, $next_reviewable_misuse, $misuse) = $this->reviewControllerHelper->reviewController->determineNavigationTargets($runs, Experiment::find(2), '-p-', '-v-', $misuse3->misuse_muid, $this->reviewer1, 30);

self::assertEquals($misuse2->misuse_muid, $previous_misuse->misuse_muid);
self::assertEquals($misuse1->misuse_muid, $next_misuse->misuse_muid);
Expand All @@ -102,47 +109,61 @@ function test_determine_next_misuse_current_is_only_reviewable()

function test_determine_next_misuse_not_fully_reviewed()
{
list($misuse1, $misuse2, $misuse3) = $this->createRunWithThreeMisuses();
$this->createRunWithMisuses("-p-", "-v-", 3);
list($misuse1, $misuse2, $misuse3) = Misuse::all()->all();
$runs = Run::of(Detector::find('test-detector'))->in(Experiment::find(2))->get();

$this->reviewControllerHelper->createReview($misuse1, $this->reviewer2, "Yes");
$this->reviewControllerHelper->createReview($misuse2, $this->reviewer1, "Yes");
$this->reviewControllerHelper->createReview($misuse3, $this->reviewer2, "Yes");

// current misuse = 1, reviewer = 1
list($previous_misuse, $next_misuse, $next_reviewable_misuse, $misuse) = $this->reviewControllerHelper->reviewController->determineNavigationTargets(Misuse::all(), '-p-', '-v-', $misuse1->misuse_muid, $this->reviewer1);
list($previous_misuse, $next_misuse, $next_reviewable_misuse, $misuse) = $this->reviewControllerHelper->reviewController->determineNavigationTargets($runs, Experiment::find(2), '-p-', '-v-', $misuse1->misuse_muid, $this->reviewer1, 30);

self::assertEquals($misuse3->misuse_muid, $previous_misuse->misuse_muid);
self::assertEquals($misuse2->misuse_muid, $next_misuse->misuse_muid);
self::assertEquals($misuse3->misuse_muid, $next_reviewable_misuse->misuse_muid);
self::assertEquals($misuse1->misuse_muid, $misuse->misuse_muid);
}

private function createRunWithThreeMisuses()
function test_determine_nav_multiple_runs()
{
$runsController = new RunsController($this->container);
$runsController->addRun(1, 'test-detector', '-p-', '-v-',
[
"result" => "success",
"runtime" => 42.1,
"number_of_findings" => 23,
"timestamp" => 12,
"potential_hits" => [
[
"misuse" => "0",
"rank" => 0,
],
[
"misuse" => "1",
"rank" => 0,
],
[
"misuse" => "2",
"rank" => 0,
]
]
$this->createRunWithMisuses("-p-", "-v-", 1);
$this->createRunWithMisuses("-p1-", "-v-", 1);
list($misuse1, $misuse2) = Misuse::all()->all();
$runs = Run::of(Detector::find('test-detector'))->in(Experiment::find(2))->get();

$this->createConclusiveReviewState($misuse1);

// current misuse = 2, reviewer = 1
list($previous_misuse, $next_misuse, $next_reviewable_misuse, $misuse) = $this->reviewControllerHelper->reviewController->determineNavigationTargets($runs, Experiment::find(2), '-p1-', '-v-', $misuse2->misuse_muid, $this->reviewer1, 30);

self::assertEquals($misuse1->misuse_muid, $previous_misuse->misuse_muid);
self::assertEquals($misuse1->misuse_muid, $next_misuse->misuse_muid);
self::assertNull($next_reviewable_misuse);
self::assertEquals($misuse2->misuse_muid, $misuse->misuse_muid);
}

private function createRunWithMisuses($project, $version, $amount)
{
$findings = [
"result" => "success",
"runtime" => 42.1,
"number_of_findings" => 23,
"timestamp" => 12,
"potential_hits" => [
]
);
return Misuse::all()->all();
];
for($i = 0; $i < $amount; $i++){
$findings["potential_hits"][] = [
"misuse" => "{$i}",
"rank" => 0,
"file" => "f",
"target_snippets" => []
];
}
$runsController = new RunsController($this->container);
$runsController->addRun(2, 'test-detector', $project, $version, $findings);
}

private function createConclusiveReviewState($misuse)
Expand Down
Loading