diff options
Diffstat (limited to 'crates/shirabe-class-map-generator/src')
4 files changed, 75 insertions, 47 deletions
diff --git a/crates/shirabe-class-map-generator/src/class_map.rs b/crates/shirabe-class-map-generator/src/class_map.rs index 51d2bef..311924b 100644 --- a/crates/shirabe-class-map-generator/src/class_map.rs +++ b/crates/shirabe-class-map-generator/src/class_map.rs @@ -1,8 +1,10 @@ //! ref: composer/vendor/composer/class-map-generator/src/ClassMap.php use indexmap::IndexMap; -use shirabe_php_shim::{Countable, InvalidArgumentException, OutOfBoundsException, rtrim, strpos, strtr}; use shirabe_external_packages::composer::pcre::preg::Preg; +use shirabe_php_shim::{ + Countable, InvalidArgumentException, OutOfBoundsException, rtrim, strpos, strtr, +}; #[derive(Debug, Clone)] pub struct PsrViolationEntry { @@ -105,20 +107,25 @@ impl ClassMap { self.psr_violations .entry(path) .or_default() - .push(PsrViolationEntry { warning, class_name }); + .push(PsrViolationEntry { + warning, + class_name, + }); } pub fn clear_psr_violations_by_path(&mut self, path_prefix: &str) { let path_prefix = rtrim(&strtr(path_prefix, "\\", "/"), Some("/")); self.psr_violations.retain(|path, _| { - path != &path_prefix - && strpos(path, &format!("{}/", path_prefix)) != Some(0) + path != &path_prefix && strpos(path, &format!("{}/", path_prefix)) != Some(0) }); } pub fn add_ambiguous_class(&mut self, class_name: String, path: String) { - self.ambiguous_classes.entry(class_name).or_default().push(path); + self.ambiguous_classes + .entry(class_name) + .or_default() + .push(path); } /// Get the raw psr violations diff --git a/crates/shirabe-class-map-generator/src/class_map_generator.rs b/crates/shirabe-class-map-generator/src/class_map_generator.rs index af3abb4..1d2084d 100644 --- a/crates/shirabe-class-map-generator/src/class_map_generator.rs +++ b/crates/shirabe-class-map-generator/src/class_map_generator.rs @@ -1,19 +1,17 @@ //! ref: composer/vendor/composer/class-map-generator/src/ClassMapGenerator.php -use shirabe_php_shim::{ - DIRECTORY_SEPARATOR, PATHINFO_EXTENSION, PHP_INT_MAX, - InvalidArgumentException, LogicException, RuntimeException, - explode, getcwd, implode, in_array, is_dir, is_file, is_string, - pathinfo, preg_quote, realpath, str_replace, str_starts_with, - stream_get_wrappers, strlen, strrpos, strpos, strtr, substr, - sprintf, PhpMixed, -}; -use shirabe_external_packages::composer::pcre::preg::Preg; -use shirabe_external_packages::symfony::component::finder::finder::Finder; -use shirabe_external_packages::symfony::component::finder::spl_file_info::SplFileInfo; use crate::class_map::ClassMap; use crate::file_list::FileList; use crate::php_file_parser::PhpFileParser; +use shirabe_external_packages::composer::pcre::preg::Preg; +use shirabe_external_packages::symfony::component::finder::finder::Finder; +use shirabe_external_packages::symfony::component::finder::spl_file_info::SplFileInfo; +use shirabe_php_shim::{ + DIRECTORY_SEPARATOR, InvalidArgumentException, LogicException, PATHINFO_EXTENSION, PHP_INT_MAX, + PhpMixed, RuntimeException, explode, getcwd, implode, in_array, is_dir, is_file, is_string, + pathinfo, preg_quote, realpath, sprintf, str_replace, str_starts_with, stream_get_wrappers, + strlen, strpos, strrpos, strtr, substr, +}; #[derive(Debug)] pub struct ClassMapGenerator { @@ -29,10 +27,8 @@ impl ClassMapGenerator { .iter() .map(|w| preg_quote(w, None)) .collect(); - let stream_wrappers_regex = sprintf( - "{^(?:%s)://}", - &[PhpMixed::String(implode("|", &wrappers))], - ); + let stream_wrappers_regex = + sprintf("{^(?:%s)://}", &[PhpMixed::String(implode("|", &wrappers))]); ClassMapGenerator { extensions, @@ -82,7 +78,8 @@ impl ClassMapGenerator { true, ) { return Err(anyhow::anyhow!(InvalidArgumentException { - message: "$autoloadType must be one of: \"psr-0\", \"psr-4\" or \"classmap\"".to_string(), + message: "$autoloadType must be one of: \"psr-0\", \"psr-4\" or \"classmap\"" + .to_string(), code: 0, })); } @@ -91,7 +88,9 @@ impl ClassMapGenerator { if autoload_type != "classmap" { if !is_string(&path) { return Err(anyhow::anyhow!(InvalidArgumentException { - message: "$path must be a string when specifying a psr-0 or psr-4 autoload type".to_string(), + message: + "$path must be a string when specifying a psr-0 or psr-4 autoload type" + .to_string(), code: 0, })); } @@ -115,7 +114,11 @@ impl ClassMapGenerator { "/\\.(?:{})$/", implode( "|", - &self.extensions.iter().map(|e| preg_quote(e, None)).collect::<Vec<_>>(), + &self + .extensions + .iter() + .map(|e| preg_quote(e, None)) + .collect::<Vec<_>>(), ) ); Finder::create() @@ -137,7 +140,9 @@ impl ClassMapGenerator { } } else { // $path is already an array or Traversable of SplFileInfo - todo!("non-string path (Traversable/array of SplFileInfo) is not yet handled in Phase A") + todo!( + "non-string path (Traversable/array of SplFileInfo) is not yet handled in Phase A" + ) }; let cwd = realpath(&getcwd().unwrap_or_default()).unwrap_or_default(); @@ -164,8 +169,8 @@ impl ClassMapGenerator { file_path = format!("{}/{}", cwd, file_path); file_path = Self::normalize_path(&file_path); } else { - file_path = Preg::replace(r"{(?<!:)[\\/]{2,}}", "/", &file_path) - .unwrap_or(file_path); + file_path = + Preg::replace(r"{(?<!:)[\\/]{2,}}", "/", &file_path).unwrap_or(file_path); } if file_path.is_empty() { diff --git a/crates/shirabe-class-map-generator/src/php_file_cleaner.rs b/crates/shirabe-class-map-generator/src/php_file_cleaner.rs index 9f7d781..1a33533 100644 --- a/crates/shirabe-class-map-generator/src/php_file_cleaner.rs +++ b/crates/shirabe-class-map-generator/src/php_file_cleaner.rs @@ -1,9 +1,9 @@ //! ref: composer/vendor/composer/class-map-generator/src/PhpFileCleaner.php -use std::sync::Mutex; use indexmap::IndexMap; -use shirabe_php_shim::preg_quote; use shirabe_external_packages::composer::pcre::preg::Preg; +use shirabe_php_shim::preg_quote; +use std::sync::Mutex; #[derive(Debug, Clone)] struct TypeConfigEntry { @@ -90,7 +90,7 @@ impl PhpFileCleaner { if char == '<' && self.peek('<') { let mut r#match: Vec<String> = vec![]; if self.r#match( - r"{<<<[ \t]*+(['\"]?)([a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*+)\1(?:\r\n|\n|\r)}A", + r#"{<<<[ \t]*+(['\"]?)([a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*+)\1(?:\r\n|\n|\r)}A"#, Some(&mut r#match), ) { self.index += r#match[0].len(); @@ -120,9 +120,7 @@ impl PhpFileCleaner { }; if let Some(entry) = type_entry { let end = self.index + entry.length; - if end <= self.len - && &self.contents[self.index..end] == entry.name - { + if end <= self.len && &self.contents[self.index..end] == entry.name { let offset = if self.index > 0 { self.index - 1 } else { 0 }; let mut r#match: Vec<String> = vec![]; if Preg::is_match_at( diff --git a/crates/shirabe-class-map-generator/src/php_file_parser.rs b/crates/shirabe-class-map-generator/src/php_file_parser.rs index b68a33a..9bc7c5c 100644 --- a/crates/shirabe-class-map-generator/src/php_file_parser.rs +++ b/crates/shirabe-class-map-generator/src/php_file_parser.rs @@ -1,17 +1,15 @@ //! ref: composer/vendor/composer/class-map-generator/src/PhpFileParser.php -use std::sync::OnceLock; +use crate::php_file_cleaner::PhpFileCleaner; use anyhow::anyhow; use indexmap::IndexMap; +use shirabe_external_packages::composer::pcre::preg::Preg; use shirabe_php_shim::{ - PHP_EOL, PHP_VERSION_ID, HHVM_VERSION, - RuntimeException, - error_get_last, file_exists, file_get_contents, function_exists, - is_file, is_readable, ltrim, php_strip_whitespace, sprintf, + HHVM_VERSION, PHP_EOL, PHP_VERSION_ID, RuntimeException, error_get_last, file_exists, + file_get_contents, function_exists, is_file, is_readable, ltrim, php_strip_whitespace, sprintf, str_replace_array, strrpos, substr, trim, version_compare, }; -use shirabe_external_packages::composer::pcre::preg::Preg; -use crate::php_file_cleaner::PhpFileCleaner; +use std::sync::OnceLock; pub struct PhpFileParser; @@ -38,11 +36,15 @@ impl PhpFileParser { // The input file was really empty and thus contains no classes return Ok(vec![]); } else { - message = "File at \"%s\" could not be parsed as PHP, it may be binary or corrupted"; + message = + "File at \"%s\" could not be parsed as PHP, it may be binary or corrupted"; } let error = error_get_last(); - let mut message = sprintf(message, &[shirabe_php_shim::PhpMixed::String(path.to_string())]); + let mut message = sprintf( + message, + &[shirabe_php_shim::PhpMixed::String(path.to_string())], + ); if let Some(error) = error { if let Some(err_msg) = error.get("message") { message = format!( @@ -87,16 +89,33 @@ impl PhpFileParser { let len = matches.get("type").map(|v| v.len()).unwrap_or(0); for i in 0..len { - let ns = matches.get("ns").and_then(|v| v.get(i)).map(|s| s.as_str()).unwrap_or(""); + let ns = matches + .get("ns") + .and_then(|v| v.get(i)) + .map(|s| s.as_str()) + .unwrap_or(""); if !ns.is_empty() { - let nsname = matches.get("nsname").and_then(|v| v.get(i)).map(|s| s.as_str()).unwrap_or(""); + let nsname = matches + .get("nsname") + .and_then(|v| v.get(i)) + .map(|s| s.as_str()) + .unwrap_or(""); namespace = str_replace_array( - &[" ".to_string(), "\t".to_string(), "\r".to_string(), "\n".to_string()], + &[ + " ".to_string(), + "\t".to_string(), + "\r".to_string(), + "\n".to_string(), + ], &["".to_string()], nsname, ) + "\\"; } else { - let name = matches.get("name").and_then(|v| v.get(i)).map(|s| s.as_str()).unwrap_or(""); + let name = matches + .get("name") + .and_then(|v| v.get(i)) + .map(|s| s.as_str()) + .unwrap_or(""); // skip anon classes extending/implementing if name == "extends" { continue; @@ -151,8 +170,7 @@ impl PhpFileParser { let mut extra_types = String::new(); let mut extra_types_array: Vec<String> = vec![]; if PHP_VERSION_ID >= 80100 - || (HHVM_VERSION.is_some() - && version_compare(HHVM_VERSION.unwrap(), "3.3", ">=")) + || (HHVM_VERSION.is_some() && version_compare(HHVM_VERSION.unwrap(), "3.3", ">=")) { extra_types += "|enum"; extra_types_array = vec!["enum".to_string()]; |
