diff --git a/app/Http/Controllers/ManageMeasurementsController.php b/app/Http/Controllers/ManageMeasurementsController.php index c3a1284b86..2576078ce4 100644 --- a/app/Http/Controllers/ManageMeasurementsController.php +++ b/app/Http/Controllers/ManageMeasurementsController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers; use App\Models\Measurement; +use App\Models\Project as EloquentProject; use App\Services\PageTimer; use Illuminate\Http\JsonResponse; use Illuminate\Support\Facades\Gate; @@ -37,16 +38,17 @@ public function apiGet(): JsonResponse // Get any measurements associated with this project's tests. $measurements_response = []; - $measurements = Measurement::where('projectid', $this->project->Id) + $measurements = EloquentProject::findOrFail($this->project->Id) + ->measurements() ->orderBy('position', 'asc') ->get(); foreach ($measurements as $measurement) { - $measurement_response = []; - $measurement_response['id'] = $measurement->id; - $measurement_response['name'] = $measurement->name; - $measurement_response['position'] = $measurement->position; - $measurements_response[] = $measurement_response; + $measurements_response[] = [ + 'id' => $measurement->id, + 'name' => $measurement->name, + 'position' => $measurement->position, + ]; } $response['measurements'] = $measurements_response; $pageTimer->end($response); @@ -103,10 +105,10 @@ public function apiDelete(): JsonResponse abort(400, 'Invalid measurement ID provided.'); } - $deleted = Measurement::where([ - 'id' => (int) request()->input('id'), - 'projectid' => $this->project->Id, - ])->delete(); + $deleted = EloquentProject::findOrFail($this->project->Id) + ->measurements() + ->where('id', (int) request()->input('id')) + ->delete(); if ($deleted) { return response()->json(); diff --git a/app/Models/Measurement.php b/app/Models/Measurement.php index b852f4392d..2de4217f2c 100644 --- a/app/Models/Measurement.php +++ b/app/Models/Measurement.php @@ -4,6 +4,7 @@ use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\BelongsTo; /** * @property int $id @@ -16,11 +17,26 @@ class Measurement extends Model { protected $table = 'measurement'; + + public $timestamps = false; + protected $fillable = [ 'projectid', 'name', 'position', ]; - public $timestamps = false; + protected $casts = [ + 'id' => 'integer', + 'projectid' => 'integer', + 'position' => 'integer', + ]; + + /** + * @return BelongsTo + */ + public function project(): BelongsTo + { + return $this->belongsTo(Project::class, 'id', 'projectid'); + } } diff --git a/app/Models/Project.php b/app/Models/Project.php index aa124ef4e6..92132559ab 100644 --- a/app/Models/Project.php +++ b/app/Models/Project.php @@ -116,4 +116,12 @@ public function subprojects(?Carbon $date = null): HasMany ->orWhere('endtime', '=', Carbon::create(1980)); }); } + + /** + * @return HasMany + */ + public function measurements(): HasMany + { + return $this->hasMany(Measurement::class, 'projectid', 'id'); + } } diff --git a/app/cdash/app/Controller/Api/QueryTests.php b/app/cdash/app/Controller/Api/QueryTests.php index 8a0ac99538..816afdcf8c 100644 --- a/app/cdash/app/Controller/Api/QueryTests.php +++ b/app/cdash/app/Controller/Api/QueryTests.php @@ -16,7 +16,9 @@ namespace CDash\Controller\Api; +use App\Models\Measurement; use App\Models\TestOutput; +use App\Models\Project as EloquentProject; use CDash\Database; use CDash\Model\Build; @@ -272,28 +274,20 @@ public function getResponse() $project_response['showtesttime'] = $this->project->ShowTestTime; $response['project'] = $project_response; - // Get information about all the builds for the given project - // and date range. - $builds = []; - - // Add the date/time - $builds['projectid'] = $this->project->Id; - $builds['currentstarttime'] = $this->currentStartTime; - $builds['teststarttime'] = $this->beginDate; - $builds['testendtime'] = $this->endDate; - // Get the list of extra test measurements that should be displayed on this page. $this->extraMeasurements = []; - $stmt = $this->db->prepare( - 'SELECT name FROM measurement WHERE projectid = ? ORDER BY position'); - $this->db->execute($stmt, [$this->project->Id]); - while ($row = $stmt->fetch()) { + $measurements = EloquentProject::findOrFail($this->project->Id) + ->measurements() + ->orderBy('position') + ->get(); + /** @var Measurement $measurement */ + foreach ($measurements as $measurement) { // If we have the Processors measurement, then we should also // compute and display 'Proc Time'. - if ($row['name'] == 'Processors') { + if ($measurement->name === 'Processors') { $this->hasProcessors = true; } else { - $this->extraMeasurements[] = $row['name']; + $this->extraMeasurements[] = $measurement->name; } } $this->numExtraMeasurements = count($this->extraMeasurements); diff --git a/app/cdash/app/Controller/Api/TestDetails.php b/app/cdash/app/Controller/Api/TestDetails.php index 8aed810992..eb215bd1af 100644 --- a/app/cdash/app/Controller/Api/TestDetails.php +++ b/app/cdash/app/Controller/Api/TestDetails.php @@ -18,6 +18,7 @@ use App\Models\BuildTest; use App\Models\TestOutput; +use App\Models\Project as EloquentProject; use CDash\Database; @@ -286,13 +287,11 @@ public function getResponse() } // Get the list of extra test measurements that have been explicitly added to this project. - $stmt = $this->db->prepare( - 'SELECT name FROM measurement WHERE projectid = ? ORDER BY position'); - $this->db->execute($stmt, [$this->project->Id]); - $extra_measurements = []; - while ($row = $stmt->fetch()) { - $extra_measurements[] = $row['name']; - } + $extra_measurements = EloquentProject::findOrFail($this->project->Id) + ->measurements() + ->orderBy('position') + ->pluck('name') + ->toArray(); // Sort measurements: put those listed explicitly first (sorted by position) // then sort the rest alphabetically by name. diff --git a/app/cdash/app/Controller/Api/ViewTest.php b/app/cdash/app/Controller/Api/ViewTest.php index 4d4f5c82ce..8a16c8b3ed 100644 --- a/app/cdash/app/Controller/Api/ViewTest.php +++ b/app/cdash/app/Controller/Api/ViewTest.php @@ -17,6 +17,7 @@ namespace CDash\Controller\Api; use App\Models\BuildTest; +use App\Models\Project as EloquentProject; use CDash\Config; use CDash\Database; @@ -272,16 +273,15 @@ public function getResponse() // Get the list of extra measurements that should be displayed on this page. $response['hasprocessors'] = false; $processors_idx = -1; - $extra_measurements_stmt = $this->db->prepare( - 'SELECT name FROM measurement - WHERE projectid = ? - ORDER BY position'); - $this->db->execute($extra_measurements_stmt, [$this->project->Id]); - while ($row = $extra_measurements_stmt->fetch()) { - $this->extraMeasurements[] = $row['name']; + $extra_measurements = EloquentProject::findOrFail($this->project->Id) + ->measurements() + ->orderBy('position') + ->get(); + foreach ($extra_measurements as $extra_measurement) { + $this->extraMeasurements[] = $extra_measurement->name; // If we have the Processors measurement, then we should also // compute and display 'Proc Time'. - if ($row['name'] == 'Processors') { + if ($extra_measurement->name === 'Processors') { $processors_idx = count($this->extraMeasurements) - 1; $response['hasprocessors'] = true; } diff --git a/app/cdash/public/api/v1/index.php b/app/cdash/public/api/v1/index.php index a530ed1405..32df9c2419 100644 --- a/app/cdash/public/api/v1/index.php +++ b/app/cdash/public/api/v1/index.php @@ -22,6 +22,7 @@ use CDash\Controller\Api\Index as IndexController; use CDash\Database; use App\Models\Banner; +use App\Models\Project as EloquentProject; use CDash\Model\Build; use App\Models\BuildInformation; use CDash\Model\BuildGroup; @@ -639,14 +640,10 @@ // We support an additional advanced column called 'Proc Time'. // This is only shown if this project is setup to display // an extra test measurement called 'Processors'. -$stmt = DB::select(" - SELECT count(*) AS c - FROM measurement - WHERE - projectid = ? - AND name = 'Processors' - ", [$Project->Id])[0]; -$response['showProcTime'] = $stmt->c > 0; +$response['showProcTime'] = EloquentProject::findOrFail($Project->Id) + ->measurements() + ->where('name', 'Processors') + ->exists(); $response['buildgroups'] = $controller->buildgroupsResponse; $response['updatetype'] = $controller->updateType; diff --git a/app/cdash/tests/test_managemeasurements.php b/app/cdash/tests/test_managemeasurements.php index 04fe9dbd45..c13b2433b0 100644 --- a/app/cdash/tests/test_managemeasurements.php +++ b/app/cdash/tests/test_managemeasurements.php @@ -3,11 +3,10 @@ // After including cdash_test_case.php, subsequent require_once calls are // relative to the top of the CDash source tree // -require_once dirname(__FILE__) . '/cdash_test_case.php'; - - +require_once dirname(__FILE__) . '/cdash_test_case.php'; +use App\Models\Measurement; class ManageMeasurementsTestCase extends KWWebTestCase { @@ -37,10 +36,8 @@ public function __destruct() if (!is_null($this->SubProjectBuildId)) { remove_build($this->SubProjectBuildId); } - foreach ($this->MeasurementIds as $measurement_id) { - $this->PDO->query( - "DELETE FROM measurement WHERE id = $measurement_id"); - } + + Measurement::destroy($this->MeasurementIds); } // function to validate test results returned by the API. diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index fd9bc5290f..03037b8380 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -748,7 +748,17 @@ parameters: path: app/Http/Controllers/ManageBannerController.php - - message: "#^Dynamic call to static method Illuminate\\\\Database\\\\Eloquent\\\\Builder\\\\:\\:orderBy\\(\\)\\.$#" + message: "#^Dynamic call to static method Illuminate\\\\Database\\\\Eloquent\\\\Relations\\\\HasMany\\\\:\\:delete\\(\\)\\.$#" + count: 1 + path: app/Http/Controllers/ManageMeasurementsController.php + + - + message: "#^Dynamic call to static method Illuminate\\\\Database\\\\Eloquent\\\\Relations\\\\HasMany\\\\:\\:orderBy\\(\\)\\.$#" + count: 1 + path: app/Http/Controllers/ManageMeasurementsController.php + + - + message: "#^Dynamic call to static method Illuminate\\\\Database\\\\Eloquent\\\\Relations\\\\HasMany\\\\:\\:where\\(\\)\\.$#" count: 1 path: app/Http/Controllers/ManageMeasurementsController.php @@ -3094,9 +3104,14 @@ parameters: count: 1 path: app/cdash/app/Controller/Api/QueryTests.php + - + message: "#^Dynamic call to static method Illuminate\\\\Database\\\\Eloquent\\\\Relations\\\\HasMany\\\\:\\:orderBy\\(\\)\\.$#" + count: 1 + path: app/cdash/app/Controller/Api/QueryTests.php + - message: "#^Loose comparison via \"\\=\\=\" is not allowed\\.$#" - count: 10 + count: 9 path: app/cdash/app/Controller/Api/QueryTests.php - @@ -3319,6 +3334,16 @@ parameters: count: 2 path: app/cdash/app/Controller/Api/TestDetails.php + - + message: "#^Dynamic call to static method Illuminate\\\\Database\\\\Eloquent\\\\Relations\\\\HasMany\\\\:\\:orderBy\\(\\)\\.$#" + count: 1 + path: app/cdash/app/Controller/Api/TestDetails.php + + - + message: "#^Dynamic call to static method Illuminate\\\\Database\\\\Eloquent\\\\Relations\\\\HasMany\\\\:\\:pluck\\(\\)\\.$#" + count: 1 + path: app/cdash/app/Controller/Api/TestDetails.php + - message: "#^Loose comparison via \"\\!\\=\" is not allowed\\.$#" count: 1 @@ -3626,6 +3651,11 @@ parameters: count: 5 path: app/cdash/app/Controller/Api/ViewTest.php + - + message: "#^Dynamic call to static method Illuminate\\\\Database\\\\Eloquent\\\\Relations\\\\HasMany\\\\:\\:orderBy\\(\\)\\.$#" + count: 1 + path: app/cdash/app/Controller/Api/ViewTest.php + - message: "#^Foreach overwrites \\$test with its value variable\\.$#" count: 1 @@ -3638,7 +3668,7 @@ parameters: - message: "#^Loose comparison via \"\\=\\=\" is not allowed\\.$#" - count: 11 + count: 10 path: app/cdash/app/Controller/Api/ViewTest.php - @@ -15574,6 +15604,16 @@ parameters: count: 3 path: app/cdash/public/api/v1/index.php + - + message: "#^Dynamic call to static method Illuminate\\\\Database\\\\Eloquent\\\\Relations\\\\HasMany\\\\:\\:exists\\(\\)\\.$#" + count: 1 + path: app/cdash/public/api/v1/index.php + + - + message: "#^Dynamic call to static method Illuminate\\\\Database\\\\Eloquent\\\\Relations\\\\HasMany\\\\:\\:where\\(\\)\\.$#" + count: 1 + path: app/cdash/public/api/v1/index.php + - message: """ #^Call to deprecated function pdo_error\\(\\)\\: