GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 61aacd...05f06e )
by Alexey
18:53
created

ViewHelper   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 46.03%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 27
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 139
rs 10
ccs 29
cts 63
cp 0.4603

7 Methods

Rating   Name   Duplication   Size   Complexity  
B menu() 0 29 5
A header() 0 4 1
C replace() 0 27 11
A contact() 0 7 2
A phones() 0 9 2
A phone() 0 11 3
A getClearPhone() 0 13 3
1
<?php
2
/**
3
 * @author Alexey Tatarinov <[email protected]>
4
 * @link https://github.com/shogodev/argilla/
5
 * @copyright Copyright &copy; 2003-2015 Shogo
6
 * @license http://argilla.ru/LICENSE
7
 */
8
class ViewHelper
9
{
10
  /**
11
   * Строит виджет FMenu по входному массиву или масииву объектов
12
   * @param CComponent[]|array $elements
13
   * @param array $attributeOptions атрибуты для построения эламента массива из объектов
14
   * @param array $menuOptions опции виджета FMenu
15
   *
16
   * @return array
17
   *
18
   * Пример использования:
19
   * ViewHelper::menu($part, array('label' => 'name', 'url' => 'url'), array('itemTemplate' => '{menu} ({linkCount})'))
20
   */
21 7
  public static function menu($elements, $attributeOptions = array(), $menuOptions = array())
22
  {
23 7
    $items = array();
24
25 7
    foreach($elements as $key => $element)
26
    {
27 7
      if( $element instanceof CComponent )
0 ignored issues
show
Bug introduced by
The class CComponent does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
28 7
      {
29
        $item = array();
30
        foreach($attributeOptions as $name => $attribute)
31
        {
32
          $item[$name] = $element->$attribute;
33
        }
34
      }
35
      else
36
      {
37 7
        $item = $element;
38
      }
39
40 7
      if( isset($menuOptions['itemTemplate']) )
41 7
      {
42
        $item['template'] = self::replace($menuOptions['itemTemplate'], $element);
43
      }
44
45 7
      $items[$key] = $item;
46 7
    }
47
48 7
    Yii::app()->controller->widget('FMenu', CMap::mergeArray($menuOptions, array('items' => $items)));
49 7
  }
50
51
  public static function header($header, $tag = 'h1', $htmlOptions = array())
52
  {
53
    echo CHtml::tag($tag, $htmlOptions, Yii::app()->meta->setHeader($header));
54
  }
55
56
  /**
57
   * Заменяет содержащиеся в $template выражения вида {выражение} на своиства или заначения $data
58
   * @param string $template шаблон
59
   * @param array|CComponent $data источник данных для замены объект или массив
60
   * @param bool $clearNotReplaced очистить не замененные выражения default false
61
   *
62
   * @return string
63
   */
64 1
  public static function replace($template, $data, $clearNotReplaced = false)
65
  {
66 1
    if( preg_match_all('/{([^{}\s]*)}/', $template, $matches) )
67 1
    {
68
      $replaceArray = array();
69
      foreach(Arr::reset($matches) as $matchIndex => $expression)
70
      {
71
        $attribute = $matches[1][$matchIndex];
72
73
        if( is_array($data) && isset($data[$attribute]) )
74
          $replaceArray[$expression] = $data[$attribute];
75
        else if( is_object($data) && isset($data->{$attribute}) )
76
          $replaceArray[$expression] = $data->{$attribute};
77
78
        if( $attribute == 'url' && is_array($replaceArray[$expression]) )
79
          $replaceArray[$expression] = CHtml::normalizeUrl($replaceArray[$expression]);
80
      }
81
82
      if( $replaceArray )
0 ignored issues
show
Bug Best Practice introduced by
The expression $replaceArray of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
83
        $template = strtr($template, $replaceArray);
84
    }
85
86
    if( $clearNotReplaced )
87 1
      $template = preg_replace('/({[^{}\s]*})/', '', $template);
88
89 1
    return $template;
90
  }
91
92
  /**
93
   * @return Contact|FActiveRecord|null
94
   */
95 7
  public static function contact()
96
  {
97 7
    if( $contact = Yii::app()->controller->getHeaderContacts() )
98 7
      return $contact;
99
100
    return null;
101
  }
102
103
  /**
104
   * @return ContactField[]
105
   */
106 7
  public static function phones()
107
  {
108 7
    if( $contact = self::contact() )
109 7
    {
110 7
      return $contact->getFields('phones');
111
    }
112
113
    return array();
114
  }
115
116
  /**
117
   * @param bool $clear
118
   *
119
   * @return ContactField|null|string
120
   */
121
  public static function phone($clear = false)
122
  {
123
    if( $phones = self::phones() )
124
    {
125
      $phone = Arr::reset($phones);
126
127
      return $clear ? $phone->getClearPhone() : $phone;
128
    }
129
130
    return null;
131
  }
132
133 7
  public static function getClearPhone($phone)
134
  {
135 7
    $clearPhone = preg_replace('/[^\d\+]/', '', $phone);
136
137 7
    if( $clearPhone == 10 )
138 7
    {
139
      if( $clearPhone[0] == 8 )
140
        $clearPhone = ltrim($clearPhone, '8');
141
      $clearPhone = '+'.$clearPhone;
142
    }
143
144 7
    return $clearPhone;
145
  }
146
}