Filters file report producing another one containing only the lines affected by diff

@param string $path File path
@param array $report Report data

@return array
320697
public function filter(string $path, array $report) : array
    {
        $report['messages'] = isset($this->paths[$path]) ? array_intersect_key(
            $report['messages'],
            array_flip($this->paths[$path])
        ) : [];

        $errors = $warnings = $fixable = 0;

        foreach ($report['messages'] as $line) {
            foreach ($line as $messages) {
                foreach ($messages as $message) {
                    switch ($message['type']) {
                        case 'ERROR':
                            $errors++;
                            break;
                        case 'WARNING':
                            $warnings++;
                            break;
                    }

                    if ($message['fixable']) {
                        $fixable++;
                    }
                }
            }
        }

        return array_merge($report, [
            'errors' => $errors,
            'warnings' => $warnings,
            'fixable' => $fixable,
        ]);
    }
714049
private function filterDiffs($compRes)
    {
        $result = [];

        if (is_array($this->config['updateTypes'])) {
            $updateActions = $this->config['updateTypes'];
        } else {
            $updateActions = array_map('trim', explode(',', $this->config['updateTypes']));
        }

        $allowedActions = ['create', 'drop', 'add', 'remove', 'modify'];
        $updateActions = array_intersect($updateActions, $allowedActions);

        foreach ($compRes as $table => $info) {
            if ($info['sourceOrphan']) {
                if (in_array('create', $updateActions)) {
                    $result[$table] = $info;
                }
            } elseif ($info['destOrphan']) {
                if (in_array('drop', $updateActions)) {
                    $result[$table] = $info;
                }
            } elseif ($info['differs']) {
                $resultInfo = $info;
                unset($resultInfo['differs']);

                foreach ($info['differs'] as $diff) {
                    if (empty($diff['dest']) && in_array('add', $updateActions)) {
                        $resultInfo['differs'][] = $diff;
                    } elseif (empty($diff['source']) && in_array('remove', $updateActions)) {
                        $resultInfo['differs'][] = $diff;
                    } elseif (in_array('modify', $updateActions)) {
                        $resultInfo['differs'][] = $diff;
                    }
                }

                if (!empty($resultInfo['differs'])) {
                    $result[$table] = $resultInfo;
                }
            }
        }
        return $result;
    }
1162463
protected function getReportAttachment(common_report_Report $report)
    {
        $filename = null;
        /** @var common_report_Report $success */
        foreach ($report->getSuccesses() as $success) {
            if (!is_null($filename = $success->getData())) {
                if (is_array($filename)) {
                    $filename = $filename['uriResource'];
                }
                break;
            }
        }
        return $filename;
    }
191818
public function listFiles(string $path = '', bool $recursive = false)
    {
        $contents = $this->listContents($path, $recursive);

        $filter = function ($object) {
            return $object['type'] === 'file';
        };

        return \array_values(\array_filter($contents, $filter));
    }
915024
private function getPhpFilesMatchingPatternForCommandFromGivenPath($path)
    {
        $folders = ['Commands','commands','Command'];
        foreach ($folders as $f) {
            $cPath = $path . DIRECTORY_SEPARATOR . $f . DIRECTORY_SEPARATOR;

            if (!is_dir($cPath)) {
                continue;
            }
            $files = glob("$cPath*[cC]ommand\.php");

            return $files;
        }
        return [];
    }
894806
protected function normalizeResponse(array $response, $path = null)
    {
        $result = [
            'path' => $path ?: $this->removePathPrefix(
                isset($response['Key']) ? $response['Key'] : $response['Prefix']
            ),
        ];
        $result = array_merge($result, Util::pathinfo($result['path']));

        if (isset($response['LastModified'])) {
            $result['timestamp'] = strtotime($response['LastModified']);
        }

        if (substr($result['path'], -1) === '/') {
            $result['type'] = 'dir';
            $result['path'] = rtrim($result['path'], '/');

            return $result;
        }

        return array_merge($result, Util::map($response, static::$resultMap), ['type' => 'file']);
    }
1010006
private function getFilesList($path, $relation) {
		$fileSystemTypes = FileSystemTypes::getInstance();
		LogMaster::log("Query filesystem: ".$path);
		$dir = opendir($path.'/'.$relation);
		$result = new FileSystemResult();
		// forms fields list
		for ($i = 0; $i < count($this->config->data); $i++) {
			$fields[] = $this->config->data[$i]['db_name'];
		}
		// for every file and directory of folder
		while ($file = readdir($dir)) {
			// . and .. should not be in output list
			if (($file == '.')||($file == '..')) {
				continue;
			}
			$newFile = array();
			// parse file name as Array('name', 'ext', 'is_dir')
			$fileNameExt = $this->parseFileName($path.'/'.$relation, $file);
			// checks if file should be in output array
			if (!$fileSystemTypes->checkFile($file, $fileNameExt)) {
				continue;
			}
			// takes file stat if it's need
			if ((in_array('size', $fields))||(in_array('date', $fields))) {
				$fileInfo = stat($path.'/'.$file);
			}

			// for every field forms list of fields
			for ($i = 0; $i < count($fields); $i++) {
				$field = $fields[$i];
				switch ($field) {
					case 'filename':
						$newFile['filename'] = $file;
						break;
					case 'full_filename':
						$newFile['full_filename'] = $path."/".$file;
						break;
					case 'size':
						$newFile['size'] = $fileInfo['size'];
						break;
					case 'extention':
						$newFile['extention'] = $fileNameExt['ext'];
						break;
					case 'name':
						$newFile['name'] = $fileNameExt['name'];
						break;
					case 'date':
						$newFile['date'] = date("Y-m-d H:i:s", $fileInfo['ctime']);
						break;
				}
				$newFile['relation_id'] = $relation.'/'.$file;
				$newFile['safe_name'] = $this->setFileName($relation.'/'.$file);
				$newFile['is_folder'] = $fileNameExt['is_dir'];
			}
			// add file in output list
			$result->addFile($newFile);
		}
		return $result;
	}
553140
protected function _list($path, $prefix = '', $mode = 'file', $recursive = false, $filter = null)
    {
        $files  = [];
        $ignore = ['.', '..', '.DS_Store', '.svn', '.git', '.gitignore', '.gitmodules', 'cgi-bin'];

        if ($scan = @scandir($path)) {
            foreach ($scan as $file) {
                // continue if ignore match
                if (Arr::in($file, $ignore)) {
                    continue;
                }

                if (is_dir($path . '/' . $file)) {
                    // add dir
                    if ($mode === 'dir') {
                        // continue if no regex filter match
                        if ($filter && !preg_match($filter, $file)) {
                            continue;
                        }

                        $files[] = $prefix . $file;
                    }

                    // continue if not recursive
                    if (!$recursive) {
                        continue;
                    }

                    // read subdirectory
                    $files = array_merge(
                        $files,
                        $this->_list($path . '/' . $file, $prefix . $file . '/', $mode, $recursive, $filter)
                    );

                } else {
                    // add file
                    if ($mode === 'file') {
                        // continue if no regex filter match
                        if ($filter && !preg_match($filter, $file)) {
                            continue;
                        }

                        $files[] = $prefix.$file;
                    }
                }
            }
        }

        return $files;
    }
978147
private static function scanBfs(string $path, bool $ignoreDir = true): array
    {
        $files = [];
        $queue = new \SplQueue();
        $queue->enqueue($path);

        while (! $queue->isEmpty()) {
            $file = $queue->dequeue();
            $fileType = filetype($file);
            if ('dir' == $fileType) {
                $subFiles = self::scanCurrentDir($file, false);
                foreach ($subFiles as $subFile) {
                    $queue->enqueue($subFile);
                }
                if (false === $ignoreDir && $file !== $path) {
                    $files[] = $file;
                }
            }
            if ('file' == $fileType) {
                $files[] = $file;
            }
        }
        return $files;
    }
746300
public function checkFilter($path)
    {
        //No filters? Include all paths.
        if (empty($this->filters)) {
            return true;
        }

        //Match either with or without braces.
        $cleanPath = str_replace(array('{', '}'), array('', ''), $path);
        foreach ($this->filters as $filter) {
            if (stripos($path, $filter) !== false || stripos($cleanPath, $filter) !== false) {
                return true;
            }
        }
        return false;
    }
1271688
protected function findLighthouseScoreReportData(\SplFileInfo $path, array $selected)
    {
        $data = [];
        $json = json_decode(file_get_contents($path), true);

        foreach ($json['reportCategories'] as $report) {
            if (!isset($report['name']) || !isset($report['score'])) {
                continue;
            }
            $label = $report['name'];
            $key = Utility::cleanString(
                strtolower(strtr($label, ' ', '_')),
                '/[^a-zA-Z\_]/'
            );

            if (!in_array($key, $selected)) {
                continue;
            }

            $data[$key] = [
                'name' => $label,
                'score' => round($report['score'])
            ];
        }

        return $data;
    }
1153734
protected function getFileIterator($path, $pattern = '/^.+\.php$/')
    {
        if (is_dir($path)) {
            return new \RegexIterator(new \DirectoryIterator($path), $pattern, \RegexIterator::GET_MATCH);
        }
        if (is_file($path) && 1 === preg_match($pattern, $path)) {
            return [new \SplFileInfo($path)];
        }
        return [];
    }
1178729
protected function findNewFiles(string $path) : array
    {
        $arrReturn = [];
        $arrFolders = scandir($path);
        $arrSkip = [
            '.',
            '..',
        ];
        
        foreach ($arrFolders as $file) {
            //No Folder Defaults
            if (in_array($file, $arrSkip)) {
                continue;
            }
            //Process Subfolders
            if (is_dir($path.$file)) {
                $arrSubFolderFiles = $this->findNewFiles($path.$file."/");
                $arrReturn = $this->addArrayToNewFiles($arrSubFolderFiles, $arrReturn);
                continue;
            }
            $arrReturn[] = $path.$file;
        }
        
        //Remove file from list if already processed
        foreach($arrReturn as $key => $file) {
            if (in_array($file, $this->knownUpdateFiles)) {
                unset($arrReturn[$key]);
            }
        }
        return $arrReturn;
    }
193750
private function _get_items(string $path, $type = 'both', bool $hidden = false, string $detailed = ''): array
  {
    $files = [];
    if ( ($this->mode === 'ftp') && ($detailed || ($type !== 'both')) ){
      if ( $fs = ftp_mlsd($this->stream, substr($path, strlen($this->prefix))) ){
        foreach ( $fs as $f ){
          if ( ($f['name'] !== '.') && ($f['name'] !== '..') && ($hidden || (strpos(basename($f['name']), '.') !== 0)) ){
            $ok = 0;
            if ( $type === 'both' ){
              $ok = 1;
            }
            else if ( $type === 'dir' ){
              $ok = $f['type'] === 'dir';
            }
            else if ( $type === 'file' ){
              $ok = $f['type'] === 'file';
            }
            else if ( !is_string($type) || is_file($path.'/'.$f['name']) ){
              $ok = $this->_check_filter($f['name'], $type);
            }
            if ( $ok ){
              if ( $detailed ){
                if ( !isset($has_type, $has_mod) ){
                  $has_type = stripos($detailed, 't') !== false;
                  $has_mod = stripos($detailed, 'm') !== false;
                }
                $tmp = [
                  'path' => $path.'/'.$f['name']
                ];
                if ( $has_mod ){
                  $tmp['mtime'] = mktime(
                    substr($f['modify'], 8, 2),
                    substr($f['modify'], 10, 2),
                    substr($f['modify'], 12, 2),
                    substr($f['modify'], 4, 2),
                    substr($f['modify'], 6, 2),
                    substr($f['modify'], 0, 4)
                  );
                }
                if ( $has_type ){
                  $tmp['dir'] = $f['type'] === 'dir';
                  $tmp['file'] = $f['type'] !== 'dir';
                }
                $files[] = $tmp;
              }
              else{
                $files[] = $path.'/'.$f['name'];
              }
            }
          }
        }
      }
      else{
        bbn\x::log(error_get_last(), 'filesystem');
      }
    }
    else{
      $fs = scandir($path, SCANDIR_SORT_ASCENDING);
      foreach ( $fs as $f ){
        if ( ($f !== '.') && ($f !== '..') && ($hidden || (strpos(basename($f), '.') !== 0)) ){
          $ok = 0;
          $is_dir = null;
          $is_file = null;
          if ( $type === 'both' ){
            $ok = 1;
          }
          else if ( $type === 'dir' ){
            if ( $ok = is_dir($path.'/'.$f) ){
              $is_dir = $ok;
              $is_file = !$ok;
            }
          }
          else if ( $type === 'file' ){
            if ( $ok = is_file($path.'/'.$f) ){
              $is_file = $ok;
              $is_dir = !$ok;
            }
          }
          else if ( !is_string($type) || is_file($path.'/'.$f) ){
            $ok = $this->_check_filter($f, $type);
            $is_file = true;
            $is_dir = false;
          }
          if ( $ok ){
            if ( $detailed ){
              if ( !isset($has_type, $has_mod) ){
                $has_type = stripos($detailed, 't') !== false;
                $has_mod = stripos($detailed, 'm') !== false;
              }
              $tmp = [
                'path' => $path.'/'.$f
              ];
              if ( $has_mod ){
                $tmp['mtime'] = filemtime($path.'/'.$f);
              }
              if ( $has_type ){
                $tmp['dir'] = $is_dir ?? is_dir($path.'/'.$f);
                $tmp['file'] = $is_file ?? is_file($path.'/'.$f);
              }
              $files[] = $tmp;
            }
            else {
              $files[] = $path.'/'.$f;
            }
          }
        }
      }
    }
    if ( \count($files) > 0 ){
      //bbn\x::sort($files, 'real');
    }
    return $files;
  }
435635
protected function addressbookQueryReport($report)
    {
        $depth = $this->server->getHTTPDepth(0);

        if (0 == $depth) {
            $candidateNodes = [
                $this->server->tree->getNodeForPath($this->server->getRequestUri()),
            ];
            if (!$candidateNodes[0] instanceof ICard) {
                throw new ReportNotSupported('The addressbook-query report is not supported on this url with Depth: 0');
            }
        } else {
            $candidateNodes = $this->server->tree->getChildren($this->server->getRequestUri());
        }

        $contentType = $report->contentType;
        if ($report->version) {
            $contentType .= '; version='.$report->version;
        }

        $vcardType = $this->negotiateVCard(
            $contentType
        );

        $validNodes = [];
        foreach ($candidateNodes as $node) {
            if (!$node instanceof ICard) {
                continue;
            }

            $blob = $node->get();
            if (is_resource($blob)) {
                $blob = stream_get_contents($blob);
            }

            if (!$this->validateFilters($blob, $report->filters, $report->test)) {
                continue;
            }

            $validNodes[] = $node;

            if ($report->limit && $report->limit <= count($validNodes)) {
                // We hit the maximum number of items, we can stop now.
                break;
            }
        }

        $result = [];
        foreach ($validNodes as $validNode) {
            if (0 == $depth) {
                $href = $this->server->getRequestUri();
            } else {
                $href = $this->server->getRequestUri().'/'.$validNode->getName();
            }

            list($props) = $this->server->getPropertiesForPath($href, $report->properties, 0);

            if (isset($props[200]['{'.self::NS_CARDDAV.'}address-data'])) {
                $props[200]['{'.self::NS_CARDDAV.'}address-data'] = $this->convertVCard(
                    $props[200]['{'.self::NS_CARDDAV.'}address-data'],
                    $vcardType,
                    $report->addressDataProperties
                );
            }
            $result[] = $props;
        }

        $prefer = $this->server->getHTTPPrefer();

        $this->server->httpResponse->setStatus(207);
        $this->server->httpResponse->setHeader('Content-Type', 'application/xml; charset=utf-8');
        $this->server->httpResponse->setHeader('Vary', 'Brief,Prefer');
        $this->server->httpResponse->setBody($this->server->generateMultiStatus($result, 'minimal' === $prefer['return']));
    }
849119
def filter_files(self, path):
        
        excludes = r'|'.join([fnmatch.translate(x) for x in self.project.EXCLUDES]) or r'$.'
        for root, dirs, files in os.walk(path, topdown=True):
            dirs[:] = [d for d in dirs if not re.match(excludes, d)]
            dirs[:] = [os.path.join(root, d) for d in dirs]
            rel_path = os.path.relpath(root, path)

            paths = []
            for f in files:
                if rel_path == '.':
                    file_path = f
                else:
                    file_path = os.path.join(rel_path, f)
                if not re.match(excludes, file_path):
                    paths.append(f)

            files[:] = paths
            yield root, dirs, files
1134532
protected function filterPaths(array $paths, $pattern, array $extensions=[])
    {
        if (!$this->isFilterPattern($pattern) && !$extensions) {
            return $paths;
        }
        return array_filter($paths, function ($path) use ($pattern, $extensions) {
            return $this->filterPath($path, $pattern, $extensions);
        });
    }