Overview

Namespaces

  • Contrib
    • Bundle
      • CoverallsBundle
        • Console
        • Entity
      • CoverallsV1Bundle
        • Api
        • Collector
        • Command
        • Config
        • Entity
          • Git
    • Component
      • File
      • Log
      • System
        • Git
  • Guzzle
    • Batch
      • Exception
    • Cache
    • Common
      • Exception
    • Http
      • Curl
      • Exception
      • Message
      • QueryAggregator
    • Inflection
    • Iterator
    • Log
    • Parser
      • Cookie
      • Message
      • UriTemplate
      • Url
    • Plugin
      • Async
      • Backoff
      • Cache
      • Cookie
        • CookieJar
        • Exception
      • CurlAuth
      • ErrorResponse
        • Exception
      • History
      • Log
      • Md5
      • Mock
      • Oauth
    • Service
      • Builder
      • Command
        • Factory
        • LocationVisitor
          • Request
          • Response
      • Description
      • Exception
      • Resource
    • Stream
  • PHP
  • Psr
    • Log
  • Symfony
    • Component
      • Config
        • Definition
          • Builder
          • Exception
        • Exception
        • Loader
        • Resource
        • Util
      • Console
        • Command
        • Formatter
        • Helper
        • Input
        • Output
        • Tester
      • EventDispatcher
        • Debug
      • Finder
        • Adapter
        • Comparator
        • Exception
        • Expression
        • Iterator
        • Shell
      • Stopwatch
      • Yaml
        • Exception

Classes

  • Coveralls
  • JsonFile
  • Metrics
  • SourceFile
  • Overview
  • Namespace
  • Class
  • Tree
  • Todo
  1: <?php
  2: namespace Contrib\Bundle\CoverallsV1Bundle\Entity;
  3: 
  4: use Contrib\Bundle\CoverallsV1Bundle\Entity\Git\Git;
  5: 
  6: /**
  7:  * Data represents "json_file" of Coveralls API.
  8:  *
  9:  * @author Kitamura Satoshi <with.no.parachute@gmail.com>
 10:  */
 11: class JsonFile extends Coveralls
 12: {
 13:     /**
 14:      * Service name.
 15:      *
 16:      * @var string
 17:      */
 18:     protected $serviceName;
 19: 
 20:     /**
 21:      * Service job id.
 22:      *
 23:      * @var string
 24:      */
 25:     protected $serviceJobId;
 26: 
 27:     /**
 28:      * Service number (not documented).
 29:      *
 30:      * @var string
 31:      */
 32:     protected $serviceNumber;
 33: 
 34:     /**
 35:      * Service event type (not documented).
 36:      *
 37:      * @var string
 38:      */
 39:     protected $serviceEventType;
 40: 
 41:     /**
 42:      * Build URL of the project (not documented).
 43:      *
 44:      * @var string
 45:      */
 46:     protected $serviceBuildUrl;
 47: 
 48:     /**
 49:      * Branch name (not documented).
 50:      *
 51:      * @var string
 52:      */
 53:     protected $serviceBranch;
 54: 
 55:     /**
 56:      * Pull request info (not documented).
 57:      *
 58:      * @var string
 59:      */
 60:     protected $servicePullRequest;
 61: 
 62:     /**
 63:      * Repository token.
 64:      *
 65:      * @var string
 66:      */
 67:     protected $repoToken;
 68: 
 69:     /**
 70:      * Source files.
 71:      *
 72:      * @var \Contrib\Bundle\CoverallsV1Bundle\Entity\SourceFile[]
 73:      */
 74:     protected $sourceFiles = array();
 75: 
 76:     /**
 77:      * Git data.
 78:      *
 79:      * @var array
 80:      */
 81:     protected $git;
 82: 
 83:     /**
 84:      * A timestamp when the job ran. Must be parsable by Ruby.
 85:      *
 86:      * "2013-02-18 00:52:48 -0800"
 87:      *
 88:      * @var string
 89:      */
 90:     protected $runAt;
 91: 
 92:     /**
 93:      * Metrics.
 94:      *
 95:      * @var Metrics
 96:      */
 97:     protected $metrics;
 98: 
 99:     // API
100: 
101:     /**
102:      * {@inheritdoc}
103:      *
104:      * @see \Contrib\Bundle\CoverallsBundle\Entity\ArrayConvertable::toArray()
105:      */
106:     public function toArray()
107:     {
108:         $array = array();
109: 
110:         $arrayMap = array(
111:             // json key => property name
112:             'service_name'         => 'serviceName',
113:             'service_job_id'       => 'serviceJobId',
114:             'service_number'       => 'serviceNumber',
115:             'service_build_url'    => 'serviceBuildUrl',
116:             'service_branch'       => 'serviceBranch',
117:             'service_pull_request' => 'servicePullRequest',
118:             'service_event_type'   => 'serviceEventType',
119:             'repo_token'           => 'repoToken',
120:             'git'                  => 'git',
121:             'run_at'               => 'runAt',
122:             'source_files'         => 'sourceFiles',
123:         );
124: 
125:         foreach ($arrayMap as $jsonKey => $propName) {
126:             if (isset($this->$propName)) {
127:                 $array[$jsonKey] = $this->toJsonProperty($this->$propName);
128:             }
129:         }
130: 
131:         return $array;
132:     }
133: 
134:     /**
135:      * Fill environment variables.
136:      *
137:      * @param  array                                             $env $_SERVER environment.
138:      * @return \Contrib\Bundle\CoverallsV1Bundle\Entity\JsonFile
139:      * @throws \RuntimeException
140:      */
141:     public function fillJobs(array $env)
142:     {
143:         return $this
144:         ->fillStandardizedEnvVars($env)
145:         ->ensureJobs();
146:     }
147: 
148:     /**
149:      * Exclude source files that have no executable statements.
150:      *
151:      * @return void
152:      */
153:     public function excludeNoStatementsFiles()
154:     {
155:         $this->sourceFiles = array_filter(
156:             $this->sourceFiles,
157:             function (SourceFile $sourceFile) {
158:                 return $sourceFile->getMetrics()->hasStatements();
159:             }
160:         );
161:     }
162: 
163:     /**
164:      * Sort source files by path.
165:      *
166:      * @return void
167:      */
168:     public function sortSourceFiles()
169:     {
170:         ksort($this->sourceFiles);
171:     }
172: 
173:     /**
174:      * Return line coverage.
175:      *
176:      * @return float
177:      */
178:     public function reportLineCoverage()
179:     {
180:         $metrics = $this->getMetrics();
181: 
182:         foreach ($this->sourceFiles as $sourceFile) {
183:             /* @var $sourceFile \Contrib\Bundle\CoverallsV1Bundle\Entity\SourceFile */
184:             $metrics->merge($sourceFile->getMetrics());
185:         }
186: 
187:         return $metrics->getLineCoverage();
188:     }
189: 
190:     // internal method
191: 
192:     /**
193:      * Convert to json property.
194:      *
195:      * @param  mixed $prop
196:      * @return mixed
197:      */
198:     protected function toJsonProperty($prop)
199:     {
200:         if ($prop instanceof Coveralls) {
201:             return $prop->toArray();
202:         } elseif (is_array($prop)) {
203:             return $this->toJsonPropertyArray($prop);
204:         }
205: 
206:         return $prop;
207:     }
208: 
209:     /**
210:      * Convert to array as json property.
211:      *
212:      * @param  array $propArray
213:      * @return array
214:      */
215:     protected function toJsonPropertyArray(array $propArray)
216:     {
217:         $array = array();
218: 
219:         foreach ($propArray as $prop) {
220:             $array[] = $this->toJsonProperty($prop);
221:         }
222: 
223:         return $array;
224:     }
225: 
226:     /**
227:      * Fill standardized environment variables.
228:      *
229:      * "CI_NAME", "CI_BUILD_NUMBER" must be set.
230:      *
231:      * Env vars are:
232:      *
233:      * * CI_NAME
234:      * * CI_BUILD_NUMBER
235:      * * CI_BUILD_URL
236:      * * CI_BRANCH
237:      * * CI_PULL_REQUEST
238:      *
239:      * These vars are supported by Codeship.
240:      *
241:      * @param  array                                             $env $_SERVER environment.
242:      * @return \Contrib\Bundle\CoverallsV1Bundle\Entity\JsonFile
243:      */
244:     protected function fillStandardizedEnvVars(array $env)
245:     {
246:         $map = array(
247:             // defined in Ruby lib
248:             'serviceName'        => 'CI_NAME',
249:             'serviceNumber'      => 'CI_BUILD_NUMBER',
250:             'serviceBuildUrl'    => 'CI_BUILD_URL',
251:             'serviceBranch'      => 'CI_BRANCH',
252:             'servicePullRequest' => 'CI_PULL_REQUEST',
253: 
254:             // extends by php-coveralls
255:             'serviceJobId'       => 'CI_JOB_ID',
256:             'serviceEventType'   => 'COVERALLS_EVENT_TYPE',
257:             'repoToken'          => 'COVERALLS_REPO_TOKEN',
258:         );
259: 
260:         foreach ($map as $propName => $envName) {
261:             if (isset($env[$envName])) {
262:                 $this->$propName = $env[$envName];
263:             }
264:         }
265: 
266:         return $this;
267:     }
268: 
269:     /**
270:      * Ensure data consistency for jobs API.
271:      *
272:      * @return \Contrib\Bundle\CoverallsV1Bundle\Entity\JsonFile
273:      * @throws \RuntimeException
274:      */
275:     protected function ensureJobs()
276:     {
277:         if (!$this->hasSourceFiles()) {
278:             throw new \RuntimeException('source_files must be set');
279:         }
280: 
281:         if ($this->requireServiceJobId()) {
282:             return $this;
283:         }
284: 
285:         if ($this->requireServiceNumber()) {
286:             return $this;
287:         }
288: 
289:         if ($this->requireServiceEventType()) {
290:             return $this;
291:         }
292: 
293:         if ($this->isUnsupportedServiceJob()) {
294:             return $this;
295:         }
296: 
297:         $message = 'requirements are not satisfied.';
298: 
299:         throw new \RuntimeException($message);
300:     }
301: 
302:     /**
303:      * Return whether the job requires "service_job_id" (for Travis CI).
304:      *
305:      * @return boolean
306:      */
307:     protected function requireServiceJobId()
308:     {
309:         return isset($this->serviceName) && isset($this->serviceJobId) && !isset($this->repoToken);
310:     }
311: 
312:     /**
313:      * Return whether the job requires "service_number" (for CircleCI, Jenkins, Codeship or other CIs).
314:      *
315:      * @return boolean
316:      */
317:     protected function requireServiceNumber()
318:     {
319:         return isset($this->serviceName) && isset($this->serviceNumber) && isset($this->repoToken);
320:     }
321: 
322:     /**
323:      * Return whether the job requires "service_event_type" (for local environment).
324:      *
325:      * @return boolean
326:      */
327:     protected function requireServiceEventType()
328:     {
329:         return isset($this->serviceName) && isset($this->serviceEventType) && isset($this->repoToken);
330:     }
331: 
332:     /**
333:      * Return whether the job is running on unsupported service.
334:      *
335:      * @return boolean
336:      */
337:     protected function isUnsupportedServiceJob()
338:     {
339:         return !isset($this->serviceJobId) && !isset($this->serviceNumber) && !isset($this->serviceEventType) && isset($this->repoToken);
340:     }
341: 
342:     // accessor
343: 
344:     /**
345:      * Return whether the json file has source file.
346:      *
347:      * @param  string  $path Absolute path to source file.
348:      * @return boolean
349:      */
350:     public function hasSourceFile($path)
351:     {
352:         return isset($this->sourceFiles[$path]);
353:     }
354: 
355:     /**
356:      * Return source file.
357:      *
358:      * @param  string                                                   $path Absolute path to source file.
359:      * @return \Contrib\Bundle\CoverallsV1Bundle\Entity\SourceFile|null
360:      */
361:     public function getSourceFile($path)
362:     {
363:         if ($this->hasSourceFile($path)) {
364:             return $this->sourceFiles[$path];
365:         }
366: 
367:         return null;
368:     }
369: 
370:     /**
371:      * Add source file.
372:      *
373:      * @param SourceFile $sourceFile
374:      */
375:     public function addSourceFile(SourceFile $sourceFile)
376:     {
377:         $this->sourceFiles[$sourceFile->getPath()] = $sourceFile;
378:     }
379: 
380:     /**
381:      * Return whether the json file has a source file.
382:      *
383:      * @return boolean
384:      */
385:     public function hasSourceFiles()
386:     {
387:         return count($this->sourceFiles) > 0;
388:     }
389: 
390:     /**
391:      * Return source files.
392:      *
393:      * @return SourceFile[]
394:      */
395:     public function getSourceFiles()
396:     {
397:         return $this->sourceFiles;
398:     }
399: 
400:     /**
401:      * Set service name.
402:      *
403:      * @param  string    $serviceName Service name.
404:      * @return Coveralls
405:      */
406:     public function setServiceName($serviceName)
407:     {
408:         $this->serviceName = $serviceName;
409: 
410:         return $this;
411:     }
412: 
413:     /**
414:      * Return service name.
415:      *
416:      * @return string
417:      */
418:     public function getServiceName()
419:     {
420:         if (isset($this->serviceName)) {
421:             return $this->serviceName;
422:         }
423: 
424:         return null;
425:     }
426: 
427:     /**
428:      * Set repository token.
429:      *
430:      * @param  string    $repoToken Repository token.
431:      * @return Coveralls
432:      */
433:     public function setRepoToken($repoToken)
434:     {
435:         $this->repoToken = $repoToken;
436: 
437:         return $this;
438:     }
439: 
440:     /**
441:      * Return repository token.
442:      *
443:      * @return string
444:      */
445:     public function getRepoToken()
446:     {
447:         if (isset($this->repoToken)) {
448:             return $this->repoToken;
449:         }
450: 
451:         return null;
452:     }
453: 
454:     /**
455:      * Set service job id.
456:      *
457:      * @param  string    $serviceJobId Service job id.
458:      * @return Coveralls
459:      */
460:     public function setServiceJobId($serviceJobId)
461:     {
462:         $this->serviceJobId = $serviceJobId;
463: 
464:         return $this;
465:     }
466: 
467:     /**
468:      * Return service job id.
469:      *
470:      * @return string
471:      */
472:     public function getServiceJobId()
473:     {
474:         if (isset($this->serviceJobId)) {
475:             return $this->serviceJobId;
476:         }
477: 
478:         return null;
479:     }
480: 
481:     /**
482:      * Return service number.
483:      *
484:      * @return string
485:      */
486:     public function getServiceNumber()
487:     {
488:         return $this->serviceNumber;
489:     }
490: 
491:     /**
492:      * Return service event type.
493:      *
494:      * @return string
495:      */
496:     public function getServiceEventType()
497:     {
498:         return $this->serviceEventType;
499:     }
500: 
501:     /**
502:      * Return build URL of the project.
503:      *
504:      * @return string
505:      */
506:     public function getServiceBuildUrl()
507:     {
508:         return $this->serviceBuildUrl;
509:     }
510: 
511:     /**
512:      * Return branch name.
513:      *
514:      * @return string
515:      */
516:     public function getServiceBranch()
517:     {
518:         return $this->serviceBranch;
519:     }
520: 
521:     /**
522:      * Return pull request info.
523:      *
524:      * @return string
525:      */
526:     public function getServicePullRequest()
527:     {
528:         return $this->servicePullRequest;
529:     }
530: 
531:     /**
532:      * Set git data.
533:      *
534:      * @param  array     $git Git data.
535:      * @return Coveralls
536:      */
537:     public function setGit(Git $git)
538:     {
539:         $this->git = $git;
540: 
541:         return $this;
542:     }
543: 
544:     /**
545:      * Return git data.
546:      *
547:      * @return array
548:      */
549:     public function getGit()
550:     {
551:         if (isset($this->git)) {
552:             return $this->git;
553:         }
554: 
555:         return null;
556:     }
557: 
558:     /**
559:      * Set timestamp when the job ran.
560:      *
561:      * @param  string    $runAt Timestamp.
562:      * @return Coveralls
563:      */
564:     public function setRunAt($runAt)
565:     {
566:         $this->runAt = $runAt;
567: 
568:         return $this;
569:     }
570: 
571:     /**
572:      * Return timestamp when the job ran.
573:      *
574:      * @return string
575:      */
576:     public function getRunAt()
577:     {
578:         if (isset($this->runAt)) {
579:             return $this->runAt;
580:         }
581: 
582:         return null;
583:     }
584: 
585:     /**
586:      * Return metrics.
587:      *
588:      * @return \Contrib\Bundle\CoverallsV1Bundle\Entity\Metrics
589:      */
590:     public function getMetrics()
591:     {
592:         if (!isset($this->metrics)) {
593:             $this->metrics = new Metrics();
594:         }
595: 
596:         return $this->metrics;
597:     }
598: }
599: 
php-coveralls API documentation generated by ApiGen 2.8.0