original_string: public function getPreferredLanguage(array $locales = null)
      {
          $preferredLanguages = $this->getLanguages();
  
          if (empty($locales)) {
              return isset($preferredLanguages[0]) ? $preferredLanguages[0] : null;
          }
  
          if (!$preferredLanguages) {
              return $locales[0];
          }
  
          $extendedPreferredLanguages = [];
          foreach ($preferredLanguages as $language) {
              $extendedPreferredLanguages[] = $language;
              if (false !== $position = strpos($language, '_')) {
                  $superLanguage = substr($language, 0, $position);
                  if (!\in_array($superLanguage, $preferredLanguages)) {
                      $extendedPreferredLanguages[] = $superLanguage;
                  }
              }
          }
  
          $preferredLanguages = array_values(array_intersect($extendedPreferredLanguages, $locales));
  
          return isset($preferredLanguages[0]) ? $preferredLanguages[0] : $locales[0];
      }
code: public function getPreferredLanguage(array $locales = null)
      {
          $preferredLanguages = $this->getLanguages();
  
          if (empty($locales)) {
              return isset($preferredLanguages[0]) ? $preferredLanguages[0] : null;
          }
  
          if (!$preferredLanguages) {
              return $locales[0];
          }
  
          $extendedPreferredLanguages = [];
          foreach ($preferredLanguages as $language) {
              $extendedPreferredLanguages[] = $language;
              if (false !== $position = strpos($language, '_')) {
                  $superLanguage = substr($language, 0, $position);
                  if (!\in_array($superLanguage, $preferredLanguages)) {
                      $extendedPreferredLanguages[] = $superLanguage;
                  }
              }
          }
  
          $preferredLanguages = array_values(array_intersect($extendedPreferredLanguages, $locales));
  
          return isset($preferredLanguages[0]) ? $preferredLanguages[0] : $locales[0];
      }
code_tokens: public function getPreferredLanguage ( array $ locales = null ) { $ preferredLanguages = $ this -> getLanguages ( ) ; if ( empty ( $ locales ) ) { return isset ( $ preferredLanguages [ 0 ] ) ? $ preferredLanguages [ 0 ] : null ; } if ( ! $ preferredLanguages ) { return $ locales [ 0 ] ; } $ extendedPreferredLanguages = [ ] ; foreach ( $ preferredLanguages as $ language ) { $ extendedPreferredLanguages [ ] = $ language ; if ( false !== $ position = strpos ( $ language , '_' ) ) { $ superLanguage = substr ( $ language , 0 , $ position ) ; if ( ! \ in_array ( $ superLanguage , $ preferredLanguages ) ) { $ extendedPreferredLanguages [ ] = $ superLanguage ; } } } $ preferredLanguages = array_values ( array_intersect ( $ extendedPreferredLanguages , $ locales ) ) ; return isset ( $ preferredLanguages [ 0 ] ) ? $ preferredLanguages [ 0 ] : $ locales [ 0 ] ; }
docstring: Returns the preferred language.
  
  @param array $locales An array of ordered available locales
  
  @return string|null The preferred locale
docstring_tokens: Returns the preferred language .