11<?php
2- declare (strict_types=1 );
2+ declare ( strict_types=1 );
33
44/**
55 * Validation applied to verify wether the bot should scan or skip the file specified
66 */
77
88/**
9- * @param $temp_file_name
10- * @param $file_name
9+ * @param string $temp_file_name
10+ * @param string $file_name
1111 * @param string $commit_id
12+ * @param int $max_lines
1213 *
1314 * Validates if the file is valid to be scanned by vip-go-ci
1415 *
1516 * @return array
16- * [
17- * 'issues' =>
18- * [
19- * 'max-lines' => [$file_name],
20- * ],
21- * 'total' => 1
22- * ]
17+ * [
18+ * 'issues' =>
19+ * [
20+ * 'max-lines' => [$file_name],
21+ * ],
22+ * 'total' => 1
23+ * ]
2324 * In a oop we could simply inject a service for caching
2425 * Vars and set values in the constructor
2526 */
26- function vipgoci_validate ( string $ temp_file_name , string $ file_name , string $ commit_id ): array
27- {
27+ function vipgoci_validate ( string $ temp_file_name , string $ file_name , string $ commit_id , int $ max_lines ): array {
2828 $ validation_result = array ( 'total ' => 0 );
2929
30- if ( false === vipgoci_is_number_of_lines_valid ( $ temp_file_name , $ file_name , $ commit_id ) ) {
31- $ validation_result [ 'issues ' ][ VIPGOCI_VALIDATION_MAXIMUM_LINES ] = [ $ file_name ];
32- $ validation_result [ 'total ' ] = count ( $ validation_result [ 'issues ' ] );
30+ if ( false === vipgoci_is_number_of_lines_valid ( $ temp_file_name , $ file_name , $ commit_id, $ max_lines ) ) {
31+ $ validation_result ['issues ' ][ VIPGOCI_VALIDATION_MAXIMUM_LINES ] = [ $ file_name ];
32+ $ validation_result ['total ' ] = count ( $ validation_result ['issues ' ] );
3333 }
3434
3535 return $ validation_result ;
@@ -39,17 +39,17 @@ function vipgoci_validate( string $temp_file_name, string $file_name, string $co
3939 * @param string $temp_file_name
4040 * @param string $file_name
4141 * @param string $commit_id
42+ * @param int $max_lines
4243 *
4344 * @return bool
4445 */
45- function vipgoci_is_number_of_lines_valid ( string $ temp_file_name , string $ file_name , string $ commit_id ): bool
46- {
46+ function vipgoci_is_number_of_lines_valid ( string $ temp_file_name , string $ file_name , string $ commit_id , int $ max_lines ): bool {
4747 /**
4848 * Verifies if number of lines validation are in the cache
4949 * If so, returns the value
5050 */
5151
52- $ cache_key = vipgoci_cache_get_is_number_of_lines_valid_key ( $ file_name , $ commit_id );
52+ $ cache_key = vipgoci_cache_get_is_number_of_lines_valid_key ( $ file_name , $ commit_id );
5353 $ is_number_of_lines_valid = vipgoci_cache_get_is_number_of_lines_valid ( $ cache_key );
5454 if ( ! is_null ( $ is_number_of_lines_valid ) ) {
5555 return $ is_number_of_lines_valid ;
@@ -72,7 +72,7 @@ function vipgoci_is_number_of_lines_valid( string $temp_file_name, string $file_
7272 array ( 'file_name ' => $ file_name , 'cmd ' => $ cmd , 'output ' => $ output )
7373 );
7474
75- $ is_number_of_lines_valid = vipgoci_verify_number_of_lines_output ( $ output );
75+ $ is_number_of_lines_valid = vipgoci_verify_number_of_lines_output ( $ output, $ max_lines );
7676
7777 vipgoci_cache_set_is_number_of_lines_valid ( $ cache_key , $ is_number_of_lines_valid );
7878
@@ -85,8 +85,7 @@ function vipgoci_is_number_of_lines_valid( string $temp_file_name, string $file_
8585 *
8686 * Sets cache for converted is_number_of_lines_valid
8787 */
88- function vipgoci_cache_set_is_number_of_lines_valid ( array $ cache_key , bool $ is_number_of_lines_valid ): void
89- {
88+ function vipgoci_cache_set_is_number_of_lines_valid ( array $ cache_key , bool $ is_number_of_lines_valid ): void {
9089 vipgoci_cache (
9190 $ cache_key ,
9291 $ is_number_of_lines_valid === true ? 'true ' : 'false '
@@ -95,14 +94,12 @@ function vipgoci_cache_set_is_number_of_lines_valid( array $cache_key, bool $is_
9594
9695/**
9796 * @param string $output
97+ * @param int $max_lines
9898 *
9999 * @return bool
100100 */
101- function vipgoci_verify_number_of_lines_output ( string $ output ): bool
102- {
103- return is_numeric ( $ output )
104- ? $ output < VIPGOCI_VALIDATION_MAXIMUM_LINES_LIMIT
105- : false ;
101+ function vipgoci_verify_number_of_lines_output ( string $ output , int $ max_lines ): bool {
102+ return is_numeric ( $ output ) && $ output < $ max_lines ;
106103}
107104
108105/**
@@ -113,11 +110,10 @@ function vipgoci_verify_number_of_lines_output( string $output ): bool
113110 *
114111 * @return bool|null
115112 */
116- function vipgoci_cache_get_is_number_of_lines_valid ( array $ cache_key ): ?bool
117- {
113+ function vipgoci_cache_get_is_number_of_lines_valid ( array $ cache_key ): ?bool {
118114 $ cached_value = vipgoci_cache ( $ cache_key );
119115
120- return false !== $ cached_value ? 'true ' === $ cached_value : null ;
116+ return false !== $ cached_value ? 'true ' === $ cached_value : null ;
121117}
122118
123119/**
@@ -127,7 +123,6 @@ function vipgoci_cache_get_is_number_of_lines_valid( array $cache_key ): ?bool
127123 *
128124 * @return array
129125 */
130- function vipgoci_cache_get_is_number_of_lines_valid_key ( string $ file_name , string $ commit_id ): array
131- {
126+ function vipgoci_cache_get_is_number_of_lines_valid_key ( string $ file_name , string $ commit_id ): array {
132127 return array ( __FUNCTION__ , $ file_name , $ commit_id );
133128}
0 commit comments