PhpExclusionsFileFilter implements InputFileFilter { @Override public boolean accept(InputFile inputFile) { if (!Php.KEY.equals(inputFile.language())) { return true; } String relativePath = inputFile.uri().toString(); if (WildcardPattern.match(WildcardPattern.create(excludedPatterns), relativePath)) { LOG.debug("File [" + inputFile.uri() + "] is excluded by '" + PhpPlugin.PHP_EXCLUSIONS_KEY + "' property and will not be analyzed"); return false; } if (new AverageLineLengthCalculator(inputFile).getAverageLineLength() > DEFAULT_AVERAGE_LINE_LENGTH_THRESHOLD) { LOG.debug("File [" + inputFile.uri() + "] is excluded because it is considered generated (average line length is too big)."); return false; } return true; } PhpExclusionsFileFilter(Configuration configuration); @Override boolean accept(InputFile inputFile);  }
@Test public void should_exclude_vendor_dir() { MapSettings settings = new MapSettings(); settings.setProperty(PhpPlugin.PHP_EXCLUSIONS_KEY, PhpPlugin.PHP_EXCLUSIONS_DEFAULT_VALUE); PhpExclusionsFileFilter filter = new PhpExclusionsFileFilter(settings.asConfig()); assertThat(filter.accept(inputFile("some_app.php"))).isTrue(); assertThat(filter.accept(inputFile("vendor/some_lib.php"))).isFalse(); assertThat(filter.accept(inputFile("vendor/my_lib_folder/my_lib.php"))).isFalse(); assertThat(filter.accept(inputFile("sub_module/vendor/submodule_lib.php"))).isFalse(); }
@Test public void should_exclude_only_php() { MapSettings settings = new MapSettings(); settings.setProperty(PhpPlugin.PHP_EXCLUSIONS_KEY, PhpPlugin.PHP_EXCLUSIONS_DEFAULT_VALUE); PhpExclusionsFileFilter filter = new PhpExclusionsFileFilter(settings.asConfig()); assertThat(filter.accept(inputFile("vendor/some_lib.php"))).isFalse(); assertThat(filter.accept(inputFile("vendor/some_lib.ts"))).isTrue(); }
@Test public void should_include_vendor_when_property_is_empty() { MapSettings settings = new MapSettings(); settings.setProperty(PhpPlugin.PHP_EXCLUSIONS_KEY, ""); PhpExclusionsFileFilter filter = new PhpExclusionsFileFilter(settings.asConfig()); assertThat(filter.accept(inputFile("some_app.php"))).isTrue(); assertThat(filter.accept(inputFile("vendor/some_lib.php"))).isTrue(); }
@Test public void should_exclude_using_custom_path_regex() { MapSettings settings = new MapSettings(); settings.setProperty( PhpPlugin.PHP_EXCLUSIONS_KEY, PhpPlugin.PHP_EXCLUSIONS_DEFAULT_VALUE + "," + "**/libs/**"); PhpExclusionsFileFilter filter = new PhpExclusionsFileFilter(settings.asConfig()); assertThat(filter.accept(inputFile("some_app.php"))).isTrue(); assertThat(filter.accept(inputFile("vendor/some_lib.php"))).isFalse(); assertThat(filter.accept(inputFile("libs/some_lib.php"))).isFalse(); }
@Test public void should_ignore_empty_path_regex() { MapSettings settings = new MapSettings(); settings.setProperty(PhpPlugin.PHP_EXCLUSIONS_KEY, "," + PhpPlugin.PHP_EXCLUSIONS_DEFAULT_VALUE + ","); PhpExclusionsFileFilter filter = new PhpExclusionsFileFilter(settings.asConfig()); assertThat(filter.accept(inputFile("some_app.php"))).isTrue(); assertThat(filter.accept(inputFile("vendor/some_lib.php"))).isFalse(); }
@Test(timeout = 2000) public void should_ignore_php_files_with_average_line_length_over_threshold() throws IOException { PhpExclusionsFileFilter filter = new PhpExclusionsFileFilter(new MapSettings().asConfig()); File baseDir = new File("src/test/resources/exclusions"); SensorContextTester context = SensorContextTester.create(baseDir); DefaultInputFile testFile = setupSingleFile(baseDir, context, "excluded.php"); assertThat(filter.accept(testFile)).isFalse(); testFile = setupSingleFile(baseDir, context, "included.php"); assertThat(filter.accept(testFile)).isTrue(); testFile = setupSingleFile(baseDir, context, "excluded2.php"); assertThat(filter.accept(testFile)).isFalse(); testFile = setupSingleFile(baseDir, context, "excluded3.php"); assertThat(filter.accept(testFile)).isFalse(); testFile = setupSingleFile(baseDir, context, "excluded4.php"); assertThat(filter.accept(testFile)).isFalse(); testFile = setupSingleFile(baseDir, context, "unknown.php", ""); try { filter.accept(testFile); fail("unknown file should have failed"); } catch (AnalysisException ae) { assertThat(ae).hasMessage("Unable to read file 'unknown.php'"); } testFile = setupSingleFile(baseDir, context, "empty.php"); assertThat(filter.accept(testFile)).isTrue(); }